【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。

原文地址:https://www.cnblogs.com/yifanrensheng/p/13582798.html