Given a cancer-screening dataset split 100 positive vs 900 negative, what loss will you use and why? How exactly does it help, and what metric do you report?

The Problem: A cancer-screening model is trained on 1,000 scans, 100 with cancer and 900 healthy. Plain BCE reaches 91% accuracy and 0.93 ROC-AUC yet catches only 38 of 100 cancers at threshold 0.5, and the team must choose a loss and a reported metric that an all-negative model’s 90% accuracy cannot fool.
Answer
I would train with class-weighted binary cross-entropy, setting the positive weight to so the 100 cancers carry as much total gradient as the 900 healthy scans. At the optimum, weighting adds
to every logit, which equals moving the threshold from 0.5 to 0.1, and during training it keeps the negatives from dominating every update. Focal loss is the tool for much harsher imbalance flooded with easy negatives, so it is not my first choice at 1:9. For reporting I lead with PR-AUC (average precision) against its no-skill baseline of 0.10, plus sensitivity, specificity and PPV at a threshold tuned on validation to the clinical sensitivity target. Accuracy is useless because an all-negative model already scores 90%, and ROC-AUC flatters the model because it ignores how many false alarms each detected cancer costs.
(1) Cost Asymmetry Is The Real Reason: Weight 9 at threshold 0.5 is equivalent to declaring a missed cancer as costly as 9 false alarms. Imbalance alone does not justify reweighting; the asymmetric cost of a false negative does.
(2) Gradient Balance: At initialization, where every prediction is 0.5, plain BCE receives a total gradient of 450 from healthy scans and 50 from cancers, so early updates mostly learn to say “healthy”. With the two totals are 450 and 450.
(3) Weighting Is Mostly A Threshold Shift: For a well-specified model with enough data, weighting only adds to each logit, so the ranking, and therefore ROC-AUC and PR-AUC, barely changes. It still helps a small or heavily regularized model spend capacity near the cancers, but it leaves the scores miscalibrated by a factor of 9 in odds.
(4) Focal Loss Targets Easy-Negative Floods: The factor with
cuts a confident healthy scan at
by 400 times. It was built for dense detection at roughly 1:1000, and at 1:9 it mainly adds a second hyperparameter and distorts probabilities.
(5) PR-AUC Plus An Operating Point: A random ranker scores ROC-AUC 0.5 at any prevalence but PR-AUC equal to the prevalence, 0.10 here, so PR-AUC shows how far above chance the model really is. The decision itself is reported as sensitivity, specificity and PPV at the validated threshold, with confidence intervals.
Mathematical Formulation:
Where:
is the predicted cancer probability,
the sigmoid, and
the model’s logit for one scan.
is the per-example weighted BCE,
the label with 1 meaning cancer, and
the positive-class weight (the pos_weight argument of PyTorch’s BCEWithLogitsLoss).
and
are the negative and positive counts, so
gives both classes equal total loss mass.
is the gradient on the logit: a cancer’s push
is scaled by 9, a healthy scan’s push
is unchanged.
is the true posterior,
the minimizer of the expected weighted loss, and
. The shift by
assumes a model flexible enough to reach the optimum.
is focal loss,
the probability assigned to the true class,
the focusing parameter (
recovers weighted BCE), and
the class weight, which Lin et al. set to 0.25 on the rare class because
already rebalances.
is average precision, the step-wise PR-AUC, with
and
the precision and recall at the n-th threshold over sorted scores; its no-skill value equals the prevalence.
is precision at a fixed threshold,
the prevalence,
the sensitivity, and
.

Figure 1: (a) Gradient on the logit per example. With a cancer predicted at 0.5 pushes with 4.5 instead of 0.5, so at initialization the 100 cancers and 900 healthy scans each contribute a total of 450. (b) Focal loss keeps only
of each loss: at
a confident healthy scan keeps 0.25% while a hard cancer at
keeps 49%.
Worked Example:
Take a scan whose true cancer posterior is . A plain-BCE model outputs 0.2 and misses it at threshold 0.5. The weighted model outputs
and flags it, exactly as the plain model would at threshold 0.1. Now suppose the trained model, at its chosen threshold, flags 180 scans: 90 of the 100 cancers and 90 of the 900 healthy scans. Sensitivity and specificity are both 0.90, precision is
, and accuracy is
. That is the same 90% the all-negative model scores, so accuracy cannot separate a model that catches 90 cancers from one that catches none.
| Property | Weighted BCE (pos_weight 9) | Focal loss (gamma 2) | Plain BCE + tuned threshold |
|---|---|---|---|
| What it changes | Scales every cancer’s loss and gradient by 9 | Shrinks the loss of confident examples by (1 – p_t)^gamma, plus an alpha class weight | Nothing in training; only the decision cut-off moves |
| Probabilities | Odds inflated 9x; undo by subtracting log 9 from the logit | Distorted with no closed-form fix; recalibrate on validation | As calibrated as the model itself |
| Extra knobs | One, set by the class ratio or the cost ratio | Two (alpha, gamma), hard to tune with 100 positives | None beyond the threshold |
| Best when | Moderate imbalance with a real cost asymmetry, as in this screening set | Extreme imbalance flooded by easy negatives, such as dense detection near 1:1000 | Plenty of data and a flexible model, where weighting reduces to a threshold shift |
| Main risk | Amplifies any label noise among the 100 positives | Underweights easy cases and adds tuning variance on a tiny positive set | Negatives dominate early updates and small batches may hold no cancers |
Leave a Reply