Skip to content
ML Academy

Neural networks, deep learning, NLP · insight-0428

Triage a NaN loss in a fixed order, starting with the data

Evidence type
practitioner heuristic
Difficulty
medium
Roles
ML engineer, Data scientist
Industries
banking

Summary: situation, action, reason, boundary

  1. Situation

    The loss of a fraud model becomes NaN in epoch 3, and the team starts changing the architecture.

  2. Action

    Check in order: non-finite values in inputs and targets of the failing batch; operations such as log, division or sqrt on values near zero; learning rate and gradient norm; reduced-precision overflow. Use torch.autograd.detect_anomaly to locate the forward operation behind the failing backward.

  3. Reason

    Data and numerical edge cases cause most NaNs and are cheap to check; anomaly detection points to the exact operation instead of guessing.

  4. Boundary

    Any training run producing inf or NaN.

Symptom or risk

Changing several things at once hides the cause; a corrupt batch or a log of zero returns in the next retrain.

Exceptions

Divergence that grows smoothly before NaN is usually an optimization issue; start with the learning rate.

Trade-offs

Anomaly detection slows training noticeably; enable it only for reproduction runs.

Pitfalls

Replacing NaNs with zeros in the loss, which silently trains on broken batches.

Lessons and concepts

Claims and their sources