torch报错TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor

错误代码:a.numpy()(企图使用tensor.numpy()函数将torch.Tensor类型转换成numpy类型)
报错:TypeError: can’t convert CUDA 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
a.numpy()改为a.cpu().numpy()即可

猜你喜欢

转载自blog.csdn.net/aaon22357/article/details/84946484