为什么深度学习模型的预测结果为数据集均值--pytorch loss里input和target的维度必须相同
因此需要把pred的1那维给去掉,用squeeze或者reshape都行。如图,上边的为真值,下边的为预测值,可见预测值都是在30左右。一直debug不出来。后来发现是loss处写错了。
·
如图,上边的为真值,下边的为预测值,可见预测值都是在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也是错的
更多推荐
已为社区贡献1条内容
所有评论(0)