错误总结"/>
pytorch resnet训练中出现的错误总结
以下是我在训练resnet时遇到的一些错误,记录下来!
错误1:pytorch训练报错TypeError: batch must contain tensors, numbers, dicts or lists found class PIL.Image.Image
原因:送入dataloader的img是用PIL.Image直接读取的,没有转换成tensor格式
解决方案:用torchvision.transforms.ToTensor()转化为tensor格式。
错误2:RuntimeError: The size of tensor a (58) must match the size of tensor b (56) at non-singleton dimension 3
原因:这与resnet网络的结构有关系,因为resnet的残差单元存在短路连接,所以要求输入的x和经过残差单元的输出y要具有一样的纬度,但是我在训练时输入的是58x58,但经过卷积等操作后,变成了56x56,所以没办法将他们cat在一起,所以会报错。
解决方案:修改了残差模块里的网络结构使x与feature map的大小相同。
错误3: File"D:\anaconda\anaconda\lib\sitepackages\torch\nn\functional.py", line 2228, in nll_lossout_size, target.size()))
ValueError: Expected target size (1, 1), got torch.Size([1, 10])
原因:因为我训练的类别有10类,所以我在构造label时,以为得用一个one-hot编码后的1x10维tensor作为label去计算损失函数
解决方案:实际上,nn.CrossEntropyLoss()损失函数只需要对应类别的索引即可,所以我们不用再进行one-hot编码。
错误4:“log_softmax” not implemented for 'Long’
原因:在计算交叉熵损失函数时, 需要long型的label
解决方案:按上图直接转换为long型的label。
错误5: 1D target tensor expected, multi-target not supported
同错误3.
更多推荐
pytorch resnet训练中出现的错误总结
发布评论