概述
随着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:为什么索引会在这种情况下失败?所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧