Theano: Why is there an indexing error in this case?

I am trying to get the maximum vector given by a boolean value.

With Numpy:

>>> this = np.arange(10) >>> this[~(this>=5)].max() 4 

But using Theano:

 >>> that = T.arange(10, dtype='int32') >>> that[~(that>=5)].max().eval() 9 >>> that[~(that>=5).nonzero()].max().eval() Traceback (most recent call last): File "<pyshell#146>", line 1, in <module> that[~(that>=5).nonzero()].max().eval() AttributeError: 'TensorVariable' object has no attribute 'nonzero' 

Why is this happening? Is this a subtle nuance that I miss?

+6
source share
1 answer

You are using a version of Tean that is too old. In fact, tensor_var.nonzero () is not in any released version. You need to update the development version.

In the development version, I have the following:

 >>> that[~(that>=5).nonzero()].max().eval() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for unary ~: 'tuple' 

This is because there is no bracket in your line. Here is a nice line:

 >>> that[(~(that>=5)).nonzero()].max().eval() array(9, dtype=int32) 

But we still have an unexpected result! The problem is that Theano does not support bool. Performing ~ on int8, performs bitwise inversion of 8 bits, not 1 bit. It gives the following result:

 >>> (that>=5).eval() array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=int8) >>> (~(that>=5)).eval() array([-1, -1, -1, -1, -1, -2, -2, -2, -2, -2], dtype=int8) 

You can remove ~ with this:

 >>> that[(that<5).nonzero()].max().eval() array(4, dtype=int32) 
+9
source

Source: https://habr.com/ru/post/946229/


All Articles