Pytorch 类型错误:Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor.
生活随笔
收集整理的這篇文章主要介紹了
Pytorch 类型错误:Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor.
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor
Pytorcht調(diào)試過程中,將數(shù)據(jù)傳入模型,進(jìn)行計算。出現(xiàn)這個error,表明你的數(shù)據(jù)格式有問題。也許模型是GPU上的,參數(shù)是CPU類型。也許模型是CPU,參數(shù)是GPU類型。這是由于用了.cuda()進(jìn)行轉(zhuǎn)換。兩個方法可以解決。
1. 既然需要FloatTensor,就強(qiáng)制轉(zhuǎn)換你的模型和數(shù)據(jù)為cpu,就將GPU的model和input轉(zhuǎn)換為CPU。
device1=torch.device("cpu")model_ft = model_ft.to(device1)#將模型轉(zhuǎn)換為cpu版本。model_ft.train()inputs = inputs.to(device1)#將輸入數(shù)據(jù)轉(zhuǎn)換為CPU版本。labels = labels.to(device1)#將label轉(zhuǎn)換為CPU版本。output = model_ft(inputs)_,preds = torch.max(output,1)
2. 將模型和數(shù)據(jù)轉(zhuǎn)換成GPU版本,既然參數(shù)是cuda.FloatTensor,說明模型的參數(shù)是cuda類型的。強(qiáng)制轉(zhuǎn)換輸入和模型在GPU上就可以。
device1=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")model_ft = model_ft.to(device1)#將模型轉(zhuǎn)換為GPU版本。model_ft.train()inputs = inputs.to(device1)#將輸入數(shù)據(jù)轉(zhuǎn)換為GPU版本。labels = labels.to(device1)#將label轉(zhuǎn)換為GPU版本。output = model_ft(inputs)_,preds = torch.max(output,1)
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Pytorch 类型错误:Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钻戒一般多少钱啊?
- 下一篇: 单唾液酸四己糖神经节苷脂钠注射液多少钱一