首页 > 其他分享 >RuntimeError: Input, output and indices must be on the current device 问题

RuntimeError: Input, output and indices must be on the current device 问题

时间:2023-02-13 11:23:13浏览次数:63  
标签:RuntimeError torch current indices device Input

RuntimeError: Input, output and indices must be on the current device

做项目时遇到这个问题

image

明明就已经加了这条语句,使其运行在我设置好的设备上了。

为何会报错呢?

解决方法

将运行的设备挂到cpu上查看具体的错误信息

image

在cpu上运行能够查看更多的错误信息。
重新运行后,得到下面的错误信息:

image

这个错误很具体,这就很好解决了,去看看自己的embedding size。

再回看这个错误语句:

RuntimeError: Input, output and indices must be on the current device

说明有些indices没有挂到cuda上(可能使超出了范围),也在暗示你的embedding环节出现了问题。

总结

使用pytorch的时候,习惯使用语句:

if torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('cpu')

使其选择可以使用的设备

一般来说实验电脑都会配有cuda(刚开始学机器学习的时候,大家应该都装了)

但是有时候它的报错信息十分有限,这时候建议将设备设置回使用CPU,查看更多的报错信息。

device = torch.device('cpu')

标签:RuntimeError,torch,current,indices,device,Input
From: https://www.cnblogs.com/jev-0987/p/17115672.html

相关文章