Optimizing deep networks
The nonconvex loss surface of a deep net, warm-up and cosine schedules, AdamW, gradient clipping, the batch-size rule, SAM/SWA and mixed precision — closing the training module.
From scale to the step
The topic Normalization and initialization made a deep network trainable: signals and gradients keep their scale through depth. Whether training then goes fast and ends somewhere good is up to the optimizer. The Machine Learning course derived SGD, momentum, RMSProp and Adam on problems with one valley. This topic is about what changes when they drive a deep network: many equivalent minima, a best learning rate that moves during training, weight decay meeting normalization, gradients that occasionally blow up, and large batches.
Notation. Parameters , loss , minibatch gradient ; learning rate (or ), weight decay , batch size , horizon . Adam averages with , .
The loss surface
Permuting hidden units leaves the function unchanged. A net has about parameter vectors per function — so the loss is not convex (the midpoint of two permuted exact fits of by two ReLUs has loss and gradient : a saddle).
Critical points at high loss are typically saddles with flat plateaus, not bad local minima. Sharpness of a minimum is not intrinsic for ReLUs: has the same function at and but Hessian eigenvalues vs . Flat-minima stories are empirical heuristics, yet they motivate SAM and SWA below.
Schedules and warm-up
Default schedule for many vision and Transformer runs: linear warm-up over steps, then cosine decay
Why warm-up helps Adam. Bias correction makes the first step in every coordinate. Under pure noise the steady-state mean absolute step is about ; the first steps are ~5.5× larger. A post-norm residual net on digits with Adam at peak and cosine decay stays at loss without warm-up; with 200 warm-up steps it reaches ~0.98 validation accuracy. Other reasons (variance of , high initial curvature, post-norm gradient spikes) stack rather than exclude each other.
AdamW and normalization
Under SGD, L2 and weight decay coincide. Under Adam they differ:
AdamW shrinks every weight by the same factor. With zero data gradient, , , 1000 steps: AdamW multiplies both and by ; Adam+L2 drives the small weight through zero and leaves the large one at ~0.26.
On weights followed by normalization the function ignores ; decay only sets the rotation speed of the direction. At equilibrium SGD turns by about radians per step.
Clipping and batch size
Gradient clipping by global norm keeps direction and caps length: . For and one gets ; value clipping to would give and change direction. Place clip_grad_norm_ between backward() and step() (after unscaling in mixed precision).
Batch size. If gradients change slowly, steps of batch with rate match one step of batch with rate (linear scaling for SGD). For Adam under noise-dominated gradients the match is . Both need warm-up early on; both break for very large . On a small digit MLP, batch 16 at reached val 0.971; batch 256 at the same rate only 0.788; rate recovered 0.965.
Flat minima and precision
SAM minimizes the worst loss in a ball of radius : approximate and step with — two forward/backward passes. SWA averages late SGD iterates; recompute BatchNorm stats for the average.
Mixed precision. fp16 compute with an fp32 master copy of the weights; multiply the loss by a scale before backward so tiny gradients survive, then unscale in fp32. Dynamic scaling halves on inf/nan and grows it after clean runs. bf16 has fp32 range, so loss scaling is usually unnecessary; the master copy still is.
Closing the training module
| One-valley recipe | Deep network | |
|---|---|---|
| Loss surface | one minimum | huge symmetry copies, saddles, sharp/flat basins |
| First Adam steps | full | warm-up, or a post-norm net may stick at |
| Weight decay | L2 = decay under SGD | AdamW outside the adaptive scale |
| Outliers | taken raw | clip by global norm |
| Larger batch | same | (SGD) or (Adam) |
Watch train and validation curves: a safe learning rate with oscillating loss often means forgotten zero_grad or an oversized step; a flat train loss at means scale/init or missing warm-up.
This closes the Training neural networks module. Fully connected layers still waste parameters on images: the next topic, Convolution, shares weights across positions and builds the layer every later vision note stacks.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Adam vs SGD on a quadratic valleyoptional
Convergence comparison on L=½(w₁²+100 w₂²). Self-check in the notebook.
~15 min · easier on a computer · Optimizing deep networks topic read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.