python实现BackPropagation算法
实现神经网络的权重和偏置更新,很重要的一部就是使用BackPropagation(反向传播)算法。具体来说,反向传播算法就是用误差的反向传播来计算w(权重)和b(偏置)相对于目标函数的导数,这样就可以在原来的w,b的基础上减去偏导数来更新。其中我上次写的python实现梯度下降中有一个函数backprop(x,y)就是用来实现反向传播的算法。(注:代码并非自己总结,github上有这个代码的实现https://github.com/LCAIZJ/neural-networks-and-deep-learning)
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]
# 通过输入x,前向计算输出层的值
activation = x
activations = [x]# 存储的是所以的输出层
zs = []
for b,w in zip(self.biases,self.weights):
z = np.dot(w,activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# 计算输出层的error
delta = self.cost_derivative(activations[-1],y)*sigmoid_prime(zs[:-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta,activations[-2].transpose())
#反向更新error
for l in xrange(2,self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weight[-l+1].transpose(),delta)*sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta,activations[-l-1].transpose())
return (nabla_b,nabla_w)
其中,传入的x和y是一个单独的实例。
def cost_derivative(self,output_activation,y):
return (output_activation-y)
def sigmoid(z):
return 1.0/(1.0+np.exp(z))
def sigmoid_prime(z):
return sigmoid(z)*(1-sigmoid(z))