我用python编写了一个mlp,我编写了以下代码:
class NeuralNetwork(object):
def __init__(self):
#parameters
self.inputSize = 13
self.hidden1Size = 13
self.hidden2Size = 13
self.outputSize = 13
#weights
self.W1 = np.random.randn(self.inputSize, self.hidden1Size) # weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hidden1Size, self.hidden2Size) # weight matrix from hidden to hidden2 layer
self.W3 = np.random.randn(self.hidden2Size, self.outputSize) # weight matrix from hidden to output layer
def feedForward(self, X):
self.z = np.dot(X, self.W1) #Zh1
self.z2 = self.sigmoid(self.z) #ah1
self.z3 = np.dot(self.z2, self.W2) #Zh2
self.z4 = self.sigmoid(self.z3) #ah2
self.z5 = np.dot(self.z3, self.W3) #zout
output = self.sigmoid(self.z5) #a out
print(output)
return output
def sigmoid(self, s, deriv=False):
if (deriv == True):
return s * (1 - s)
return 1/(1 + np.exp(-s))
def backward(self, X, y, output):
#step1
self.output_error = output - y # error in output
#step2
self.output_delta = self.output_error * self.sigmoid(self.z3).T
self.W3 = self.W3 - self.output_delta
#step3
self.z2_error = self.output_delta.dot(self.output_delta)
self.z2_delta = self.z2_error * self.sigmoid(self.z3)
self.W2 = self.W2 - self.z2_delta
#step4
self.z1_error = self.output_delta.dot(self.W2.T)
self.z1_delta = self.z2_error * self.sigmoid(self.z2, deriv=True)
self.W1 = self.W1 - self.z1_delta
def train(self, X, y):
output = self.feedForward(X)
self.backward(X, y, output)
def accuracy (predict , y_test):
C = 0
length = len(y_test)
for i in length:
if predict[i] == y_test[i]:
C = C + 1
return(C/length)
但我有个问题:
ValueError Traceback (most recent
call last) <ipython-input-8-69e28bd743d3> in <module>
1 NN = NeuralNetwork()
2
----> 3 NN.train(X_train,y_train)
4
5 predict = NN.feedforward(X_train)
<ipython-input-7-55e55429732f> in train(self, X, y)
54 output = self.feedForward(X)
55
---> 56 self.backward(X, y, output)
57
58
<ipython-input-7-55e55429732f> in backward(self, X, y, output)
31 def backward(self, X, y, output):
32 #step1
---> 33 self.output_error = output - y # error in output
34
35 #step2
ValueError: operands could not be broadcast together with shapes
(242,13) (242,)
我知道问题是numpy不能做点函数,因为矩阵的问题,但我不知道如何解决这个问题。
1条答案
按热度按时间x9ybnkn61#
请参见以下与错误相同的示例:
所以你可以试试
self.backward(X, y, output[:, np.newaxis])
医生。挺烦人的。它转换一个一维数组(形状)
(n,)
)二维阵列(形状)(n, 1)
)那努比就可以应付了。