Expected one of xxx device type 解决方法

问题

出错代码:

DEVICE = torch.device(‘gpu’ if torch.cuda.is_available() else ‘cpu’)

报错信息:

RuntimeError: Expected one of cpu, cuda, xpu, mkldnn, opengl, opencl,
ideep, hip, ve, ort, mlc, xla, lazy, vulkan, meta, hpu device type at
start of device string: gpu

原理

无法识别“gpu”这个字符串,只能识别“cpu, cuda, xpu, mkldnn, opengl, opencl, ideep, hip, ve, ort, mlc, xla, lazy, vulkan, meta, hpu”这样的字符串

解决

将“gpu”改为训练用的设备字符串,我用的是cuda,所以写为

device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)

参考文章:https://blog.csdn.net/u014264373/article/details/87640753

衍生问题

出错代码:

if torch.cuda.is_available():#是否可以gpu加速
correct += (predict.gpu() == labels.gpu()).sum()
else:
correct += (predict == labels).sum()

报错信息:

AttributeError: ‘Tensor’ object has no attribute ‘gpu’

原理

device选择的加速设备名称是Tensor的合法属性。由于我使用的设备是cuda,则gpu应该改为cuda。

解决

将“gpu”改为训练用的设备字符串。根据使用的设备名确定修改的属性名称。

if torch.cuda.is_available():#是否可以gpu加速
correct += (predict.cuda() == labels.cuda()).sum()
else:
correct += (predict == labels).sum()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值