pytorch resnet训练中出现的错误总结

编程入门 行业动态 更新时间:2024-10-24 04:44:38

pytorch resnet训练中出现的<a href=https://www.elefans.com/category/jswz/34/1771449.html style=错误总结"/>

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训练中出现的错误总结

本文发布于:2023-07-27 22:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   pytorch   resnet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!