0

I want to clip to specific values with reference to their location in a tensor. so I'm trying to get their locations when they equal to one in the tensor y. But I'm receiving this error message AttributeError: 'TensorVariable' object has no attribute 'nonezeros'

I can't post all the code but it is similar to the tutorial CNN on theano website.

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'
    )

I tested nonzero out of this CNN code and it worked as a test

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())
0

1 Answer 1

1

The method is called nonzero, that's why your second example worked:

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

and the first didn't:

loc = T.eq(y,1).nonezeros()[0]
#                  ^------- to fix it remove this "e"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.