What is focal loss and when would you use it over cross-entropy?
Written by the mlprep team · Updated
What is focal loss? Why was it introduced, and how does it differ from cross-entropy? When would you reach for it?
The problem: easy examples swamp the gradient#
In single-stage object detection (RetinaNet's setting), the ratio of background anchors to foreground anchors can be 1000:1. Cross-entropy loss treats all examples equally. An easy background anchor with p=0.01 contributes a small but nonzero loss. Summed over 100,000 such anchors, the easy negatives overwhelm the gradient signal from the rare hard positives the model actually needs to learn.
Standard techniques: hard negative mining, class-weighted loss. These help but require careful tuning and can still miss many hard examples.
Focal loss#
Focal loss modifies cross-entropy by a modulating factor (1-p_t)^γ:
FL(p_t) = -(1 - p_t)^γ · log(p_t)
where p_t is the model's estimated probability for the true class, and γ ≥ 0 is a focusing parameter (typically γ=2).
What the modulating factor does#
- When the model is confident and correct (p_t ≈ 0.95): factor = (1-0.95)² = 0.0025. Loss scaled down by 400×.
- When the model is uncertain or wrong (p_t = 0.4): factor = (0.6)² = 0.36. Loss only reduced 3×.
Easy examples that the model already handles correctly contribute almost zero to the total loss. Hard examples — misclassified or uncertain — dominate. The model's attention is automatically focused on the difficult cases.
At γ=0, focal loss = standard cross-entropy. As γ increases, downweighting of easy examples becomes stronger. γ=2 is the standard choice from the RetinaNet paper.
Often combined with α (class weighting)#
FL(p_t) = -α_t · (1 - p_t)^γ · log(p_t)
α_t is a scalar weight per class (e.g., 0.25 for foreground, 0.75 for background in detection). This handles prior class imbalance; the (1-p_t)^γ term handles example difficulty imbalance.
When to use it#
- Object detection with dense anchor boxes (RetinaNet, YOLO variants). The original motivation.
- Severe class imbalance where easy negatives swamp training — fraud detection, medical anomaly detection.
- When class-weighted cross-entropy isn't enough: weighting adjusts for frequency, not difficulty. Focal loss additionally downweights easy examples regardless of class.
When NOT to use it#
- Balanced datasets where easy examples carry signal
- When you haven't tried weighted cross-entropy first — it's simpler and often sufficient
- Small datasets where you need all examples contributing
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Focal loss = cross-entropy × (1 - p_t)^γ. The modulating factor downweights easy, well-classified examples so hard examples dominate training. γ=0 recovers CE; γ=2 is standard. Use for dense detection (many background anchors) or severe class imbalance where easy negatives drown out hard positives. Simple weighted cross-entropy is still the first thing to try.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How would you set the α and γ hyperparameters? Is there a systematic way?
- Focal loss was designed for single-stage detectors. Could you use it for NLP tasks with class imbalance?
- What are alternatives to focal loss for handling class imbalance, and how do they compare?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Explain cross-entropy loss — binary vs categorical, and why it works for classification
What is KL divergence and how does it relate to cross-entropy?
Explain contrastive and triplet loss — how do they train embedding spaces?
Implement softmax and cross-entropy loss from scratch