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
在使用PyTorch进行模型测试时,遇到RuntimeError,提示输出张量在CPU上,而模型参数张量也在CPU上,但期望它们都在GPU上。问题源于先将模型移动到GPU,然后加载在CPU上的预训练模型。解决方法是调整加载模型的顺序,确保模型移动到GPU后再加载权重。
395

被折叠的 条评论
为什么被折叠?



