加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:02:44 所属栏目:Python 来源:网络整理
导读:这是代码: import numpy as np# sigmoid functiondef nonlin(x,deriv=False): if(deriv==True): return x*(1-x) return 1/(1+np.exp(-x))# input datasetX = np.array([ [0,1],[0,1,[1,1] ])# output dataset y = np.array([[0,1]]).T# seed random numbers
这是代码:

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,1],[0,1,[1,1] ])

# output dataset            
y = np.array([[0,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函数给你一个HIGH或LOW值(非常好的信心),那个值的导数是LOW.如果得到最陡斜率(0.5)的值,则该值的导数为HIGH.

当函数给我们一个糟糕的预测时,我们希望用更高的数字改变我们的权重,相反,如果预测是好的(高信度),我们不想更改我们的权重.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读