RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0
基本上问题就是出在该报错前一条代码里。里面的变量一个在cpu,一个在cuda,可以使用print(变量.device)运行查看
例如 报错内容为 return x+y
在报错内容前加上
print('x in device:', x.device)
print('y in device:', y.device)
输出结果为:
x in device:cpu
y in device:cuda:0
则说明x没有跑在gpu上
给x加上
device = torch.device("cuda:0")
x = x.to(device) #指定设备