2013-05-31 131 views
6

我想获得给定布尔值的向量的最大值。Theano:为什么索引在这种情况下失败?

随着NumPy的:

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

但随着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' 

为什么会出现这种情况?这是我错过的微妙的细微差别吗?

+0

那么,对于你的第二个文字回溯是说,数组没有一个'非零()'方法/属性,所以你不能像使用numpy数组那样使用它。 –

+0

@JeffTratner:反对网站上提供的[示例](http://deeplearning.net/software/theano/library/tensor/basic.html#indexing)... –

+1

@NoobSailbot您是否使用了正确的版? –

回答

9

您正在使用太旧版本的Theano。实际上,tensor_var.nonzero()不在任何发布版本中。您需要更新到开发版本。

随着时代的发展版本,我有这样的:

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

这是因为你在你的行缺少括号。这里是好行:

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

但是我们仍然有意想不到的结果!问题是Theano不支持bool。在int8上做〜,是在8位而不是1位上按位反转。它给这个结果:

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

您可以删除〜这个:

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

好东西,谢谢。但我对你的意思是“开发版”有点困惑。这是我读过的“流血”吗?这不是应该是实验吗? –

+0

nonzero()是Theano 0.7发布的一部分 – sim

+0

@nouiz,我只想说,谢谢你的精彩答案+1。 –

相关问题