标签平滑和样本类别不均衡处理方式

本文介绍了两种用于提高模型泛化能力的损失函数:Label Smoothing和Focal Loss。Label Smoothing通过平滑标签分布减少过拟合,Focal Loss则针对类别不平衡问题,通过调整难易样本权重降低简单样本的影响。提供了PyTorch实现的代码示例。

1. 论文

1.1 Label Smoothing

  1. https://arxiv.org/abs/1906.02629 When Does Label Smoothing Help?
  2. https://arxiv.org/abs/1701.06548 Regularizing Neural Networks by Penalizing Confident Output Distributions
  3. https://arxiv.org/abs/1706.04599 On Calibration of Modern Neural Networks

1.2 Focal Loss

https://arxiv.org/abs/1708.02002 Focal Loss for Dense Object Detection
标签平滑: 提高模型的泛化能力,对于未知域任务,分类任务,可以提高精度。

2. 代码实现

2.1 Label Smoothing loss function

class LabelSmoothingCrossEntropy(nn.Module):
    def __init__(self, eps=0.1, reduction='mean'):
        super(LabelSmoothingCrossEntropy, self).__init__()
        self.eps = eps
        self.reduction = reduction

    def forward(self, output, target):
        c = output.size()[-1]
        log_preds = F.log_softmax(output, dim=-1)
        if self.reduction=='sum':
            loss = -log_preds.sum()
        else:
            loss = -log_preds.sum(dim=-1)
            if self.reduction=='mean':
                loss = loss.mean()
        return loss*self.eps/c + (1-self.eps) * F.nll_loss(log_preds, target, reduction=self.reduction)

2.2 labels mooth+类别不均衡loss

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值