Skip to content
BIASED

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 ww, loss L(w)L(w), minibatch gradient gg; learning rate η\eta (or ηt\eta_t), weight decay λ\lambda, batch size bb, horizon TT. Adam averages m^,v^\hat m,\hat v with β1=0.9\beta_1=0.9, β2=0.999\beta_2=0.999.

The loss surface

Permuting hidden units leaves the function unchanged. A 64→256→256→1064\to 256\to 256\to 10 net has about 256!2≈101014256!^2\approx 10^{1014} parameter vectors per function — so the loss is not convex (the midpoint of two permuted exact fits of y=xy=x by two ReLUs has loss 0.50.5 and gradient 00: 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: L=12(w1w2−1)2L=\tfrac12(w_1 w_2-1)^2 has the same function at (1,1)(1,1) and (10,0.1)(10,0.1) but Hessian eigenvalues 22 vs 100100. 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 ww steps, then cosine decay

ηt=ηmax⁡⋅min⁡ ⁣(1,t+1w)⋅12(1+cos⁡πtT).\eta_t=\eta_{\max}\cdot\min\!\Big(1,\frac{t+1}{w}\Big)\cdot\frac12\Big(1+\cos\frac{\pi t}{T}\Big).

Why warm-up helps Adam. Bias correction makes the first step ηsign⁡(g1)\eta\operatorname{sign}(g_1) in every coordinate. Under pure noise the steady-state mean absolute step is about 0.183 η0.183\,\eta; the first steps are ~5.5× larger. A post-norm residual net on digits with Adam at peak 10−210^{-2} and cosine decay stays at loss 2.3032.303 without warm-up; with 200 warm-up steps it reaches ~0.98 validation accuracy. Other reasons (variance of 1/v^1/\sqrt{\hat v}, 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:

Adam+L2:w←w−η m^/(v^+ϵ) with g=∇L+λw,AdamW:w←(1−ηλ) w−η m^/(v^+ϵ).\begin{aligned} \text{Adam+L2:}&\quad w\leftarrow w-\eta\,\hat m/(\sqrt{\hat v}+\epsilon)\ \text{with }g=\nabla L+\lambda w,\\ \text{AdamW:}&\quad w\leftarrow(1-\eta\lambda)\,w-\eta\,\hat m/(\sqrt{\hat v}+\epsilon). \end{aligned}

AdamW shrinks every weight by the same factor. With zero data gradient, η=10−3\eta=10^{-3}, λ=10−2\lambda=10^{-2}, 1000 steps: AdamW multiplies both 11 and 0.010.01 by 0.9900.990; Adam+L2 drives the small weight through zero and leaves the large one at ~0.26.

On weights followed by normalization the function ignores ∥w∥\|w\|; decay only sets the rotation speed of the direction. At equilibrium SGD turns by about 2ηλ\sqrt{2\eta\lambda} radians per step.

Clipping and batch size

Gradient clipping by global norm keeps direction and caps length: g←g⋅min⁡(1,c/∥g∥)g\leftarrow g\cdot\min(1,c/\|g\|). For g=(3,4)g=(3,4) and c=1c=1 one gets (0.6,0.8)(0.6,0.8); value clipping to [−1,1][-1,1] would give (1,1)(1,1) and change direction. Place clip_grad_norm_ between backward() and step() (after unscaling in mixed precision).

Batch size. If gradients change slowly, kk steps of batch bb with rate η\eta match one step of batch kbkb with rate kηk\eta (linear scaling for SGD). For Adam under noise-dominated gradients the match is k\sqrt{k}. Both need warm-up early on; both break for very large kk. On a small digit MLP, batch 16 at η=0.01\eta=0.01 reached val 0.971; batch 256 at the same rate only 0.788; ×16\times 16 rate recovered 0.965.

Flat minima and precision

SAM minimizes the worst loss in a ball of radius ρ\rho: approximate ε^=ρ∇L/∥∇L∥\hat\varepsilon=\rho\nabla L/\|\nabla L\| and step with ∇L(w+ε^)\nabla L(w+\hat\varepsilon) — 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 SS before backward so tiny gradients survive, then unscale in fp32. Dynamic scaling halves SS 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 η\eta warm-up, or a post-norm net may stick at 2.3032.303
Weight decay L2 = decay under SGD AdamW outside the adaptive scale
Outliers taken raw clip by global norm
Larger batch same η\eta ×k\times k (SGD) or ×k\times\sqrt{k} (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 ln⁡K\ln K 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.

Full notes

Answers are saved on the server and tied to this browser until you sign in.