Train a classifier that fails with a naïve loss implementation
Estimated 100–130 min
Investigate and repair an unstable 4,096-class cross-entropy implementation.
Extreme logits, executable NumPy tests, gradient checks, and recorded precision stress data.
Preserve the exact objective; finite output alone is not sufficient.
Submit a correctness argument, evidence, limitations, and next adversarial test.
Python executes locally in the browser; the precision stress matrix is clearly labeled recorded data.
Repair an unstable classifier loss
4,096-class classifier trained with BF16 activations
Loss becomes NaN when logits reach approximately ±120
Preserve exact cross-entropy while preventing overflow
Forward agreement, finite outputs, and gradient checks
Python runs locally in the browser with NumPy. Recorded stress tables are labeled separately.
Trace the numerical failure
Why does the mathematically correct expression fail in finite precision?
Naive softmax computes exp(logit). Large positive logits overflow; very negative logits underflow. Dividing an infinite numerator by an infinite sum can produce NaN.
Subtracting one constant from every logit leaves softmax unchanged. Choosing the row maximum makes every exponent non-positive, so the largest exponent is exactly one.
For logits [120, 119, -120], use m=120: logsumexp = 120 + log(exp(0)+exp(-1)+exp(-240)). Cross-entropy is logsumexp minus the target logit.
| Implementation | Largest exponent | Objective changed? |
|---|---|---|
| Naive exp(logits) | exp(120) | No, but it overflows |
| Shift by row maximum | exp(0)=1 | No |
| Clip logits to [-20,20] | exp(20) | Yes; clipping changes differences |