解決ToPILImage時(shí)出現(xiàn)維度報(bào)錯問題pic should be 2/3 dimensional. Got 4 dimensions.
ToPILImage時(shí)出現(xiàn)維度報(bào)錯pic should be 2/3 dimensional. Got 4 dimensions.
主要原因是在數(shù)據(jù)集加載過程中加入了batch_size,將tensor變?yōu)榱怂木S。
print(img.shape) img1 = img[0] print(img1.shape)
直接將其轉(zhuǎn)為三維即可

PyTorch中常見報(bào)錯
本部分介紹一些PyTorch中常見的報(bào)錯信息及其解決方法
報(bào)錯1:
ValueError: num_samples should be a positive integer value, but got num_samples=0
可能的原因:傳入的Dataset中的len(self.data_info)==0,即傳入該dataloader的dataset里沒有數(shù)據(jù)
解決方法:
- 檢查dataset中的路徑,路徑不對,讀取不到數(shù)據(jù)。
- 檢查Dataset的__len__()函數(shù)為何輸出為零
報(bào)錯2:
TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>
可能的原因:當(dāng)前操作需要PIL Image或ndarray數(shù)據(jù)類型,但傳入了Tensor
解決
- 檢查transform中是否存在兩次ToTensor()方法
- 檢查transform中每一個操作的數(shù)據(jù)類型變化
報(bào)錯3:
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 93 and 89 in dimension 1 at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensorMath.cpp:3616
可能的原因:dataloader的__getitem__函數(shù)中,返回的圖片形狀不一致,導(dǎo)致無法stack
解決方法:檢查__getitem__函數(shù)中的操作
報(bào)錯4:
# 通道數(shù)不匹配
conv: RuntimeError: Given groups=1, weight of size 6 1 5 5, expected input[16, 3, 32, 32] to have 1 channels, but got 3 channels instead
# 維度不匹配
linear: RuntimeError: size mismatch, m1: [16 x 576], m2: [400 x 120] at ../aten/src/TH/generic/THTensorMath.cpp:752
可能的原因:網(wǎng)絡(luò)層輸入數(shù)據(jù)與網(wǎng)絡(luò)的參數(shù)不匹配
解決方法:
- 檢查對應(yīng)網(wǎng)絡(luò)層前后定義是否有誤
- 檢查輸入數(shù)據(jù)shape
報(bào)錯5:
AttributeError: 'DataParallel' object has no attribute 'linear'
可能的原因:并行運(yùn)算時(shí),模型被dataparallel包裝,所有module都增加一個屬性 module. 因此需要通過 net.module.linear調(diào)用
解決方法:
網(wǎng)絡(luò)層前加入module.
報(bào)錯6:
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
可能的原因:gpu訓(xùn)練的模型保存后,在無gpu設(shè)備上無法直接加載
解決方法:
需要設(shè)置map_location=“cpu”
報(bào)錯7:
AttributeError: Can't get attribute 'FooNet2' on <module '__main__' from '
可能的原因:保存的網(wǎng)絡(luò)模型在當(dāng)前python腳本中沒有定義
解決方法:
提前定義該類
報(bào)錯8:
這個錯誤經(jīng)常在交叉熵?fù)p失函數(shù)中碰到
RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at ../aten/src/THNN/generic/ClassNLLCriterion.c:94
可能的原因:
標(biāo)簽數(shù)大于等于類別數(shù)量,即不滿足 cur_target < n_classes,通常是因?yàn)闃?biāo)簽從1開始而不是從0開始
解決方法:
修改label,從0開始,例如:10分類的標(biāo)簽取值應(yīng)該是0-9
報(bào)錯9:
RuntimeError: expected device cuda:0 and dtype Long but got device cpu and dtype Long
可能的原因:需計(jì)算的兩個數(shù)據(jù)不在同一個設(shè)備上
解決方法:
采用to函數(shù)將數(shù)據(jù)遷移到同一個設(shè)備上
報(bào)錯10:
Expected object of backend CPU but got backend CUDA for argument #2 'weight'
可能的原因:張量的to函數(shù)非原地操作,轉(zhuǎn)換數(shù)據(jù)設(shè)備未賦值,即inputs.to("CUDA"),此時(shí),數(shù)據(jù)仍然在CPU上,未轉(zhuǎn)換成功
解決辦法:
inputs = inputs.to(device)
報(bào)錯11:
RuntimeError: DataLoader worker (pid 27) is killed by signal: Killed. Details are lost
due to multiprocessing. Rerunning with num_workers=0 may give better error trace.
可能原因:內(nèi)存不夠(不是gpu顯存,是內(nèi)存)
解決方法:申請更大內(nèi)存
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django?celery的替代品Dramatiq使用特性詳解
這篇文章主要為大家介紹了Django?celery的替代品Dramatiq使用特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
python之線程池map()方法傳遞多參數(shù)list
這篇文章主要介紹了python之線程池map()方法傳遞多參數(shù)list問題,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
解讀requests.session()獲取Cookies全過程
這篇文章主要介紹了解讀requests.session()獲取Cookies全過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Python實(shí)現(xiàn)格式化輸出的實(shí)例詳解
這篇文章主要為大家介紹了Python語法中實(shí)現(xiàn)格式化輸出的方法,本文通過幾個實(shí)例為大家進(jìn)行了詳細(xì)的講解,感興趣的小伙伴可以了解一下2022-08-08
Python自定義實(shí)現(xiàn)GUI時(shí)鐘的示例代碼
GUI時(shí)鐘是一個基于Python tkinter庫開發(fā)的圖形界面時(shí)鐘應(yīng)用程序,具有簡潔美觀的界面和實(shí)用的功能,下面就跟隨小編一起來看看如何使用Python實(shí)現(xiàn)這一需求吧2026-01-01
python用WxPython庫實(shí)現(xiàn)無邊框窗體和透明窗體實(shí)現(xiàn)方法詳解
這篇文章主要介紹了python用WxPython庫實(shí)現(xiàn)無邊框窗體和透明窗體實(shí)現(xiàn)方法詳解,需要的朋友可以參考下2020-02-02

