Deep learning
Training neural networks well
Optimizers, schedules, initialization, normalization, regularization and a practical recipe for debugging loss curves.
A correct model definition is maybe 20% of getting a neural net to work. The rest is optimization: learning rates, initialization, normalization, regularization and a disciplined debugging loop. This page is the craft layer on top of gradient descent and the PyTorch training loop.
Ship a boring stack: AdamW plus warmup and cosine, LayerNorm or RMSNorm residuals, and weight decay. Spend budget on data quality and eval, not exotic optimizers. When pretraining at scale, the same knobs appear with bigger numbers. See pretraining and scaling laws.
Concepts in this section
Read them in order. Each one builds on the one before.
| Step | Concept | What it answers |
|---|---|---|
| 1 | BatchNorm, LayerNorm and RMSNorm | How do you keep activations on a usable scale? |
| 2 | Debugging loss curves | What should loss be at step 0, and how do you read a plot? |
| 3 | Gradient clipping and accumulation | How do you survive spikes and fake a larger batch? |
| 4 | Regularization for neural nets | Why AdamW, and which regularizers actually get used? |
Related pages already cover pieces of this craft: momentum and Adam, warmup and schedules, initialization and residual connections.
The learning rate is usually the single most important hyperparameter. Warmup stops early updates from destroying randomly initialized features. Cosine decay is standard for long runs. If you invent a deep architecture, default to residual plus norm.
PyTorch nn.Linear already uses reasonable Kaiming-style defaults. Do not randomly re-init unless you know why. For a balanced K-way classifier, loss at step 0 should be about ln K (2.3 for 10 classes, about 10.8 for a 50k vocab). If you see 15 or 0.01, something is wrong before you blame Adam.
Keep a training diary: git commit hash, config YAML, dataset revision, best checkpoint metric. Reproducibility is a process, not a seed.
Interview angle: explain AdamW versus SGD, why warmup exists, what ln K checks, diagnose a curve that diverges versus overfits, and how grad accumulation interacts with BatchNorm.
Common mistakes
- Blaming the optimizer before you overfit one batch. If one batch will not go to ~0, the bug is in code, loss or shapes.
- Skipping
model.train()/eval(). BatchNorm and dropout then silently misbehave. - One learning rate on a frozen backbone plus a new head. The head underfits or the backbone wrecks.
Key takeaways
- AdamW is the default modern optimizer. Remember 2× moment memory.
- Warmup plus cosine beats a constant LR for long runs.
- Init so forward variance is stable. Verify loss ≈
ln(num_classes)at step 0. - LayerNorm or RMSNorm for transformers. BatchNorm still common in CNNs.
- Residuals make depth trainable. Weight decay and data beat clever tricks.
- Overfit one batch first, then read train/val curves systematically.
Further learning
Karpathy's recipe is short and worth re-reading before every new training run.
A Recipe for Training Neural NetworksMusings of a Computer Scientist.karpathy.github.io Decoupled Weight Decay Regularization (AdamW)L$_2$ regularization and weight decay regularization are equivalent for standard stochastic gradient descent (when rescaled by the learning rate), but as we demonstrate this is \emph{not} the case for adaptive gradien…arXiv.org
Stanford lecture on training nets — solid on init, BatchNorm and schedules:
Related
Optimization and gradient descent
How models learn, from loss landscapes and gradient descent to learning rates, mini-batches, momentum and Adam.
PyTorch fundamentals
Tensors, autograd, modules, data loaders and a clean training loop. The daily-driver API for deep learning. Start here for the map.
Pretraining a language model
Data, objective and compute: how a base model is trained on trillions of tokens.