从FocalLoss到hardsample

本文探讨了Focal Loss在处理深度学习正负样本不平衡问题中的应用,以及Hard Sample在SVM、Adaboost、神经网络中的角色。Focal Loss通过降低易分类样本的损失,强调难例样本的损失,提高训练效率。同时,文章还讨论了如何在SVM和Adaboost中利用Hard Sample提升模型性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Focal Loss

何凯明大神在2017年的论文中提出Focal loss 论文
Focal Loss是一种处理深度学习中正负样本不均衡的方法. 它并没有固定形式,应该算是一种思路: 降低non-hard-sample的loss, 提高hard-sample的loss. 以二分类cross entropy loss为例
L ( y , y 0 ) = − l o g ( P y ) L(y,y_0) = -log(P_y) L(y,y0)=log(Py)
其中
P y = P i f   y 0 = 1 P_y = P \quad if\ y_0 = 1 Py=Pif y0=1
P y = 1 − P i f   y 0 = 0 P_y = 1-P \quad if\ y_0 = 0 Py=1Pif y0=0
y 0 y_0 y0是truth label, P y P_y Py是predicted probablity, P y ∈ [ 0 , 1 ] P_y \in [0,1] Py[0,1]
Focal Loss的一个实现是修改loss为如下形式
L ( y , y 0 ) = − ( 1 − P y ) 2 l o g ( P y ) L(y,y_0) = -(1-P_y)^2log(P_y) L(y,y0)=(1Py)2log(Py)
显然 P y P_y Py越接近1的样本,其分类效果越好,也说明这个样本属于non-hard-sample,观察下图,Focal loss压制了较大的 P y P_y Py对应的loss值,降低在反向传播过程中的作用
这里写图片描述

hard-sample

图像分类中,hard-sample就是对当前分类器难以区分的样本,"难以区分"包含两种情况

  • 分类正确,但置信度不高
    上面二分类的例子就属于这种情况
  • 分类错误
    adaboost中权重调教策略,更接近这种情况
    显然训练中希望大部分都是hard-sample,避免在non-hard-sample上消耗资源,有助于提高训练效率

hard-sample 和 SVM

hard-negative-sample 对SVM训练十分关键, 前几轮训练出的SVM分类器效果都不会太好,需要反复几轮筛选hard-negative-sample,构成训练集中负样本,才有可能得到较好的分类器. 这里hard-sample通过人工筛选方式获得.

hard-sample 和 adaboost

adaboost通过修改训练样本权重(提高hard-sample权重,降低non-hard-sample权重)驱动分类器选择那些对hard-sample更加有效的弱分类器,构成强分类达到高准确度的目的. 下面是常用的一个样本权重更新公式
w 1 = w 0 e − y f ( x ) w_1 = w_0 e^{-yf(x)} w1=w0eyf(x)
其中 w 0 w_0 w0是当前样本 x x x权重, w 1 w_1 w1是更新后的权重, y ∈ { − 1 , 1 } y \in \{-1,1\} y{1,1}是样本 x x x的truth label, f ( x ) f(x) f(x)当前分类器对 x x x的预测结果,最简单的实现中 f ( x ) f(x) f(x)是预测值和某个阈值的比较,大于阈值 f ( x ) = 1 f(x)=1 f(x)=1,否则 f ( x ) = − 1. 如 果 f(x)=-1. 如果 f(x)=1.y 和 和 f(x)$同号,则分类正确, w 1 &lt; w 0 w_1&lt; w_0 w1<w0,否则 w 1 &gt; w 0 w_1&gt; w_0 w1>w0. adaboost中样本权重更新的思路和focus loss一致.

hard-sample 和 neural network (此处特指浅层网络,不是deep learn)

似乎早期有人用sliding-window + neural network, 其中涉及繁复的负样本人工筛选,类似SVM中的策略

hard-sample 和 random forest

(random forest使用不多,不敢乱写,求指教)

### Equalized Focal Loss Code Implementation Equalized Focal Loss aims to address class imbalance and hard example mining more effectively by adjusting the standard Focal Loss formulation. The following Python code demonstrates an implementation of equalized focal loss based on existing research advancements[^1]: ```python import torch import torch.nn as nn import torch.nn.functional as F class EqualizedFocalLoss(nn.Module): def __init__(self, alpha=0.25, gamma=2.0, num_classes=80, reduction='mean'): super(EqualizedFocalLoss, self).__init__() self.alpha = alpha self.gamma = gamma self.num_classes = num_classes self.reduction = reduction def forward(self, logits, targets): # Calculate probabilities from logits probas = F.softmax(logits, dim=-1) # Create one-hot encoding for target classes y_onehot = F.one_hot(targets, num_classes=self.num_classes).float() # Compute weights inversely proportional to frequency freq_weights = 1 / ((y_onehot.sum(dim=0) + 1e-6) ** 0.5) # Normalize frequencies so they sum up to number of samples norm_freqs = freq_weights * (targets.shape[0] / freq_weights.sum()) # Apply normalization factor per sample eq_factor = norm_freqs.gather(1, targets.unsqueeze(-1)).squeeze() # Standard FL component ce_loss = F.cross_entropy(logits, targets, reduction="none") p_t = probas.gather(1, targets.unsqueeze(-1)).squeeze() + 1e-9 fl_modulating_factor = (1 - p_t)**self.gamma balanced_fl_weight = self.alpha * y_onehot + (1-self.alpha)*(1-y_onehot) # Combine all components into final EFL formula ef_loss = eq_factor * balanced_fl_weight.gather( 1, targets.unsqueeze(-1)).squeeze() * \ fl_modulating_factor * ce_loss if self.reduction == 'mean': return ef_loss.mean() elif self.reduction == 'sum': return ef_loss.sum() else: return ef_loss ``` This implementation introduces a new term `eq_factor` which adjusts each training instance's contribution according to its rarity within the dataset.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值