【发布时间】:2021-04-19 10:13:02
【问题描述】:
我正在阅读free online book,但我正在努力处理某些代码。
(代码来源于 Michael Nielsen)
class Network(object):
def update_mini_batch(self, mini_batch, eta):
"""Update the network's weights and biases by applying
gradient descent using backpropagation to a single mini batch.
The "mini_batch" is a list of tuples "(x, y)", and "eta"
is the learning rate."""
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]
def backprop(self, x, y):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
因为它说mini_batch 是一个元组列表(x, y),所以函数backprop 中x 的参数是一个标量,对吧?如果是这样,因为w(权重)是一个矩阵(比如它的维度是n*p),其行在lth 层中有n 神经元,而在l-1 层中有p 神经元.那么,x 必须是 n x 1 向量。我感到困惑。
在本书的示例中,它使用了[2,3,1],即三层,分别有 2,3 和 1 个神经元。因为第一层输入,它有两个元素。所以第 2 层的权重矩阵有 3*2 维。看来x 应该是一个长度为2 的向量来与w 进行矩阵乘法。
另外,C_x 对激活 a 的偏导代码如下:
def cost_derivative(self, output_activations, y):
"""Return the vector of partial derivatives \partial C_x /
\partial a for the output activations."""
return (output_activations-y)
我检查了我理解的公式,(output_activations-y)表示成本的变化。但这应该除以激活的变化吗?
你能帮帮我吗?
【问题讨论】:
标签: python deep-learning backpropagation