我正在尝试进行反向传播,并且错误出现在训练的最后一步,更新权重。
错误如下
syn1 += l1.T.dot(l2_delta) * 0,2
ValueError: shapes (4,) and (1,) not aligned: 4 (dim 0) != 1 (dim 0)
我已经尝试了几种方法,但是找不到有用的答案。 这是代码
import numpy
import random
trainset = [[0,0,0],
[0,0,1],
[1,0,0],
[1,0,1],
[1,1,1],
[1,1,0],
[0,1,1]]
answerset = numpy.array([[0],[0],[0],[1],[0],[1],[1]])
def sigmoid(z, deriv=False):
if deriv==True:
return z*(1-z)
return 1/(1+numpy.exp(-z))
sigmoid(45)
syn0 = 2*numpy.random.random((3,4)) - 1
syn1 = 2*numpy.random.random((4,1)) - 1
for i in range(60000):
an = random.randint(0,len(trainset)-1)
l0 = trainset[an]
l1 = sigmoid(numpy.dot(l0,syn0))
l2 = sigmoid(numpy.dot(l1,syn1))
#print("l2"l2)
l2_error = answerset[an] - l2
if(i % 10000) == 0:
print(l2_error)
l2_delta = l2_error*sigmoid(l2, deriv=True)
l1_error = l2_delta.dot(syn1.T)
l1_delta = l1_error*sigmoid(l1,deriv=True)
print(l2_delta)
print(l1)
syn1 += l1.T.dot(l2_delta) * 0,2
syn0 += l0.T.dot(l1_delta) * 0,2
print(l2)