Unwanted [Nan] output in Python neural network

℡╲_俬逩灬. 提交于 2019-12-01 12:49:45

There are at least two issues with your code.

The first is the inexplicable use of 2 return statements in your sigmoid function, which should simply be:

def sigmoid(x):
  return 1/(1 + np.exp(-x))

which gives the correct result for x=0 (0.5), and goes to 1 for large x:

sigmoid(0)
# 0.5
sigmoid(20)
# 0.99999999793884631

Your (wrong) sigmoid:

def your_sigmoid(x):
  return x*(1-x)
  return 1/(1 + np.exp(-x))

can easily lead to overflow:

your_sigmoid(20)
# -380

The other issue is that your derivative is wrong; it should be:

def Sigmoid_Derivative(x):
    return sigmoid(x) * (1-sigmoid(x))

See the Derivative of sigmoid function thread at Math.SE, as well as the discussion here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!