Implement Focal Loss from “Focal Loss for Dense Object Detection” (Lin et al., 2017).
Focal loss addresses class imbalance by down-weighting easy examples:
$$\text{FL}(p_t) = -\alpha_t (1 - p_t)^\gamma \log(p_t)$$
Where $p_t$ is the model’s predicted probability for the true class.
For binary classification:
Given:
probs: shape (batch,) — predicted probabilities (after sigmoid) targets: shape (batch,) — binary targets (0 or 1) alpha: float — class balancing factor gamma: float — focusing parameter Output: Scalar (float) — mean focal loss over the batch.