Debug a Training Loss That Suddenly Turns NaN
Last updated: November 29, 2026
Quick Overview
A deep ranking model trains normally for several hours, then the loss becomes NaN and never recovers. Walk through how you would find the cause and prevent it.
Amazon
November 29, 20260
0
0 solved
A deep ranking model trains normally for several hours, then the loss becomes NaN and never recovers. Walk through how you would find the cause and prevent it.
Amazon interviewers like debugging questions because they reveal Dive Deep habits: forming hypotheses, instrumenting, and narrowing down systematically rather than randomly changing hyperparameters. The scenario is common in large recommendation and LLM training jobs.
What the Interviewer Expects
- List likely causes: exploding gradients, bad data, numerical ops, learning rate
- Instrument gradient norms, activation ranges and per-batch loss
- Reproduce from a checkpoint with the offending batch
- Fix with clipping, stable ops, data validation or lower learning rate
- Add guards so future runs fail fast
Key Topics to Cover
How to Approach This
- Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
- Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
- Feature engineering is often more impactful than model selection.
- Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
- Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
- How would you find the exact batch that caused it in a distributed job?
- What if NaNs appear only on one worker?
- How would you tell a data issue from an optimization issue?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Explore ML Interview PrepSample Answer
Clarify
Ask: did loss spike before NaN or jump straight to NaN? Is mixed precision on? Did the data pipeline change (new feature, new day of logs)? Is it reproducible from the last checkpoint?...
Approach
Gradual increase then NaN suggests optimization instability (learning rate too high, exploding gradients). An instant jump from normal loss suggests a bad input (a NaN or infinite feature value, a lab...