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 be the model's predicted probability for the true class (so close to 1 means a confident, correct prediction). Ordinary cross-entropy is . Focal loss adds a modulating factor:
In words: take the ordinary cross-entropy loss and multiply it by , a term that is close to 0 when is close to 1 (an easy, correct example) and close to 1 when is small (a hard or wrong example). The focusing parameter (commonly 2) controls how aggressively easy examples get down-weighted — larger suppresses them harder.
Worked example 1: an easy example versus a hard example,
Easy example: the model predicts . Ordinary cross-entropy: . Focal loss: , so — about 400 times smaller than plain cross-entropy.
Hard example: the model predicts (getting it mostly wrong). Ordinary cross-entropy: . Focal loss: , so — 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 and the hard one — the hard example already dominates by about 18×. Under focal loss, the easy example contributes and the hard one — 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 ) to see how sharply the down-weighting effect grows as increases and moves away from 1.
Focal loss multiplies cross-entropy by , 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)