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))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持安科网。
相关推荐
scuyxi 2020-10-25
csdnfelix 2020-10-18
代码之神 2020-10-15
赶路人儿 2020-11-02
Pokemogo 2020-09-17
MasterCui 2020-09-08
Morelia 2020-09-04
meylovezn 2020-08-30
Hannah 2020-08-19
hugebawu 2020-08-17
scuyxi 2020-08-16
hang0 2020-08-16
higher0 2020-08-11
Tips 2020-08-08
Tristahong 2020-08-05
xiesheng 2020-08-02
huakai 2020-07-26
RememberMePlease 2020-06-26
zangdaiyang 2020-06-25