1
我一直在試驗基本的神經網絡,並且我在網上發現了一些python代碼。但是,當我嘗試向網絡添加2個隱藏層時,我收到一個錯誤:爲什麼形狀不對齊?
文件「python」,第30行,在 ValueError:形狀(6,4)和(1,4)未對齊:4(dim 1)!= 1
有人可以請解釋這個問題是什麼?我無法在任何地方找到明確的解釋。謝謝!
代碼:
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([[1,0,1],
[0,1,0],
[1,1,0],
[0,0,0],
[0,0,1],
[1,1,1]])
y = np.array([[1,1,0,1,0,1]]).T
np.random.seed(1)
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,5)) - 1
syn2 = 2*np.random.random((5,4)) - 1
syn3 = 2*np.random.random((4,1)) - 1
for iter in range(100000):
l0 = X
l1 = nonlin(np.dot(l0,syn0))
l2 = nonlin(np.dot(l1,syn1))
l3 = nonlin(np.dot(l2,syn2))
l4 = nonlin(np.dot(l3,syn3))
l4_error = y - l4
l4_delta = l4_error*nonlin(l3,deriv=True)
l3_error = l4_delta.dot(syn3.T)
l3_delta = l3_error*nonlin(l3,deriv=True)
l2_error = l3_delta.dot(syn2.T)
l2_delta = l2_error*nonlin(l2,deriv=True)
l1_error = l2_delta.dot(syn1.T)
l1_delta = l1_error * nonlin(l1,deriv=True)
syn3 += l3.T.dot(l4_delta)
syn2 += l2.T.dot(l3_delta)
syn1 += l1.T.dot(l2_delta)
syn0 += l0.T.dot(l1_delta)
print("Neural network trained:")
while True:
l0 = np.array([[int(i) for i in input().split()]])
l1 = nonlin(np.dot(l0,syn0))
l2 = nonlin(np.dot(l1,syn1))
l3 = nonlin(np.dot(l2,syn2))
l4 = nonlin(np.dot(l3,syn3))
print("Output:")
if l4[0][0] > 0.5:
print("Yes")
else:
print("No")