torch.Size报错:ValueError: Using a target size (torch.Size([128])) that is different to the input

这里写目录标题

问题

/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1193 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1194 return forward_call(*input, **kwargs)
1195 # Do not call functions when jit is used
1196 full_backward_hooks, non_full_backward_hooks = [], []

/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
617

-> 3088 “Please ensure they have the same size.”.format(target.size(), input.size())
3089 )
3090

ValueError: Using a target size (torch.Size([128])) that is different to the input size (torch.Size([256])) is deprecated. Please ensure they have the same size.
出现torch.Size不匹配的问题,并且这个不匹配还是整数倍,比如在我的例子中,target.size()是128,但input.size()是256.

分析

由于我是调用别人写好的库,不应该有这个问题,并且在github上也没有没有找到一样的问题,最后我看了下源码,这个库支持多GPU训练,我使用了2个GPU,于是我修改了源码改成单个GPU训练,问题解决。

解决办法

训练时制定单个GPU即可解决问题

os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值