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 from alpha (defaulting to 1).

Parameters:
  • gamma (float) – The focusing parameter \(\gamma\), as described in the paper.

  • alpha (Tensor | None) – Per-class rescaling weights. Defaults to None.

  • reduction (str) – 'mean', 'sum' or 'none'. Defaults to 'mean'.

References

[1] Lin, T.-Y., Goyal, P., Girshick, R., He, K., & Dollár, P. (2017). Focal Loss for Dense Object Detection.

[2] Inspired by AdeelH/pytorch-multi-class-focal-loss.