Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Theano:为什么在这种情况下索引失败?_Python_Numpy_Theano - Fatal编程技术网

Python Theano:为什么在这种情况下索引失败?

Python Theano:为什么在这种情况下索引失败?,python,numpy,theano,Python,Numpy,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

我试图得到给定布尔值的向量的最大值

对于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'
>that=T.arange(10,dtype='int32')
>>>that[~(that>=5)].max().eval()
9
>>>that[~(that>=5).nonzero()].max().eval()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
that[~(that>=5).nonzero()].max().eval()
AttributeError:“TensorVariable”对象没有属性“非零”

为什么会发生这种情况?这是我遗漏的细微差别吗?

您使用的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'
但我们仍然有意想不到的结果!问题是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)

>>>那[(好吧,第二个数组的文本回溯是说数组没有
非零()
method/attribute,所以你不能像使用numpy数组那样使用它。@JeffTratner:这与网站上提供的相反…@NoobSailbot你使用的版本正确吗?@JeffTratner:
theano.version.version
给了我
'0.6.0rc3'
非零()
该版本不支持?很好,谢谢。但我对你所说的“开发版本”有点困惑。这就是我读到的“前沿”吗?这不是实验性的吗?@nouiz,我只想说,非常感谢你的精彩回答+1。
>>> that[(that<5).nonzero()].max().eval()
array(4, dtype=int32)