1. CPU tensor转GPU tensor:

cpu_imgs.cuda()
2. GPU tensor 转CPU tensor:

gpu_imgs.cpu()
3. numpy转为CPU tensor:

torch.from_numpy( imgs )
4.CPU tensor转为numpy数据:

cpu_imgs.numpy()
5. note:GPU tensor不能直接转为numpy数组,必须先转到CPU tensor。

6. 如果tensor是标量的话,可以直接使用 item() 函数(只能是标量)将值取出来:

print loss_output.item()

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-11-19
  • 2022-12-23
  • 2021-08-05
猜你喜欢
  • 2021-12-27
  • 2023-01-28
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
相关资源
相似解决方案