解决【TypeError:can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy 】

项目场景:

训练pytorch模型时报错


问题描述

TypeError:can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy...


原因分析:

如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式。 numpy不能读取CUDA tensor 需要将它转化为 CPU tensor


解决方案:

提示:将报错代码self.numpy()改为self.cpu().numpy()即可

 AAE_x_hat = AAE_x_hat.detach().cpu().numpy().squeeze()
	

参考链接

提示:https://blog.csdn.net/SpadgerZ/article/details/115584829

你可能感兴趣的:(pytorch,实验,pytorch,深度学习,python)