为什么深度学习模型的预测结果为数据集均值--pytorch loss里input和target的维度必须相同

如图,上边的为真值,下边的为预测值,可见预测值都是在30左右

一直debug不出来。后来发现是loss处写错了

用的torch.nn.MSELoss(pred,target),其中pred的size是(batchsize,1), target的size是(batchsize),此时会有警告但不报错:

media/cfs/user/.pylib/lib/python3.6/site-packages/torch/nn/modules/loss.py:445: UserWarning: Using a target size (torch.Size([2560])) that is different to the input size (torch.Size([2560, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)
/media/cfs/user/.pylib/lib/python3.6/site-packages/torch/nn/modules/loss.py:445: UserWarning: Using a target size (torch.Size([360])) that is different to the input size (torch.Size([360, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)

因此需要把pred的1那维给去掉,用squeeze或者reshape都行

我们做个简单的实验。下图中的3.1375才是正确的结果(因为MSELoss是对所有样本的mse求均值)。而对target进行squeeze后,input和target的size不再相同,所得结果2.450也是错的

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值