IndexError: fail to coerce slice entry of type tensorvariable to integer

会有一股神秘感。 提交于 2019-12-10 13:28:53

问题


I run "ipython debugf.py" and it gave me error message as below

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"   

I search the forum and no luck, is there someone can help ? thanks!
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    

回答1:


Change train_set_x variable to theano.shared variable, and the code is OK. I dont know the reason, but it works! Hope this post can help others. The correct code is as below

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



回答2:


The reason this occurs is because index is a tensor symbolic variable (a long scalar, as you can see on line 4). So when python tries to build the dictionary that theano needs for its 'given' input, it tries to slice the numpy array using the symbolic variable – which it obviously can't do because it doesn't have a value yet (it is only set when you input something to the function).

As you've realised passing the data through theano.shared is the best approach. This means all the training data can be offloaded to the GPU, and then sliced/indexed on the fly to run each example.

However you might find that you have too much training data to fit in your GPU's memory, or for some other reason don't want to use a shared variable. Then you could just change your function definition

data = T.matrix()
fff=theano.function(inputs=[data],
    outputs=cost,
    givens={x: data}
)

Then instead of writing

fff(index)

You write

fff(train_set_x[index: index+1])

Be warned the process of moving data onto the GPU is slow, so it's much better to minimise the number of transfers if possible.



来源:https://stackoverflow.com/questions/39063169/indexerror-fail-to-coerce-slice-entry-of-type-tensorvariable-to-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!