TensoriumInterview preparation
ML foundations · Guided lab

Train a classifier that fails with a naïve loss implementation

Estimated 100–130 min

requirements

Investigate and repair an unstable 4,096-class cross-entropy implementation.

inputs

Extreme logits, executable NumPy tests, gradient checks, and recorded precision stress data.

constraints

Preserve the exact objective; finite output alone is not sufficient.

finish

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.

Numerical incident · executable browser lab

Repair an unstable classifier loss

model

4,096-class classifier trained with BF16 activations

incident

Loss becomes NaN when logits reach approximately ±120

objective

Preserve exact cross-entropy while preventing overflow

proof

Forward agreement, finite outputs, and gradient checks

Python runs locally in the browser with NumPy. Recorded stress tables are labeled separately.

1. Trace the numerical failure
2. Implement stable forward loss
3. Verify the gradient, not just the value
4. Stress the proposed repairs
5. Defend the numerical implementation
Chapter 1

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.

Worked analysis

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.

ImplementationLargest exponentObjective changed?
Naive exp(logits)exp(120)No, but it overflows
Shift by row maximumexp(0)=1No
Clip logits to [-20,20]exp(20)Yes; clipping changes differences