Loss Functions › Focal Loss for Class Imbalance
Language AI HandbookAll chapters Loss Functions › Focal Loss for Class ImbalanceOne section · Sections in this chapter6 / 17 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Many real-world classification problems have severe class imbalance: fraud detection, rare disease diagnosis, named entity recognition (where most tokens are not entities). Standard cross-entropy performs poorly in these settings because easy negative examples (confidently classified background tokens) dominate the loss, overwhelming the contribution of the rare positive class.
The problem is more subtle than just having fewer positive examples. Even after you account for class frequency, the model tends to become good at classifying the easy majority class early in training. Once the model can classify the majority class with high confidence, those examples contribute almost no loss (because ), but there are many of them. The rare, hard-to-classify minority examples contribute more loss per example, but there are so few of them that they still get drowned out.
Focal loss (introduced by Lin et al., 2017 in the context of object detection) down-weights easy examples dynamically, focusing training on hard examples:
where:
- : the model's predicted probability for the true class
- : the focusing parameter (typically 2.0)
- : the modulating factor, close to 1 for low confidence and close to 0 for high confidence
When the model is already confident about an example (), the modulating factor reduces the loss contribution by 100x. When the model is uncertain (), the factor reduces the loss by only 4x. Hard examples (where the model is wrong or uncertain) contribute more to the loss than easy examples.
Focal loss is often combined with a class-weighting factor :
where:
- : the per-class weight, which can be set to the inverse frequency of each class to further up-weight rare classes
Setting recovers standard cross-entropy. A common default is and for the positive class.
Focal loss adapts dynamically to each example based on the model's current confidence, rather than applying a fixed multiplier like class weighting. This is a powerful property: early in training when the model is uncertain about everything, focal loss behaves similarly to standard cross-entropy. As training progresses and the model masters easy examples, focal loss automatically down-weights those contributions and concentrates the learning signal on the difficult cases that remain.