加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 23:45:37 所属栏目: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()].max().eval()Traceback (most recent call la
我试图获得给定布尔值的向量的最大值.

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

为什么会这样?这是一个我错过的微妙的细微差别吗?

解决方法

您使用的Theano版本太旧了.实际上,tensor_var.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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读