IndexError:无法将张量变量类型的切片条目强制转换为整数

fan*_*ack 6 python theano

我运行"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"   
Run Code Online (Sandbox Code Playgroud)

我搜索论坛没有运气,有人可以帮忙吗?谢谢!
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    
Run Code Online (Sandbox Code Playgroud)

fan*_*ack 2

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

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
Run Code Online (Sandbox Code Playgroud)