解決pytorch 損失函數中輸入輸出不匹配的問題

一、pytorch 損失函數中輸入輸出不匹配問題

File “C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\modules\module.py”, line 491, in __call__  result = self.forward(*input, **kwargs)

File “C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\modules\loss.py”, line 500, in forward reduce=self.reduce)
 
File “C:\Users\Rain\AppData\Local\Programs\Python\Anaconda.3.5.1\envs\python35\python35\lib\site-packages\torch\nn\functional.py”, line 1514, in binary_cross_entropy_with_logits
 
raise ValueError(“Target size ({}) must be the same as input size ({})”.format(target.size(), input.size()))
 
ValueError: Target size (torch.Size([32])) must be the same as input size (torch.Size([32,2]))

原因

input 和 target 尺寸不匹配

解決方案:

將target轉為onehot

例如:

one_hot = torch.nn.functional.one_hot(masks, num_classes=args.num_classes)

二、Pytorch遇到權重不匹配的問題

最近,樓主在pytorch微調模型時遇到

size mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, the shape in current model is torch.Size([2, 2048]).

size mismatch for fc.bias: copying a param with shape torch.Size([1000]) from checkpoint, the shape in current model is torch.Size([2]).

這個是因為樓主下載的預訓練模型中的全連接層是1000類別的,而樓主本人的類別隻有2類,所以會報不匹配的錯誤

解決方案:

從報錯信息可以看出,是fc層的權重參數不匹配,那我們隻要不load 這一層的參數就可以瞭。

net = se_resnet50(num_classes=2)
pretrained_dict = torch.load("./senet/seresnet50-60a8950a85b2b.pkl")
model_dict = net.state_dict()
# 重新制作預訓練的權重,主要是減去參數不匹配的層,樓主這邊層名為“fc”
pretrained_dict = {k: v for k, v in pretrained_dict.items() if (k in model_dict and 'fc' not in k)}
# 更新權重
model_dict.update(pretrained_dict)
net.load_state_dict(model_dict)

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: