Noo*_*bot 6 python numpy theano
我试图获得给定布尔值的向量的最大值.
随着Numpy:
>>> this = np.arange(10)
>>> this[~(this>=5)].max()
4
Run Code Online (Sandbox Code Playgroud)
但是与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'
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这是一个我错过的微妙的细微差别吗?
您使用的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'
Run Code Online (Sandbox Code Playgroud)
这是因为您的行中缺少括号.这是好的路线:
>>> that[(~(that>=5)).nonzero()].max().eval()
array(9, dtype=int32)
Run Code Online (Sandbox Code Playgroud)
但是我们仍有意想不到的结果!问题是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)
Run Code Online (Sandbox Code Playgroud)
您可以删除〜:
>>> that[(that<5).nonzero()].max().eval()
array(4, dtype=int32)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1050 次 |
| 最近记录: |