IndexError:无法将张量变量类型的切片条目强制转换为整数 [英] IndexError: fail to coerce slice entry of type tensorvariable to integer

查看:33
本文介绍了IndexError:无法将张量变量类型的切片条目强制转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行ipython debugf.py",它给了我如下错误信息

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"   

我搜索了论坛但没有运气,有人可以帮忙吗?谢谢!
debugf.py :

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    

推荐答案

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

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

这篇关于IndexError:无法将张量变量类型的切片条目强制转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆