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.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值