深度学习(三)Backpropagation算法过程

2017-09-28  本文已影响0人  静默zzzz

总结4个公式

首先介绍BP1



为什么:
这是因为根据链接法则
∂C/∂w =∂C/∂z * ∂z/∂w
z= wx + b
∂z/∂w = x 也就是上一层的a
'nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta,activations[-2].transpose()) '

隐藏层的遍历

计算该层delta需要 下一层的W 与 下一层的delta进行点乘

        for l in xrange(2,self.num_layers):
            z = zs[-l]
            sp = sigmoid_prime[z]
            delta = np.dot(self.weights[-l+1].transpose(),delta) * sp
            nabla_b[-l] = delta
            nabla_w[-l]= np.dot(delta,activations[-l-1].transpose())
        return(nabla_b,nabla_w)

num_layers:神经网络的层数

Backpropagation算法总结

    def backprop(self,x,y):
        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(zs)
            activations.append(activation)
        #backward pass
        delta = self.cost_derivatice(activations[-1],y) * sigmoid_prime(zs[-1])
        nabla_b[-1] = delta
        nabla_w[-1] = np.dot(delta,activations[-2].transpose()) 
        for l in xrange(2,self.num_layers):
            z = zs[-l]
            sp = sigmoid_prime[z]
            delta = np.dot(self.weights[-l+1].transpose(),delta) * sp
            nabla_b[-l] = delta
            nabla_w[-l]= np.dot(delta,activations[-l-1].transpose())
        return(nabla_b,nabla_w)
上一篇 下一篇

猜你喜欢

热点阅读