python - 为什么在神经网络中将误差乘以 sigmoid 的导数?

标签 python numpy machine-learning neural-network sigmoid

这是代码:

import numpy as np

# sigmoid function
def nonlin(x,deriv=False):
    if(deriv==True):
        return x*(1-x)
    return 1/(1+np.exp(-x))

# input dataset
X = np.array([  [0,0,1],
                [0,1,1],
                [1,0,1],
                [1,1,1] ])

# output dataset            
y = np.array([[0,0,1,1]]).T

# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)

# initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1

for iter in xrange(10000):

    # forward propagation
    l0 = X
    l1 = nonlin(np.dot(l0,syn0))

    # how much did we miss?
    l1_error = y - l1

    # multiply how much we missed by the 
    # slope of the sigmoid at the values in l1
    l1_delta = l1_error * nonlin(l1,True)

    # update weights
    syn0 += np.dot(l0.T,l1_delta)

print "Output After Training:"
print l1

这是网站:http://iamtrask.github.io/2015/07/12/basic-python-network/

代码第 36 行,l1 误差 乘以输入的导数以及权重。我不知道为什么要这样做,并且花了几个小时试图弄清楚。我刚刚得出这样的结论:这是错误的,但有些事情告诉我,考虑到有多少人推荐并使用本教程作为学习神经网络的起点,这可能是不正确的。

在文章中,他们这样说

Look at the sigmoid picture again! If the slope was really shallow (close to 0), then the network either had a very high value, or a very low value. This means that the network was quite confident one way or the other. However, if the network guessed something close to (x=0, y=0.5) then it isn't very confident.

我似乎无法理解为什么 sigmoid 函数输入的高低与置信度有任何关系。当然,它有多高并不重要,因为如果预测的产出很低,那么它就会非常不自信,不像他们所说的那样,因为它很高,所以应该有信心。

如果您想强调错误,那么将 l1_error 立方化肯定会更好吗?

考虑到到目前为止,我终于找到了一种有前途的方法来真正直观地开始学习神经网络,这真是令人失望,但我再次错了。如果您有一个我可以很容易理解的好地方来开始学习,我将不胜感激。

最佳答案

看看这张图片。如果 sigmoid 函数给你一个高值或低值(相当好的置信度),则该值的导数为低值。如果您在最陡斜率 (0.5) 处获得一个值,则该值的导数为“高”。

当函数给我们一个不好的预测时,我们希望将权重改变一个更大的数字,相反,如果预测良好(高置信度),我们不想改变我们的权重太多。

Sigmoid function and derivative

关于python - 为什么在神经网络中将误差乘以 sigmoid 的导数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45787261/

相关文章:

python - 如何使字典在类实异常(exception)部不可变但在 python 中的类实例内部可变

python - 检测连续图像的非/最小变化像素的最快方法

numpy - 如何检查两个 xarray 对象是否具有相同的维度和坐标(但不一定是值)

python - 改进最小/最大下采样

r - 插入符 : family specification in glmboost doesn't work

python - 如何在 matplotlib 中将次轴图安排在主轴图下方?

python - 使用 Flask-Kerberos 时无法通过 curl 访问应用程序

machine-learning - 使用 tflearn 进行回归的神经网络

machine-learning - 弱描述符与强描述符 - 机器视觉

python - 如何在 Django 中排除表单中的继承字段?