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

2022年12月23日11:26:28

错误代码: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()即可

  • 作者:aaon22357
  • 原文链接:https://blog.csdn.net/aaon22357/article/details/84946484
    更新时间:2022年12月23日11:26:28 ,共 283 字。