問題
出錯(cuò)代碼:
DEVICE = torch.device(‘gpu’ if torch.cuda.is_available() else ‘cpu’)
報(bào)錯(cuò)信息:
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
原理
無(wú)法識(shí)別“gpu”這個(gè)字符串,只能識(shí)別“cpu, cuda, xpu, mkldnn, opengl, opencl, ideep, hip, ve, ort, mlc, xla, lazy, vulkan, meta, hpu”這樣的字符串
解決
將“gpu”改為訓(xùn)練用的設(shè)備字符串,我用的是cuda,所以寫為
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
參考文章:https://blog.csdn.net/u014264373/article/details/87640753
衍生問題
出錯(cuò)代碼:
if torch.cuda.is_available():#是否可以gpu加速
correct += (predict.gpu() == labels.gpu()).sum()
else:
correct += (predict == labels).sum()
報(bào)錯(cuò)信息:
AttributeError: ‘Tensor’ object has no attribute ‘gpu’
原理
device選擇的加速設(shè)備名稱是Tensor的合法屬性。由于我使用的設(shè)備是cuda,則gpu應(yīng)該改為cuda。
解決
將“gpu”改為訓(xùn)練用的設(shè)備字符串。根據(jù)使用的設(shè)備名確定修改的屬性名稱。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-641068.html
if torch.cuda.is_available():#是否可以gpu加速
correct += (predict.cuda() == labels.cuda()).sum()
else:
correct += (predict == labels).sum()文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-641068.html
到了這里,關(guān)于Expected one of xxx device type 解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!