liveBook · Manning

Focal loss

As explained in Machine Learning System Design

A classification loss that down-weights well-classified high-confidence examples so training emphasizes difficult examples.

When one class dominates a classification dataset, ordinary cross-entropy can encourage mode collapse: predicting the popular class for nearly every input. Focal loss responds by making easy, high-confidence examples contribute less, leaving training capacity for difficult cases. section 5.1.1

Focal loss is a dynamically scaled form of cross-entropy. Its scaling factor approaches zero as confidence in the correct class rises, so well-classified examples are down-weighted while misclassified examples receive relatively more attention. section 5.1.1 section 5.3.2

Use focal loss when deciding how to make training respond to imbalanced data:

  • Prefer it to ordinary binary cross-entropy when one class is much more common and difficult examples matter disproportionately. section 5.3.2
  • Treat it as an alternative to manual undersampling, oversampling, or class weighting when those approaches require substantial tuning or prove unreliable. section 5.1.1
  • Remember that focal loss addresses the classification component; other objectives, such as diversity, may require additional loss terms and tuned weights. section 5.3.2

PhotoStock search

A search engine learns from clicks and purchases, where positive outcomes may be rare. Binary cross-entropy is a baseline, but focal loss can emphasize the harder click or purchase examples. A separate diversity term may be added when reducing similarity among returned results is also important. section 5.3.2

Figure 5.3 The suggested focal loss function focuses more on misclassified examples while reducing the relative loss for well-classified examples (source: Lin et al.).
figure

The figure shows focal loss assigning relatively greater importance to misclassified examples and reducing the relative loss for well-classified examples. section 5.1.1

How Machine Learning System Design covers it

How deep it goes
Defines dynamic scaling and practical tradeoffs but stops short of mathematical derivation or implementation details.
Examples
Uses object detection, imbalanced clicks and purchases, and a search-engine diversity objective as concrete applications.
What it assumes
Assumes familiarity with classification and cross-entropy while explaining class imbalance and easy-versus-hard examples directly.
What you can do after
Apply focal loss when imbalanced classification makes ordinary cross-entropy insufficiently attentive to difficult examples.
How it connects
Connects cross-entropy, class imbalance, mode collapse, object detection, binary classification, and combined loss functions.

Also explained in

Compare how 3 books explain focal loss →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning System Design. The section links point to the parts of the book it draws on.