IndexError: fail to coerce slice entry of type tensorvariable to integer
IndexError: fail to coerce slice entry of type tensorvariable to integer
我 运行 "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变量,代码就OK了。
我不知道原因,但它有效!希望这个 post 可以帮助到其他人。
正确代码如下
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
出现这种情况的原因是因为 index 是一个张量符号变量(一个长标量,如您在第 4 行中所见)。因此,当 python 尝试为其 'given' 输入构建 theano 所需的字典时,它会尝试使用符号变量对 numpy 数组进行切片——这显然无法做到,因为它没有值(仅当您向函数输入内容时才设置)。
如您所知,通过 theano.shared 传递数据是最好的方法。这意味着所有训练数据都可以卸载到 GPU,然后 sliced/indexed 动态地 运行 每个示例。
但是您可能会发现您的训练数据太多而无法放入 GPU 的内存中,或者出于某些其他原因不想使用共享变量。然后你可以改变你的函数定义
data = T.matrix()
fff=theano.function(inputs=[data],
outputs=cost,
givens={x: data}
)
那就不用写
fff(index)
你写
fff(train_set_x[index: index+1])
请注意,将数据移动到 GPU 的过程很慢,因此最好尽可能减少传输次数。
我 运行 "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变量,代码就OK了。 我不知道原因,但它有效!希望这个 post 可以帮助到其他人。 正确代码如下
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
出现这种情况的原因是因为 index 是一个张量符号变量(一个长标量,如您在第 4 行中所见)。因此,当 python 尝试为其 'given' 输入构建 theano 所需的字典时,它会尝试使用符号变量对 numpy 数组进行切片——这显然无法做到,因为它没有值(仅当您向函数输入内容时才设置)。
如您所知,通过 theano.shared 传递数据是最好的方法。这意味着所有训练数据都可以卸载到 GPU,然后 sliced/indexed 动态地 运行 每个示例。
但是您可能会发现您的训练数据太多而无法放入 GPU 的内存中,或者出于某些其他原因不想使用共享变量。然后你可以改变你的函数定义
data = T.matrix()
fff=theano.function(inputs=[data],
outputs=cost,
givens={x: data}
)
那就不用写
fff(index)
你写
fff(train_set_x[index: index+1])
请注意,将数据移动到 GPU 的过程很慢,因此最好尽可能减少传输次数。