Focal Loss Calculator
Compute focal loss FL(p_t) = −α_t(1 − p_t)^γ ln(p_t) for a binary or multiclass prediction in your browser. Enter the predicted probability, γ and α to get the focal loss, the plain cross-entropy, the modulating factor, the down-weighting ratio and the batch mean — the exact loss used to train RetinaNet, cross-checked against torchvision.
How it works
Focal loss was introduced by Lin, Goyal, Girshick, He and Dollár in “Focal Loss for Dense Object Detection” (ICCV 2017) to fix a specific problem: in dense detectors like RetinaNet there are tens of thousands of easy background boxes for every real object, and under ordinary cross-entropy their many small losses add up and drown out the handful of hard, informative examples. Focal loss reshapes cross-entropy so easy examples contribute far less.
Start from the probability the model assigned to the true class, p_t. In the binary case with label y ∈ {0, 1} and predicted positive-class probability p, Eq. 2 of the paper defines p_t = p when y = 1 and p_t = 1 − p otherwise. Plain cross-entropy (Eq. 1) is then:
CE(p_t) = −ln(p_t)
Focal loss multiplies that by a modulating factor (Eq. 3) and an optional α-balancing weight (Eq. 4), giving the final form (Eq. 5):
FL(p_t) = −α_t · (1 − p_t)^γ · ln(p_t) = α_t · (1 − p_t)^γ · CE
- Modulating factor
(1 − p_t)^γ. When an example is well-classified (p_tnear 1) this is near 0, so its loss almost vanishes; when it is misclassified (p_tsmall) the factor is near 1 and the loss is barely touched.γ(gamma) sets how aggressively this happens. Withγ = 0the factor is 1 and focal loss is exactly cross-entropy. - α-balancing
α_t. When enabled,α_t = αfor the positive class and1 − αfor the negative class; when disabled,α_t = 1. It nudges the balance between the two classes on top of the γ down-weighting. - Clamp for safety.
p_tis clamped to[1e-7, 1 − 1e-7]before the log so a predicted 0 gives a large finite loss instead ofln 0 = −∞. - Batch mean. The trainable objective is the average
(1/N)·Σ FL_iover all samples, which the batch section computes.
The recommended defaults are γ = 2 and α = 0.25 — the best-performing settings in the paper (§5). Multiclass mode uses the same equations with p_t taken as the softmax probability of the correct class and α as a single per-sample class weight. To keep every result trustworthy the tool independently recomputes each binary focal loss the way torchvision.ops.sigmoid_focal_loss does — building cross-entropy from (p, y) first, then applying the modulating factor and α_t — and confirms the two agree to floating-point precision.
Worked examples
Frequently asked questions
Sources & references
- Lin, Goyal, Girshick, He & Dollár — Focal Loss for Dense Object Detection (ICCV 2017), Eq. 1–5, defaults γ = 2, α = 0.25
- PyTorch — torchvision.ops.sigmoid_focal_loss (reference implementation cross-checked here)
- scikit-learn — sklearn.metrics.log_loss (cross-entropy baseline; γ = 0 reduces focal loss to this)
The equations on this page were last cross-checked against these sources on 2026-07-12. Focal loss is a fixed mathematical definition, so this tool needs no rate or schedule updates — only the worked examples are periodically re-reconciled.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.