Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two

Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two devices

  • 一、报错信息
  • 二、报错原因
  • 三、解决方法
  • 四、原代码
  • 五、现代码
  • 参考博客

一、报错信息

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument weight in method wrapper_nll_loss_forward)
Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two_第1张图片

二、报错原因

tensors 一会在CPU运算,一会在GPU运算。

三、解决方法

都改成在GPU运算。

四、原代码

Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two_第2张图片
Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two_第3张图片

五、现代码

Pytorch——报错解决:RuntimeError: Expected all tensors to be on the same device, but found at least two_第4张图片

参考博客

  1. 【深度学习】RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

你可能感兴趣的:(PyTorch,pytorch,tensors)