Notes

Focal Loss

When a classifier trains on imbalanced data, it can get “lazy”: it learns to do extremely well on the many easy examples and barely improves on the rare, important ones. Focal Loss is a tweak to standard cross-entropy that pushes the model to spend more learning effort on the hard, misclassified cases.

What it is and how it works

Focal Loss starts from cross-entropy loss and multiplies it by a factor that shrinks the loss for examples the model already predicts confidently. For binary classification, with true label y ∈ {0,1} and predicted probability p for class 1, define pt = p if y=1 else (1−p). Then:

FL(p_t) = - α * (1 - p_t)^γ * log(p_t)

The key parts are:

  • (1 − pt)γ: the “focusing” term. If an example is easy (pt near 1), this term becomes tiny, down-weighting its gradient. If it’s hard (pt small), the term stays large.
  • γ (gamma): how aggressively you focus on hard examples. γ=0 recovers plain cross-entropy.
  • α (alpha): optional class weighting to address imbalance directly (e.g., rare positives in fraud detection).

Why it matters in supervised learning

In tasks like object detection, fraud detection, or medical screening, negatives can vastly outnumber positives. With plain cross-entropy, the sea of easy negatives can dominate training, leading to poor recall on rare positives. Focal Loss reduces that dominance, improving learning on minority and borderline cases without needing to discard data.

Where you’ll see it in practice

It’s common in dense detection models (e.g., RetinaNet) and is available in deep learning libraries (e.g., TensorFlow Addons’ focal loss, PyTorch implementations in torchvision or custom code). Typical tuning revolves around γ (focus strength) and α (class balance), validated with metrics sensitive to imbalance like PR-AUC or F1.

Focal Loss is a classification loss that modifies cross-entropy by down-weighting easy, well-classified examples and concentrating gradient on hard examples, using a focusing parameter (γ) and optionally class-balancing weights (α). It matters because it improves training under severe class imbalance (e.g., dense object detection), preventing abundant negatives from dominating optimization and helping the model learn rare, difficult positives.

Imagine you’re a teacher grading a class where most questions are easy and only a few are truly tricky. If you spend all your time on the easy ones, students won’t improve where it matters. Focal Loss is like a grading rule that pays less attention to “already easy” examples and puts more weight on the hard, mistake-prone ones.

In supervised learning, it’s used when training a classifier (a model that picks a category) and the data is unbalanced—like lots of normal emails and only a few spam, or many healthy scans and a few showing disease. It helps the model focus on the rare, important cases.