一.問題描述
在用GPU訓練模型時報如下的錯誤:
TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
二.原因分析:
GPU上的tensor張量無法轉為numpy格式,那我們把它轉到CPU上即可。文章來源:http://www.zghlxwxcb.cn/news/detail-661192.html
三.解決方案:
方法非常簡單,只需在目標張量后面加 .cpu() 即可。文章來源地址http://www.zghlxwxcb.cn/news/detail-661192.html
Before:
loss = valid_loss_function(logits,labels.to(device))
After:
loss = valid_loss_function(logits,labels.to(device)).cpu()
到了這里,關于TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!