Skip to main content
Debugging Deep Learning

Debugging Deep Learning

Common Training Failures

Deep learning debugging is notoriously difficult — unlike a segfault that points you to the offending line, a neural network fails silently. Your training loop runs without errors, the loss decreases smoothly, and three days later you discover the model has learned something completely useless. There are no compiler warnings for “your labels are accidentally shuffled” or “your learning rate is 1000x too high.” An analogy: Debugging a neural network is like diagnosing a sick patient who cannot tell you their symptoms. You have to run tests (sanity checks), look at vital signs (gradient norms, loss curves), and use process of elimination. The best debuggers are not the ones who can read error messages — they are the ones who have a systematic checklist of things to verify before they ever start training.

Gradient Health Checks

Monitor Gradient Norms

Visualize Gradient Flow


Detecting NaN/Inf


Sanity Checks

1. Overfit a Single Batch

This is the single most important debugging technique in deep learning. Before training on the full dataset, verify that your model can memorize a single batch of data to near-perfect accuracy. If it cannot, something is fundamentally broken — a bug in the model architecture, the loss function, the data pipeline, or the optimizer. Do not waste hours training on the full dataset until this test passes. Think of it as a smoke test: if the car will not start in the driveway, do not take it on the highway.
If this test fails, check in this order: (1) Are the labels correct? (print a few and verify visually), (2) Are input dimensions correct? (print shapes at each layer), (3) Is the loss function appropriate for your task? (e.g., using BCE for multi-class instead of cross-entropy), (4) Is the learning rate too low? (try 1e-2 or even 1e-1).

2. Check Data Pipeline

3. Verify Loss at Initialization

A randomly initialized classifier should assign roughly equal probability to all classes. For cross-entropy loss with KK classes, this means the initial loss should be approximately log(1/K)=log(K)-\log(1/K) = \log(K). For CIFAR-10 (10 classes), expect ~2.30. For ImageNet (1000 classes), expect ~6.91. If your initial loss is significantly different, something is wrong with the model or the loss function.
Why this matters: If initial loss is 0.1 when it should be 2.3, your model is already “confident” before seeing any data — usually meaning the final layer bias is accidentally initialized to favor certain classes. If initial loss is 15.0 when it should be 6.9, the logits are likely unnormalized or the loss function is wrong.

Loss Landscape Visualization


Common Fixes

The number one debugging rule: Change one thing at a time. If you simultaneously increase the learning rate, add dropout, and change the architecture, you will never know which change had what effect. Disciplined, isolated experiments save more time than they cost.

Debugging Toolkit


Exercises

Given a model that produces NaN loss, use debugging techniques to find and fix the issue.
Implement gradient flow visualization for a deep network. Identify vanishing gradients.
Generate loss landscape visualizations for networks with and without batch normalization.

What’s Next

Module 23: Vision Transformers

ViT, DeiT, Swin Transformer, and modern vision architectures.