TypeError: can‘t convert cuda:0 device type tensor to numpy. 解决记录

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

解决方案:

  将源代码中:
  distmat = distmat.numpy()
  改为:
  distmat = distmat.cpu().numpy()

成功运行,解决问题!

你可能感兴趣的:(python,bug解决记录,python,numpy)