Quant Memo
Core

Focal Loss

A tweak to standard classification loss that automatically down-weights examples the model already gets right, forcing training to focus on the hard, rare cases — useful when one class vastly outnumbers another.

Standard classification training uses cross-entropy loss, which treats every example's error equally regardless of how confident the model already is. In a badly imbalanced problem — say, predicting a rare fraud flag or a rare tail event that occurs in 1% of rows — the model can rack up a low average loss just by confidently predicting "no event" every time, since the 99% of easy, correctly-classified examples swamp the loss contributed by the rare, hard positives.

Focal loss modifies cross-entropy by multiplying each example's loss by a factor (1pt)γ(1-p_t)^\gamma, where ptp_t is the model's predicted probability for the true class and γ\gamma (commonly 2) is a tunable focusing parameter. When the model is already confident and correct, ptp_t is close to 1, so (1pt)γ(1-p_t)^\gamma is close to zero and that example's contribution to the loss is squashed. When the model is wrong or unsure, ptp_t is small, (1pt)γ(1-p_t)^\gamma stays close to 1, and the loss is barely reduced — training keeps pushing hard on exactly those examples.

Concretely, with γ=2\gamma = 2: an easy example correctly predicted with pt=0.95p_t = 0.95 gets its loss multiplied by (10.95)2=0.0025(1-0.95)^2 = 0.0025, essentially removed from training, while a hard example with pt=0.3p_t = 0.3 gets multiplied by (0.7)2=0.49(0.7)^2 = 0.49, barely discounted — a roughly 200x difference in how much each example moves the gradient.

Focal loss reweights cross-entropy by (1pt)γ(1-p_t)^\gamma so that easy, already-correct examples contribute almost nothing to training and hard or rare examples keep driving learning — a direct fix for the class-imbalance problem where common, easy cases otherwise dominate the loss.

Related concepts

Further reading

  • Lin et al., Focal Loss for Dense Object Detection, 2017
ShareTwitterLinkedIn