Please compare focal loss and weighted cross-entropy.
Answer
Weighted cross-entropy (WCE) multiplies each class’s loss by a fixed weight : it corrects class frequency but treats every sample of a class identically. Focal loss (FL) adds a per-sample, difficulty-dependent factor
: it corrects prediction difficulty, automatically ignoring easy examples regardless of class. WCE is simple and robust; FL is stronger when an ocean of easy negatives overwhelms learning, but it can amplify noisy labels.
(1) What Each Balances: WCE reweights by class prior; FL reweights by per-sample hardness (and can include too).
(2) Gradient Behavior: WCE still lets millions of correctly-classified negatives contribute steady gradient; FL drives their contribution toward zero.
(3) Robustness: WCE has one interpretable hyperparameter and is safe with noisy labels; FL’s focus on hard examples can overfit label noise and needs tuned .
Mathematical Formulation:
Where:
is the model’s predicted probability for the ground-truth class;
is the fixed per-class weight.
is the focusing parameter, the entire difference between the two losses; at
focal loss collapses to WCE.
| Property | Weighted Cross-Entropy | Focal Loss |
|---|---|---|
| Handles class imbalance | Yes: fixed class weights | Yes: class weight + hardness factor |
| Focuses on hard samples | No: easy samples still dominate | Yes: easy samples fade to zero |
| Hyperparameters | One: | Two: |
| Main risk | Underwhelming at extreme imbalance | Overfits noisy / mislabeled hard examples |
Table 1: WCE vs focal loss: the same class weighting, but only focal loss adds per-sample difficulty weighting.

Figure 1: WCE rescales the CE curve uniformly (same shape, different height); focal loss bends the shape, crushing the high- (easy) region toward zero.
Rule of Thumb: Moderate imbalance (up to ~10:1) → WCE is enough; extreme imbalance with floods of easy negatives (dense detection, 1000:1) → focal loss; noisy labels → prefer WCE.














