您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:23:32 python 字数 1895 阅读 601 来源 www.jb51.cc/python

我试图获得给定布尔值的向量的最大值. 随着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()].m

概述

随着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[~(that>=5).nonzero()].max().eval()
Traceback (most recent call last):
  File "<stdin>",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,1,1],dtype=int8)
>>> (~(that>=5)).eval()
array([-1,-1,-2,-2],dtype=int8)

您可以删除?:

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

总结

以上是编程之家为你收集整理的python – Theano:为什么索引会在这种情况下失败?全部内容,希望文章能够帮你解决python – Theano:为什么索引会在这种情况下失败?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶