回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<pre><code>import numpy as np
def nonlin(x, deriv=False):
if(deriv==True):
return(x*(1-x))
return 1/(1+np.exp (-x))
x = np.array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])
y = np.array([[0],
[1],
[1],
[0]])
#seed
np.random.seed(1)
#weights/synapses
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,1)) - 1
#training
for j in range(60000):
#layers (input, hidden, output)
#not a class, just thinking of neurons this way
#np.dot is mattrix multiplication
L0 = x
L1 = nonlin(np.dot(L0, syn0))
L2 = nonlin(np.dot(L1, syn1))
#backpropagation
l2_error = y - L2
if (j % 10000) == 0:
print ("Error:" + str(np.mean(np.abs(L2_error))))
#calculate deltas
L2_delta = L2_error*nonlin(L2, deriv=True)
L1_error = L2_delta.dot(syn1.T)
L1_delta = L1_error * nonlin(L1, deriv=True)
#update our synapses
syn1 += L1.T.dot(L2_delta)
syn0 += L0.T.dot(L1_delta)
print ("output after training")
print (L2)
</code></pre>
<p>错误信息是:“L2=nonlin(美国运输部(L1,合成1)
TypeError:不支持*:“NoneType”和“float”的操作数类型</p>
<p>这是一个非常基本的神经网络。出现错误的部分包括添加Layer1和syn1作为矩阵。我不确定是否需要将L2更改为float。这是我第一次在python上使用矩阵。在</p>