PyTorch / Editorial solution guide
Diagnose a Weighted Loss Reduction Mismatch
Trace target indices, class weights, ignored labels, and denominators when manual and library losses differ.
Read-only knowledge base · No public comments
Recognize the symptoms
A manually averaged weighted loss differs from the library’s mean reduction.
Loss scale changes unexpectedly when class composition or ignored labels vary across batches.
Understand the likely causes
- The manual calculation may divide by batch size instead of the documented sum of target weights.
- Class-index mapping, target representation, ignore rules, or microbatch scaling may differ from the intended contract.
Capture the exact loss contract
Preserve a small synthetic batch, logits, targets, class weights, reduction, ignore setting, and installed library version. Record whether targets are class indices or another supported form; the documented reduction can depend on that representation. Work in a bounded lab and do not change production training until the discrepancy is understood.
Check class order, target range and dtype, input shape, and whether the model supplies raw logits as required. Confirm that the same eligible examples enter both calculations and record any ignored targets explicitly.
Trace individual contributions
Compute or inspect unreduced losses for the fixture and associate each with its target and weight. For the documented class-index weighted mean, compare the sum of eligible weighted losses with the sum of their target weights. Check the installed documentation for special cases and target forms instead of assuming one formula covers every input.
Reconstruct the result by hand for a few examples and compare with the library using an appropriate tolerance. If the calculations still differ, inspect preprocessing, reduction, and the exact arguments passed to the loss before changing weights or optimizer settings.
Correct and verify the objective
Correct the first confirmed mapping or denominator mismatch in a test copy. Test unequal class weights, an ignored target, and batches with different class mixes; define a safe policy for a batch with no eligible targets. If gradients are accumulated, verify scaling against an equivalent combined objective when the model permits that comparison.
Compare one update and held-out per-class metrics with the baseline before accepting a training change. Retain the original configuration and use the normal review process. A matching scalar alone does not prove that target alignment or operational performance is correct.
Verify the fix
The manual reconstruction and library result agree within the documented tolerance for the stated target form and eligible examples.
Class mapping, ignored-label cases, and representative microbatch scaling follow the intended objective; held-out checks show the effect of the correction.
Keep it from recurring
- Version the class mapping, weights, loss arguments, and reduction with each experiment.
- Keep a small unequal-weight fixture and test ignored and empty-eligible cases whenever data or training logic changes.