报错描述

在使用pytorch框架加载下载好的本地bert预训练模型,对输入的序列分词并转换成对应的id,然后要生成tensor时,使用torch.Tensor()函数,会出现报错:

RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar

解决办法

方法一:

在Tensor()函数后面加.long()转换成long类型,就不会再报错了。例如,

token_ids=torch.Tensor(token_ids).unsqueeze(0).long()

方法二:

直接换一个函数生成tensor,就是不再使用Tensor()函数了,而是使用torch.tensor()函数,也就不会报错了。例如,

input_ids=torch.tensor([input_ids])

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐