Tensor for ‘out‘ is on CPU, Tensor for argument #1 ‘self‘ is on CPU

x33g5p2x  于2022-03-22 转载在 其他  
字(0.6k)|赞(0)|评价(0)|浏览(540)

1、问题
模型训练完后进行测试,报错

RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)

2、原因
将模型送入GPU之后才加载之前训练好的模型,导致模型加载到了GPU,然而你的网络权重还在CPU中,此时会报错如下,报错部分代码:

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
net.load_state_dict(torch.load('./results/bestmodel30.pth'),False)

3、解决
换下位置即可

net.load_state_dict(torch.load('./results/bestmodel30.pth'),False)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)

原文链接:https://blog.csdn.net/weixin_43760844/article/details/116047427

相关文章

微信公众号

最新文章

更多