出现错误indices‘ to have one of the following scalar types: Long, Int; but got torch
·
indices' to have one of the following scalar types: Long, Int; but got torch.FloatTensor instead (while checking arguments for embedding)
解决方法
看起来还是有一些混淆。在PyTorch中,.int()和.long()方法是用来将张量转换为整数类型的,但是它们的使用方式需要注意。
如果你想将一个张量转换为整数类型(int32或int64),你应该这样写:
python
input = input.long() # 或者 input = input.int() return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
具体来说:
.long()方法将张量转换为int64类型,也称为Long类型。.int()方法将张量转换为int32类型,也称为Int类型。
你的错误发生的原因是,torch.embedding函数要求indices参数是整数类型的张量,而你传递了一个torch.FloatTensor类型的张量。通过使用.long()或.int()方法,你可以将input张量正确地转换为所需的整数类型,从而解决这个问题。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)