Training Deep Networks
Chapter: 3.2 Training Deep Networks Level: 3 — Production (derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60
Instructions: Show all derivations. Where code is asked, pseudocode/NumPy from memory is acceptable but must be correct in logic. Use for math.
Question 1 — Optimizer update rules from scratch (12 marks)
(a) Write the full Adam update equations for a single parameter , given gradient , hyperparameters . Include the bias-correction step. (4)
(b) Explain why bias correction is needed by deriving in terms of , assuming stationary gradients with . Show it equals . (4)
(c) State precisely how AdamW differs from Adam-with-L2-regularization in where the weight-decay term enters the update, and why this matters. (4)
Question 2 — Momentum & Nesterov (10 marks)
(a) Write the standard (heavy-ball) momentum update and the Nesterov accelerated gradient (NAG) update. (4)
(b) For a quadratic loss with the effective learning rate held fixed, momentum behaves like an exponential moving average of gradients. If with constant , derive the steady-state value and the effective step multiplier relative to plain SGD. (4)
(c) In one sentence, explain the key conceptual difference between momentum and Nesterov ("look-ahead"). (2)
Question 3 — Batch Normalization forward & backward (14 marks)
(a) Write the BatchNorm forward pass over a mini-batch (mean, variance, normalize, scale-shift). Give all equations. (4)
(b) Derive the gradient of the loss w.r.t. an input, given . Express it in terms of , , and . (6)
(c) State two concrete differences between BatchNorm and LayerNorm (axis of normalization, and behaviour at inference / dependence on batch). (4)
Question 4 — Learning-rate schedule & warmup (10 marks)
(a) Write the cosine annealing schedule formula for learning rate over steps between and . (3)
(b) A schedule uses linear warmup for the first steps from to , then cosine decay to over the remaining steps. Write as a piecewise function. (4)
(c) Explain in one or two sentences why warmup helps when training with adaptive optimizers / large batches. (3)
Question 5 — Regularization reasoning (8 marks)
(a) Dropout with keep-probability is applied at training time. Describe the inverted dropout trick and compute the scaling factor applied to activations at train time. (3)
(b) Show that adding L2 penalty to the loss yields the SGD update . Derive it. (3)
(c) State the early-stopping criterion (what quantity is monitored and the stopping rule with "patience"). (2)
Question 6 — Gradient clipping code-from-memory (6 marks)
Write NumPy-style pseudocode for global-norm gradient clipping given a list of gradient arrays grads and a threshold clip_norm. State the rescaling rule and when it triggers. (6)
Answer keyMark scheme & solutions
Question 1 (12)
(a) (4 marks: 1 each for , , bias-correction pair, final update)
(b) (4 marks) Unrolling with : Taking expectation with : Since is biased toward 0 (factor ), dividing by corrects it. (2 for unroll+geometric sum, 2 for result & reasoning)
(c) (4 marks) In Adam-with-L2, the penalty enters the gradient (), so decay is scaled by the adaptive — parameters with large historical gradients get less effective decay. AdamW decouples decay: it applies directly to the weights, outside the adaptive step: This makes decay uniform/independent of gradient magnitudes → better generalization.
Question 2 (10)
(a) (4) Momentum: Nesterov: (gradient evaluated at the look-ahead point).
(b) (4) Steady state . Effective step is , i.e. a multiplier of vs plain SGD (). For → .
(c) (2) Momentum uses the gradient at the current point; Nesterov evaluates the gradient at the anticipated future position, giving a correction that reduces overshoot.
Question 3 (14)
(a) (4)
(b) (6) With , standard result: (3 for identifying the three paths ; 3 for correct assembled form.)
(c) (4) (i) BN normalizes across the batch dimension (per feature over the batch); LN normalizes across the feature dimension per single example. (ii) BN's statistics depend on batch → uses running averages at inference and is unstable for small batches; LN is batch-independent, identical at train/inference (ideal for RNNs/Transformers).
Question 4 (10)
(a) (3)
(b) (4)
(c) (3) Early in training, adaptive optimizers have unreliable variance estimates () and large-batch gradients can cause huge steps; warmup keeps steps small until statistics stabilize, preventing divergence.
Question 5 (8)
(a) (3) Inverted dropout: at train time keep each unit with prob , and divide surviving activations by (scale factor ), so expected activation is preserved; at test time no scaling/masking is applied.
(b) (3) Loss , so . Update:
(c) (2) Monitor validation loss (or metric); stop when it fails to improve for patience consecutive evaluations, restoring the best checkpoint.
Question 6 (6)
(6: 2 for norm computation, 2 for conditional scale, 2 for correct rescale)
total_norm = np.sqrt(sum(np.sum(g**2) for g in grads))
if total_norm > clip_norm:
scale = clip_norm / (total_norm + 1e-6)
grads = [g * scale for g in grads]Trigger: only when global norm exceeds clip_norm; rescale all grads by clip_norm/total_norm so the clipped global norm equals clip_norm, preserving direction.
[
{"claim":"E[m_t] = (1-beta1**t)*E[g] via geometric sum", "code":"b1=symbols('b1'); t=4; s=summation(b1**(t-i),(i,1,t)); result = simplify((1-b1)*s - (1-b1**t))==0"},
{"claim":"Momentum steady-state effective multiplier is 1/(1-mu); mu=0.9 -> 10", "code":"mu=Rational(9,10); result = simplify(1/(1-mu))==10"},
{"claim":"L2 SGD update gives (1-eta*lambda) coefficient on theta", "code":"eta,lam,th,g=symbols('eta lam th g'); upd=th-eta*(g+lam*th); result = simplify(upd-((1-eta*lam)*th-eta*g))==0"},
{"claim":"Cosine schedule at t=0 gives eta_max and t=T gives eta_min", "code":"emax,emin=symbols('emax emin'); f=lambda t,T: emin+Rational(1,2)*(emax-emin)*(1+cos(t/T*pi)); result = (simplify(f(0,1)-emax)==0) and (simplify(f(1,1)-emin)==0)"}
]