Expected tensor for argument #1 'indices' to have scalar type Long;but got torch.IntTensor instead
程序员文章站
2022-03-04 13:28:51
...
日萌社
人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)
报错:RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long;
but got torch.IntTensor instead (while checking arguments for embedding)
分析:训练的批量样本数据输入值需要是long值的Tensor数据,而不是int值的Tensor数据.
解决:
1.把输入数据data和目标数据data的类型值都从int转换为long
1.可以加long()
source = Variable(data, requires_grad=False).long()
target = Variable(data, requires_grad=False).long()
2.也可以加torch.LongTensor
source = Variable(torch.LongTensor(data), requires_grad=False)
target = Variable(torch.LongTensor(data), requires_grad=False)
2.如果有使用以下这个第三方pyitcast包下的transformer_utils.py的话,可以如下修改
class SimpleLossCompute
def __call__(self, x, y, norm):
#return loss.data[0] * norm
#loss.data[0] 改成 loss.data.item()
return loss.data.item() * norm
3.tensor变量.item() 的用法
x = torch.randn(1)
print(x) #tensor([-0.4464])
print(x.item()) #-0.44643348455429077
推荐阅读
-
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CUDAType
-
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAFloatT
-
RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got torch.IntT
-
Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got torch.cuda.FloatTensor i
-
Expected tensor for argument #1 'indices' to have scalar type Long;but got torch.IntTensor instead