FocalLoss#
- class torch_uncertainty.losses.FocalLoss(gamma, alpha=None, reduction='mean')[source]#
Focal Loss for classification tasks.
Down-weights the contribution of well-classified samples to the cross-entropy loss, focusing training on hard examples. For a target class \(y\) with predicted probability \(p_y\),
\[\text{FL}(p_y) = -\alpha_y \, (1 - p_y)^\gamma \, \log p_y,\]where \(\gamma \geq 0\) is
gamma(a larger \(\gamma\) suppresses easy examples more aggressively) and \(\alpha_y\) is the per-class weight fromalpha(defaulting to1).- Parameters:
gamma (
float) – The focusing parameter \(\gamma\), as described in the paper.alpha (
Tensor|None) – Per-class rescaling weights. Defaults toNone.reduction (
str) –'mean','sum'or'none'. Defaults to'mean'.
References
[2] Inspired by AdeelH/pytorch-multi-class-focal-loss.