【问题标题】:Receiving AttributeError: 'tuple' object has no attribute 'T'接收 AttributeError:“元组”对象没有属性“T”
【发布时间】:2019-10-20 19:03:53
【问题描述】:

我是这个网站的新手,如果我做的不对,但我遇到了问题,我很抱歉。我应该怎么做才能解决这个问题?

import numpy as np
X = (([0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]))    
h = (([0, 0], [0, 1], [1, 0], [1, 1]))
class NeuralNetworks(object):
    def __init__(self):
        self.InputSize = 4
        self.OutputSize = 2
        self.HiddenSize = 3

        self.w1 = np.random.randn(self.InputSize, self.HiddenSize)
        self.w2 = np.random.randn(self.HiddenSize, self.OutputSize)

    def forward(self, x):
        self.z = np.dot(x, self.w1) 
        self.z2 = self.sigmoid(self.z) 
        self.z3 = np.dot(self.z2, self.w2) 
        output = self.sigmoid(self.z3)
        return output

    def sigmoid(self, k ):
        return 1/(1 + np.exp(-k))
    def delta (self,k):
        return k*(1-k)
    def back(self, x, h, output):
       self.error = h-output 
       self.outputdelta = self.error *self.delta(output)
       self.z2error=self.outputdelta.dot(self.w2.T)
       self.z2delta = self.z2error * self.delta(self.z2)
       self.w1 += x.T.dot(self.z2delta)
       self.w2 += self.z2.T.dot(self.outputdelta)    

AttributeError: 'tuple' 对象没有属性 'T'

【问题讨论】:

  • 大概self.w2没有属性T
  • 函数def back(self, x, h, output):需要x、h、输出。你给了我们 x 和 y。哪个输入是 x 和 y。你为“输出”输入了什么?
  • idownvotedbecau.se/nomcve - 看起来self.w2 是一个元组,您正在尝试转置它? (假设它是一个 numpy 数组)。问题是没有 MCVE,如果不做假设就很难提供帮助......
  • @NickMartin 什么是 MCVE?
  • @hacquerqop 请添加更完整的代码,包括输入,以便我们重新创建您的错误。

标签: python backpropagation


【解决方案1】:

我假设您的输入应该是 NumPy 数组。您正在传递元组(由括号 () 表示)。这样做可能就足够了:

import numpy as np

X = np.array(([0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]))    
y = np.array(([0, 0], [0, 1], [1, 0], [1, 1]))

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 2021-07-30
    • 2013-07-29
    • 2020-08-29
    • 2015-04-22
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多