【E-13】TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

定位:print(np.array(str_reparametrize).shape)

二、原因

numpy不能读取CUDA tensor 需要将它转化为 CPU tensor。

   

三、解决方案

转换成CPU tenor后即可

本文采用

print(str_reparametrize.cuda().data.cpu().numpy())

   

四、建议

Pytorch代码运行在cpu中,原来的写是对的。

用GPU中代码运行,因为numpy在cuda中没有这种表达,需要将cuda中的数据转换到cpu中,再去使用numpy。

相关文章:

  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2022-02-24
  • 2022-12-23
  • 2021-08-07
  • 2021-04-14
猜你喜欢
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2021-03-28
  • 2022-02-09
  • 2021-09-08
  • 2022-12-23
相关资源
相似解决方案