反向传播:当它乘以sigmoid的导数时,为什么误差不接近零?

时间:2017-09-24 21:40:28

标签: python neural-network backpropagation

我试图将反向传播实现到我看起来像这样的简单神经网络:2个输入,2个隐藏(sigmoid),1个输出(sigmoid)。但它似乎无法正常工作。

import numpy as np

 # Set inputs and labels
 X = np.array([ [0, 1],
                [0, 1],
                [1, 0],
                [1, 0] ])

 Y = np.array([[0, 0, 1, 1]]).T

 # Make random always the same
 np.random.seed(1)
 # Initialize weights
 w_0 = 2 * np.random.rand(2, 2) - 1
 w_1 = 2 * np.random.rand(1, 2) - 1

 # Learning Rate
 lr = 0.1

 # Sigmoid Function/Derivative of Sigmoid Function
 def sigmoid(x, deriv=False):
     if(deriv==True):
         return x * (1 - x)
     return 1/(1 + np.exp(-x))

 # Neural network
 def network(x, y, w_0, w_1):
     inputs = np.array(x, ndmin=2).T
     label = np.array(y, ndmin=2).T

     # Forward Pass
     hidden = sigmoid(np.dot(w_0, inputs))
     output = sigmoid(np.dot(w_1, hidden))

     # Calculate error and delta
     error = label - output
     delta = error * sigmoid(output, True)

     hidden_error = np.dot(w_1.T, error)
     delta_hidden = error * sigmoid(hidden, True)

     # Update weight
     w_1 += np.dot(delta, hidden.T) * lr
     w_0 += np.dot(delta_hidden, record.T) * lr

     return error

 # Train
 for i in range(6000):
     for j in range(X.shape[0]):
         error = network(X[j], Y[j], w_0, w_1)

         if(i%1000==0):
             print(error)

当我打印出我的错误时,我得到: Figure 1

哪个不对,因为它不接近0。

当我将delta更改为:

delta = error

它以某种方式起作用。 Figure 2

但为什么呢?在我们进一步传递之前,我们不应该将误差乘以sigmoid函数的导数吗?

1 个答案:

答案 0 :(得分:1)

我想,应该是

delta_hidden = hidden_error * sigmoid(hidden, True)