【问题标题】:AttributeError: 'TensorVariable' object has no attribute 'nonezeros'AttributeError:“TensorVariable”对象没有属性“nonezeros”
【发布时间】:2017-04-30 02:50:59
【问题描述】:

我想根据它们在张量中的位置剪辑到特定值。所以我试图在它们等于张量 y 中的 1 时获取它们的位置。但我收到此错误消息 AttributeError:“TensorVariable”对象没有属性“nonezeros”

我无法发布所有代码,但它类似于 theano 网站上的教程 CNN。

def MSE2(self, y):
    loc = T.eq(y,1).nonezeros()[0]
    # loc = np.where(y == 1)[0]
    S = T.clip(self.input1[loc],0,1)
    self.input1 = T.set_subtensor(self.input1[loc], S)
    return T.mean((y - self.input1) ** 2)

classifier.predictor.MSE2(y)

train_model = theano.function(
        inputs=[index],
        outputs=cost,
        updates=updates,
        givens={
            x: train_set_x[index * batch_size: (index + 1) * batch_size],
            y: train_set_y[index * batch_size: (index + 1) * batch_size]
        },
        on_unused_input='ignore'
    )

我从这个 CNN 代码中测试了非零值,它作为一个测试工作

class test:
    def __init__(self, X):
        self.X = X
    def cost_MSE(self, Y):
        loc = T.eq(Y, 1).nonzero()[0]
        self.X = T.set_subtensor(self.X[loc], T.clip(self.X[loc], 0, 1))
        return T.mean((Y - self.X)**2)

X = T.vector()
Y = T.ivector()

cnn = test(X)
MSE = cnn.cost_MSE(Y)
grads = T.grad(MSE, X)

x = np.array([.5, 10], np.float32)
y = np.array([0,1], np.int32)
y_test = theano.shared(y)
f = theano.function(
    inputs = [],
    outputs = grads,
    givens = {
        X: x,
        Y: y_test
    },
    on_unused_input = 'ignore')
print(f())

【问题讨论】:

    标签: python deep-learning theano


    【解决方案1】:

    该方法称为nonzero,这就是您的第二个示例有效的原因:

    loc = T.eq(Y, 1).nonzero()[0]
    

    第一个没有:

    loc = T.eq(y,1).nonezeros()[0]
    #                  ^------- to fix it remove this "e"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多