Python 属性错误:';列表';对象没有属性';T';
我一直在尝试用python实现一个使用反向传播的神经网络,并不断得到上述错误。我怎样才能消除它呢。代码运行一个历元,而不计算系统中的错误,因此无法在网络上反向传播错误Python 属性错误:';列表';对象没有属性';T';,python,machine-learning,neural-network,Python,Machine Learning,Neural Network,我一直在尝试用python实现一个使用反向传播的神经网络,并不断得到上述错误。我怎样才能消除它呢。代码运行一个历元,而不计算系统中的错误,因此无法在网络上反向传播错误 import numpy as np X = [0.4, 0.7] y = [0.1] class Neural_Network(object): def __init__(self): #parameters self.inputSize = 2
import numpy as np
X = [0.4, 0.7]
y = [0.1]
class Neural_Network(object):
def __init__(self):
#parameters
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 2
#weights
self.W1 = [[0.1, 0.4],
[0.2, -0.2]] # (2x2) weight matrix from input to hidden layer
self.W2 = np.array([0.2, -0.5])[np.newaxis] # (2x1) weight matrix from hidden to output layer
def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z) # activation function
self.z3 = np.dot(self.z2, self.W2.T) # dot product of hidden layer (z2) and second set of 3x1 weights
o = self.sigmoid(self.z3) # final activation function
return o
def sigmoid(self, s):
# activation function
return 1/(1+np.exp(-s))
def sigmoidPrime(self, s):
#derivative of sigmoid
return s * (1 - s)
def backward(self, X, y, o):
# backward propgate through the network
self.o_error = y - o # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
self.z2_error = self.o_delta.dot(self.W2) # z2 error: how much our hidden layer weights contributed to output error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
def train (self, X, y):
o = self.forward(X)
self.backward(X, y, o)
NN = Neural_Network()
for i in xrange(1000): # trains the NN 1,000 times
print "Input: \n" + str(X)
print "Actual Output: \n" + str(y)
print "Predicted Output: \n" + str(NN.forward(X))
print "Loss: \n" + str(np.mean(np.square(y - NN.forward(X)))) # mean sum squared loss
print "\n"
NN.train(X, y)
我得到的错误是
File "C:/Users/Aaa/AppData/Local/Temp/abc.py", line 43, in backward
self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
AttributeError: 'list' object has no attribute 'T'
您正在使用的
X
是一个。您应该使用:
X
是一个列表
。您可以通过键入type(X)
看到这一点。和列表没有转置方法。您需要一个数组,因此将X=[0.4,0.7]
替换为:
X = np.array([0.4, 0.7])
哦,顺便说一句:X=np的转置。数组([0.4,0.7])
将与X
相同:
print(np.all(X.T == X))
# Out: True
这适用于所有一维的
X
。答案也应应用于您的self.W1
,它现在也是一个列表。使用np.array后,我得到另一个错误,但似乎已经解决了原始错误。-->self.z2_error=self.o_delta.dot(self.W2)#z2错误:隐藏层权重对输出错误值的贡献程度错误:形状(1,)和(2,)未对齐:1(尺寸0)!=2(尺寸0)
错误行?然而,你应该考虑问另一个问题。问题应该是一个单一的、有限的问题。在使用np.array之后,我得到了另一个错误,但似乎已经解决了原始错误。-->self.z2_error=self.o_delta.dot(self.W2)#z2错误:我们的隐藏层权重对输出错误值的贡献有多大错误:形状(1,)和(2,)未对齐:1(dim 0)!=2(dim 0)您应该打开一个新问题,因为这是另一个错误。正如卢卡指出的,一个问题应该是关于一个单一的有限的主题。否则没人会帮忙,因为你永远不知道问题何时解决。
print(np.all(X.T == X))
# Out: True