RuntimeError: The size of tensor a (128) must match the size of tensor b (16) at non-singleton dimen

RuntimeError: The size of tensor a (128) must match the size of tensor b (16) at non-singleton dimension 0

这个错误出现的原因有好多种!!
其中一个比较可能的原因是因为:封装dataloader的时候,最后剩下的不足一个batchsize! 自带的dataloader就会有这个现象。

改正:

batch_size_s = len(targets) #不足一个batch_size直接停止训练
if batch_size_s < BATCH_SIZE:
    break

就把最后一个不足batchsize的丢掉!!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
这个报错的原因是由于在计算过程中,两个张量的尺寸不匹配导致的。根据引用的描述,这个问题可能是在读取图片时出现的。要解决这个问题,可以尝试将图片转换为RGB格式。另外,根据引用的建议,在DataLoader函数中添加参数"drop_last=False"也可能解决这个问题。具体地,你可以像下面这样修改代码: dataloader = DataLoader(ImageDataset(opt.dataroot, transforms_=transforms_, unaligned=True), batch_size=opt.batchSize, shuffle=True, num_workers=opt.n_cpu, drop_last=False) 通过保持batch的尺寸不变来避免尺寸不匹配的问题。希望这个解决方案能帮到你!<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio](https://blog.csdn.net/weixin_46135327/article/details/130805823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [RuntimeError: The size of tensor a (8) must match the size of tensor b (7) at non-singleton dimensio](https://blog.csdn.net/m0_46368408/article/details/131231563)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值