label smoothing
2022/8/7 23:24:04
本文主要是介绍label smoothing,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
label 可分为 hard label 和 soft label;
label smoothing,标签平滑,像 L1 L2 dropout 一样,是一种正则化的方法;
用于 防止模型过分自信,提升泛化能力;
不过这个 方法不是 很常用,有很多 tricks 可以起到类似的作用,学到最后你会发现,很多算法之间彼此互通。
为什么需要 label smoothing
在分类 问题中,常用交叉熵 作为 loss
这是二分类 的专用公式,其实 交叉熵的本质是 log 损失,把 上式中的 y 变成 1 即可转化成 log 损失
其中 p 对应 y,q 对应 softmax,如下
传统one-hot编码标签的网络学习过程中,鼓励模型预测为目标类别的概率趋近1,非目标类别的概率趋近0,即最终预测的logits向量中目标类别z的值会趋于无穷大(如果不无穷大,就会有loss),使得模型向预测正确与错误标签的logit差值无限增大的方向学习,
而过大的logit差值会使模型缺乏适应性,对它的预测过于自信。
在训练数据不足以覆盖所有情况下,这就会导致网络过拟合,泛化能力差,而且实际上有些标注数据不一定准确,这时候使用交叉熵损失函数作为目标函数也不一定是最优的了。
label smoothing
合起来这么写
其中K为多分类的类别总个数,α是一个较小的超参数(一般取0.1);
通俗理解:将真实标签减去一个很小的数,然后平均分配到其他类上,实现了标签软化。
用法
tensorflow
tf.losses.softmax_cross_entropy( onehot_labels, logits, weights=1.0, label_smoothing=0, scope=None, loss_collection=tf.GraphKeys.LOSSES, reduction=Reduction.SUM_BY_NONZERO_WEIGHTS )
pytorch
from torch.nn.modules.loss import _WeightedLoss ## 继承_WeightedLoss类 class SmoothingBCELossWithLogits(_WeightedLoss): def __init__(self, weight=None, reduction='mean', smoothing=0.0): super(SmoothingBCELossWithLogits, self).__init__(weight=weight, reduction=reduction) self.smoothing = smoothing self.weight = weight self.reduction = reduction @staticmethod def _smooth(targets, n_labels, smoothing=0.0): assert 0 <= smoothing < 1 with torch.no_grad(): targets = targets * (1 - smoothing) + 0.5 * smoothing return targets def forward(self, inputs, targets): targets = _smooth(targets, inputs.size(-1), self.smoothing) loss = F.binary_cross_entropy_with_logits(inputs, targets, self.weights) if self.reduction == 'sum': loss = loss.item() elif self.reduction == 'mean': loss = loss.mean() return loss
参考资料:
https://www.cnblogs.com/irvingluo/p/13873699.html 标签平滑(Label Smoothing)详解
https://blog.csdn.net/qq_43211132/article/details/100510113 标签平滑Label Smoothing
https://blog.51cto.com/u_15279692/2943213
https://www.zhihu.com/question/65339831 神经网络中的label smooth为什么没有火?
这篇关于label smoothing的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?