Quant Memo
Core

Focal Loss for Imbalanced Targets

When 99% of examples are the easy, obvious class, ordinary cross-entropy spends nearly all its gradient re-confirming what the model already knows; focal loss down-weights easy examples automatically so training effort concentrates on the hard, rare ones.

Prerequisites: Cross-Entropy and Log Loss

Imagine training a fraud detector where 1 in 1,000 transactions is fraudulent. A model that has already learned to confidently flag the other 999 as "not fraud" keeps generating loss (and gradient) from those easy examples on every single batch — even though the model already gets them right — simply because ordinary cross-entropy penalizes any gap between predicted probability and 1, however small. That accumulated gradient from thousands of easy, already-correct examples can drown out the comparatively rare, useful signal coming from the hard examples the model is still getting wrong. Focal loss fixes this by making the per-example loss shrink automatically once a prediction is already confident and correct.

The analogy: a teacher's attention in a classroom

A good tutor doesn't spend equal time re-explaining a problem to a student who already nailed it and one who is still stuck — attention flows to the students who need it. Ordinary cross-entropy is a tutor who insists on reviewing every student's answer with equal weight, even the ones who've mastered the material. Focal loss is the tutor who notices "the model already got this one right with 99% confidence" and automatically spends far less loss, and therefore far less gradient, re-teaching it, redirecting that budget toward the hard cases.

The formula

For binary classification, let ptp_t be the model's predicted probability for the true class (so ptp_t close to 1 means a confident, correct prediction). Ordinary cross-entropy is log(pt)-\log(p_t). Focal loss adds a modulating factor:

FL(pt)=(1pt)γlog(pt)\text{FL}(p_t) = -(1-p_t)^{\gamma} \log(p_t)

In words: take the ordinary cross-entropy loss and multiply it by (1pt)γ(1-p_t)^\gamma, a term that is close to 0 when ptp_t is close to 1 (an easy, correct example) and close to 1 when ptp_t is small (a hard or wrong example). The focusing parameter γ\gamma (commonly 2) controls how aggressively easy examples get down-weighted — larger γ\gamma suppresses them harder.

Worked example 1: an easy example versus a hard example, γ=2\gamma = 2

Easy example: the model predicts pt=0.95p_t = 0.95. Ordinary cross-entropy: log(0.95)=0.051-\log(0.95) = 0.051. Focal loss: (10.95)2=0.0025(1-0.95)^2 = 0.0025, so FL=0.0025×0.051=0.000128\text{FL} = 0.0025 \times 0.051 = 0.000128 — about 400 times smaller than plain cross-entropy.

Hard example: the model predicts pt=0.4p_t = 0.4 (getting it mostly wrong). Ordinary cross-entropy: log(0.4)=0.916-\log(0.4) = 0.916. Focal loss: (10.4)2=0.36(1-0.4)^2 = 0.36, so FL=0.36×0.916=0.330\text{FL} = 0.36 \times 0.916 = 0.330 — reduced, but only by a factor of about 2.8, far less suppression than the easy example got.

Worked example 2: how the ratio shifts training's center of gravity

Under plain cross-entropy, the easy example contributes 0.0510.051 and the hard one 0.9160.916 — the hard example already dominates by about 18×. Under focal loss, the easy example contributes 0.0001280.000128 and the hard one 0.3300.330 — a ratio of about 2,580×. Aggregated over a batch that is mostly easy examples (as in the 999-to-1 fraud case), that shift is decisive: the total gradient from thousands of easy, correct predictions collapses toward zero, while the gradient from the rare hard cases stays comparatively large.

Drag the exponent on the curve below (a power-law shape, matching (1pt)γ(1-p_t)^\gamma) to see how sharply the down-weighting effect grows as γ\gamma increases and ptp_t moves away from 1.

Function explorer
-224.4
x = 1.00f(x) = 1.000

Cross-entropy easy 0.051 hard 0.916 Focal loss (γ=2) easy 0.0001 hard 0.330
Focal loss shrinks the easy example's loss far more aggressively than the hard example's, redirecting training's effective attention toward examples the model is still getting wrong.

Focal loss multiplies cross-entropy by (1pt)γ(1-p_t)^\gamma, a factor that vanishes for confident, correct predictions and stays near 1 for hard or wrong ones — it is a self-adjusting down-weighting of easy examples, aimed directly at severe class imbalance, without needing to hand-tune class weights.

What this means in practice

Focal loss was designed for dense object detection, where background regions vastly outnumber actual objects, but the same imbalance shows up constantly in finance: rare regime shifts, rare defaults, rare large-move labels swamped by the "nothing happened" majority class. It's a genuine alternative to naive class-weighting, because it adapts per-example rather than per-class — an easy minority-class example still gets suppressed, and a hard majority-class example still gets emphasized, which fixed class weights cannot do.

The common confusion is treating focal loss as a fix for class imbalance in the labels rather than in the loss landscape. It does nothing to change how many examples of each class the model sees, and it does not fix a genuinely broken label distribution or missing data for the minority class — it only changes how much each individual example's error counts toward the gradient, based on how confidently correct or wrong the model currently is on it.

Related concepts

Practice in interviews

Further reading

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