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.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 classes, this means the initial loss should be approximately . 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
Debugging Toolkit
Exercises
Exercise 1: Debug a Broken Model
Exercise 1: Debug a Broken Model
Given a model that produces NaN loss, use debugging techniques to find and fix the issue.
Exercise 2: Gradient Flow Analysis
Exercise 2: Gradient Flow Analysis
Implement gradient flow visualization for a deep network. Identify vanishing gradients.
Exercise 3: Loss Landscape
Exercise 3: Loss Landscape
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.