When 99% of examples are easy negatives (background in object detection, legitimate transactions in fraud), their small losses still add up and swamp the rare positives. Focal loss down-weights easy examples:
where for a binary label and predicted probability : if , else ; and if , else .
Implement focal_loss(logits, y, alpha=0.25, gamma=2.0) returning the mean loss. Compute stably from the logits: and , using np.logaddexp(0, ·). With gamma=0 and alpha=0.5 it equals half the binary cross-entropy.