Pytorch_常见的使用错误
每一小问题中上部分为错误代码,下部分为正确代码。
1.数据和模型不同设备错误
import torch
# different device error
model = torch.nn.Linear(5,1).to("cuda:0")
x = torch.Tensor([1,2,3,4,5]).to("cpu") # 如果模型加载了GPU上 那数据也要加载到GPU上 否则会报错
y = model(x)
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)
# 1. different device error (fixed)
x = torch.Tensor([1,2,3,