TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu()
文章目录1、错误提示在将 ``tensor`` 格式转换成 ``numpy`` 格式时出现报错:``TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.``
·
🤵 Author :Horizon Max
✨ 编程技巧篇:各种操作小结
🎇 机器视觉篇:会变魔术 OpenCV
💥 深度学习篇:简单入门 PyTorch
💻 算法篇:再忙也别忘了 LeetCode
错误提示
在将 tensor
格式转换成 numpy
格式时出现报错:
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
错误原因
需要先将 tensor 转换到 CPU ,因为 Numpy 是 CPU-only
解决方案
使用 .cpu()
先进行转换
修改前:
predict.numpy()
修改后:
predict.cpu().numpy()
🈺 喜欢的 留个 关注
、 加 点赞
哦 ~
更多推荐
已为社区贡献4条内容
所有评论(0)