CHH3213 / Note-Ubuntu_CHH3213

ubuntu系统CHH3213上做的笔记
0 stars 0 forks source link

39. 解决“TypeError: can‘t convert cuda:0 device type tensor to numpy. ......”问题 #39

Open CHH3213 opened 3 years ago

CHH3213 commented 3 years ago

pytorch报错:TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. 解决方案: 如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式。 numpy不能读取CUDA tensor 需要将它转化为 CPU tensor

将报错代码train_y.numpy()改为train_y.cpu().numpy()即可