我运行“ipython debugf.py”,它给了我以下错误消息

IndexError                                Traceback (most recent call last)
/home/ml/debugf.py in <module>()
      8 fff = theano.function(inputs=[index],
      9                         outputs=cost,
---> 10                         givens={x: train_set_x[index: index+1]})

IndexError: failed to coerce slice entry of type TensorVariable to integer"

我在论坛上搜索,没有运气,有人可以帮忙吗?
谢谢!
debugf.py:
import theano.tensor as T
import theano
import numpy
index =T.lscalar()
x=T.dmatrix()
cost=x +index
train_set_x=numpy.arange(100).reshape([20,5])
fff=theano.function(inputs=[index],
     outputs=cost,
     givens={x:train_set_x[index: index+1]})   #<--- Error here

最佳答案

将train_set_x变量更改为theano.shared变量,代码即可。
我不知道原因,但是有效!希望这篇文章可以帮助其他人。
正确的代码如下

import theano.tensor as T
import theano
import numpy
index =T.lscalar()
x=T.dmatrix()
cost=x +index
train_set_x=numpy.arange(100.).reshape([20,5]) #<--- change to float,
                                   #because shared must be floatX type

#change to shared variable
shared_x = theano.shared(train_set_x)

fff=theano.function(inputs=[index],
     outputs=cost,
     givens={x:shared_x[index: index+1]})  #<----change to shared_x

关于python - IndexError : fail to coerce slice entry of type tensorvariable to integer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39063169/

10-12 19:39