UserWarning: reduction: ‘mean‘ divides the total loss by both the batch size and the support size.
1、错误提示损失函数计算损失时,出现 ``UserWarning`` :`` UserWarning: reduction: 'mean' divides the total loss by both the batch size and the support size.'batchmean' divides only by the batch size, and aligns with the
🤵 Author :Horizon John
✨ 编程技巧篇:各种操作小结
🎇 机器视觉篇:会变魔术 OpenCV
💥 深度学习篇:简单入门 PyTorch
🏆 神经网络篇:经典网络模型
💻 算法篇:再忙也别忘了 LeetCode
错误提示
损失函数计算损失时,出现 UserWarning :
UserWarning: reduction: 'mean' divides the total loss by both the batch size and the support size.'batchmean' divides only by the batch size, and aligns with the KL div math definition.'mean' will be changed to behave the same as 'batchmean' in the next major release. "reduction: 'mean' divides the total loss by both the batch size and the support size."

错误原因
出现这个错误一般选择的损失函数为:torch.nn.KLDivLoss()
错误翻译成中文为:
UserWarning: reduction: 'mean’将总损失除以批处理大小和支持大小。'batchmean’只除以批大小,并与KL div的数学定义一致。
在下一个主要版本中,'mean’的行为将与’batchmean’相同。reduction: 'mean’平均值’将总损失除以批大小和支持大小。
主要就是因为 reduction 参数的选择问题,默认时 mean,而其是在 batchmean 的基础上计算每个元素的loss值, batchmean 是计算每个 batch_size 的平均值 ;
解决方案
添加 reduction = batchmean
修改前:
criterion = torch.nn.KLDivLoss()
修改后:
criterion = torch.nn.KLDivLoss(reduction = batchmean)
🈺 喜欢的 留个 关注 、 加 点赞 哦 ~
更多推荐


所有评论(0)