Skip to content
induwara.lk
Premium
induwara.lkAI · Machine learning

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.

By Induwara AshinsanaUpdated Jul 12, 2026
Focal loss calculator
True label y

Model's probability for the positive class, 0–1.

Higher γ down-weights easy examples more. Default 2.

Class weight for the positive class, 0–1. Default 0.25.

γ presets
Examples
Focal loss
0.000263401
α_t · (1 − p_t)^γ · CE
Cross-entropy
0.105361
−ln(p_t)
Modulating (1−p_t)^γ
0.01
p_t = 0.9
α-weight α_t
0.25
1 when α off
FL / CE ratio
0.0025
loss kept vs plain CE
Significant figures

Step-by-step working

  1. p_t = p = 0.9
  2. CE = −ln(p_t) = 0.105361
  3. modulating = (1 − p_t)^γ = 0.01
  4. α_t = 0.25
  5. FL = α_t · modulating · CE = 0.25 × 0.01 × 0.105361 = 0.000263401

Cross-check. Computing the same loss the torchvision way — build binary cross-entropy from (p, y) first, then multiply by the modulating factor and α_t — gives 0.000263401. It reconciles with the value above, as it must.

Loss curve

0123400.250.50.751p_t (probability of true class)
Focal (γ = 2) Cross-entropy (γ = 0) your p_t

Batch mean (optional)

Mean focal loss (N=3)
0.0939497
the training objective
Mean cross-entropy
0.690491
unweighted baseline
Mean FL / mean CE
0.136062
overall down-weighting
Rowp_tCEFocal loss
#10.90.1053610.000263401
#20.21.609440.25751
#30.70.3566750.0240756
Mean focal loss0.0939497

Method: FL(p_t) = −α_t · (1 − p_t)^γ · ln(p_t) with p_t clamped to [1e-7, 1 − 1e-7] — Lin et al. 2017 (arXiv:1708.02002), Eq. 1–5; cross-checked against torchvision.ops.sigmoid_focal_loss. No data leaves this page.

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

  1. Modulating factor (1 − p_t)^γ. When an example is well-classified (p_t near 1) this is near 0, so its loss almost vanishes; when it is misclassified (p_t small) the factor is near 1 and the loss is barely touched. γ (gamma) sets how aggressively this happens. With γ = 0 the factor is 1 and focal loss is exactly cross-entropy.
  2. α-balancing α_t. When enabled, α_t = α for the positive class and 1 − α for the negative class; when disabled, α_t = 1. It nudges the balance between the two classes on top of the γ down-weighting.
  3. Clamp for safety. p_t is clamped to [1e-7, 1 − 1e-7] before the log so a predicted 0 gives a large finite loss instead of ln 0 = −∞.
  4. Batch mean. The trainable objective is the average (1/N)·Σ FL_i over 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

Easy positive — y = 1, p = 0.9, γ = 2, α = 0.25

  1. p_t = p = 0.9 (label is positive)
  2. CE = −ln(0.9) = 0.105361
  3. modulating = (1 − 0.9)^2 = 0.01
  4. α_t = 0.25 (positive class)
  5. FL = 0.25 × 0.01 × 0.105361 = 0.000263401
  6. FL / CE = 0.0025 → the easy example keeps ~1/400th of its cross-entropy

Hard positive — y = 1, p = 0.2, γ = 2, α = 0.25

  1. p_t = p = 0.2
  2. CE = −ln(0.2) = 1.609438
  3. modulating = (1 − 0.2)^2 = 0.64
  4. α_t = 0.25
  5. FL = 0.25 × 0.64 × 1.609438 = 0.257510
  6. FL / CE = 0.16 → the hard example keeps 16% of its CE, so it dominates training

Negative example — y = 0, p = 0.3, γ = 2, α = 0.25

  1. p_t = 1 − p = 0.7 (label is negative)
  2. CE = −ln(0.7) = 0.356675
  3. modulating = (1 − 0.7)^2 = 0.09
  4. α_t = 1 − 0.25 = 0.75 (negative class)
  5. FL = 0.75 × 0.09 × 0.356675 = 0.0240756

Edge case — predicted 0 (y = 1, p = 0), clamped

  1. p_t = 0 would give −ln(0) = +∞ — undefined
  2. Clamp p_t to 1e-7: CE = −ln(1e-7) = 16.118096
  3. modulating = (1 − 1e-7)^2 ≈ 1; α_t = 0.25
  4. FL = 0.25 × 1 × 16.118096 = 4.029524 (finite, heavily penalised)
  5. Sanity: with γ = 0 and α-weighting off, FL = CE exactly for any p

Frequently asked questions

Sources & references

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

Rate this tool
Be the first to rate

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.