博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决“TypeError: can‘t convert cuda:0 device type tensor to numpy. ......”问题
阅读量:4128 次
发布时间:2019-05-25

本文共 997 字,大约阅读时间需要 3 分钟。

问题描述

在训练Pytorch模型的时候,报错

Traceback (most recent call last):  File "train.py", line 469, in 
train(hyp, tb_writer, opt, device) File "train.py", line 347, in train save_dir=log_dir) File "/home/xxx/Detection/test.py", line 176, in test plot_images(img, output_to_target(output, width, height), paths, str(f), names) # predictions File "/home/xxx/Detection/utils/utils.py", line 914, in output_to_target return np.array(targets) File "/home/xxx/anaconda3/envs/pytorch1.5/lib/python3.7/site-packages/torch/tensor.py", line 492, in __array__ return self.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

解决方法

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

补充说明

之前报错的代码是在101服务器上,创建的虚拟环境中Python=3.7,在跑实验的时候出现报错;今天在100服务器上跑同样的实验没有报错,经查在100服务器上创建的虚拟环境中Python=3.8;目测是Python版本的缘故,看来还是python3.8好用一些

转载地址:http://bgrpi.baihongyu.com/

你可能感兴趣的文章
微软(北京).NET俱乐部6月26日活动公告
查看>>
深入了解JavaScript对浏览器的支持
查看>>
Web 2.0
查看>>
font属性的设置
查看>>
微软(北京).NET俱乐部7月18日(星期日)活动通知
查看>>
腾讯,到了该创新的时候了
查看>>
解决img标签空隙问题
查看>>
学习Javascript闭包(Closure)
查看>>
我所钟爱的 HTML5 资源
查看>>
CSS优先级
查看>>
CSS Specificity--CSS特性、权重、优先级---CSS specificity规则、算法及实例分析
查看>>
JavaScript 将颜色值10进制转换成16进制
查看>>
Google的YSlow——Page Speed(附插件下载)
查看>>
Google的YSlow——Page Speed(附插件下载)
查看>>
jQuery实现首页图片淡入淡出效果
查看>>
注册表REG文件编写实例(创建、删除、添加、更改键值)
查看>>
推荐前端开发工具-Aptana
查看>>
Aptana 添加jQuery智能提示
查看>>
更改Aptana 2默认的编码格式
查看>>
Zen Coding – 超快地写网页代码
查看>>