我试图理解为什么这个带有 Numpy 的示例神经网络不能学习非线性数据。即使是一个简单的神经网络也应该学习非线性数据,对吗?
我希望我的 NN 知道,如果输入是 1,那么如果输入大于 1 并且小于 4,则为 0,则为 1。如果值> 4,则为 0。我已经尝试了许多来自谷歌的带有numpy的示例NN代码,我似乎遇到了这个问题。
下面的代码不会学习,但使用所需的输入 [2,2,0,0] 需要 [1,1,0,0] 学习得很好。
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([ [1],
[2],
[3],
[4] ])
# #output dataset
y = np.array([[0,1,1,0]]).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((1,1)) - 1
for iter in range(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)
因为您的模型本质上是一个线性模型。如果要拟合非线性数据,则需要添加至少一个隐藏层。
如前所述,您已经构建了一个简单的线性逻辑回归模型。
NN 中的 sigmoid 仅用于获取模型的预测,而不是实际非线性训练 NN。
学习神经网络的一个良好开端是这样的: http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/