AttributeError:“列表"对象没有属性"T" [英] AttributeError: 'list' object has no attribute 'T'
问题描述
我一直在尝试在python中使用反向传播来实现神经网络,并不断收到上述错误.我该如何消除它.该代码仅运行一个时期,而没有计算系统中的错误,因此它无法在网络上向后传播错误
I have been trying to implement a neural network in python that uses back propagation and keep getting the above error. How can I go about eliminating it. The code runs for one epoch without calculating the error in the system hence it is not able to back propagate the error across the network
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
是list
.您可以通过键入type(X)
看到它.并且列表没有转置方法.您需要一个数组,因此将X = [0.4, 0.7]
替换为:
X
is a list
. You can see that by typing type(X)
. And lists do not have a transpose method. You want an array, so replace X = [0.4, 0.7]
with:
X = np.array([0.4, 0.7])
哦,顺便说一句:X = np.array([0.4, 0.7])
的转置与X
相同:
Oh and btw.: A transpose of X = np.array([0.4, 0.7])
will be the same as X
:
print(np.all(X.T == X))
# Out: True
对于所有具有一维尺寸的X
都是如此.
This is true for all X
with one dimension.
这篇关于AttributeError:“列表"对象没有属性"T"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!