Training Deep Networks
Level 4 — Application (novel problems, no hints) Time limit: 60 minutes Total marks: 60
Answer all questions. Show all working. Use notation for mathematics. Where numerical answers are requested, give at least 4 significant figures unless otherwise stated.
Question 1 — Adam update trace (14 marks)
You are training a single parameter with the Adam optimizer using hyperparameters , , , . The parameter starts at , and the first two observed gradients are and .
(a) Compute the bias-corrected first and second moment estimates after step 1, and give the resulting . (5)
(b) Compute (uncorrected), then , and give . (5)
(c) A colleague proposes switching to AdamW with decoupled weight decay . Write the modified update rule for and explain in one sentence why AdamW's decay differs in effect from adding an L2 term to the loss when used with Adam. (4)
Question 2 — Designing a learning-rate schedule (12 marks)
A transformer is trained for steps. You must design a schedule with a linear warmup for the first steps up to a peak learning rate , followed by cosine decay down to a floor of .
(a) Write closed-form expressions for both phases. (4)
(b) Compute and . (4)
(c) Explain why warmup is especially important when using Adam with large batch sizes, referencing the behaviour of the second-moment estimate early in training. (4)
Question 3 — Batch norm at inference (12 marks)
A batch-norm layer receives, during a training mini-batch, the pre-activation values for one feature channel: Learned parameters are , . Use and the biased variance (divide by ).
(a) Compute the batch mean, variance, and the normalized+scaled outputs . (6)
(b) The running statistics accumulated so far are , , updated with momentum via . Compute the updated running mean and variance. (3)
(c) State one concrete failure mode of batch norm that motivates using layer norm instead, and name a setting where this matters. (3)
Question 4 — Regularization and dropout scaling (12 marks)
(a) A hidden layer has 5 units with pre-scaling activations . Using inverted dropout with keep probability , and a dropout mask that keeps units and drops (1-indexed), compute the output vector. (4)
(b) Explain why inverted dropout rescales by at train time, and what this guarantees at inference. (3)
(c) You observe training loss still decreasing while validation loss has risen for 6 consecutive epochs (patience = 5). Best validation loss was at epoch 12. State the action early stopping takes and which weights are deployed. (2)
(d) Compare the effect of L1 vs L2 weight decay on the resulting weight distribution, and state which you'd prefer if you want an interpretable, sparse feature selector. (3)
Question 5 — Gradient clipping & optimizer choice (10 marks)
During training you record the raw gradient vector .
(a) Apply global-norm gradient clipping with threshold . Give the clipped gradient. (4)
(b) With learning rate and plain SGD, give the parameter update using the clipped gradient. (2)
(c) A recurrent network exhibits occasional loss spikes to NaN. Explain mechanistically how gradient clipping mitigates this, and state one hyperparameter interaction between clipping threshold and learning rate. (4)
End of paper.
Answer keyMark scheme & solutions
Question 1 (14)
(a) Step 1, .
- ; . (1)
- Bias correction: . (1)
- . (1)
- Update: . (2)
Why: bias correction removes the initialization-toward-zero bias; step 1 the corrected moments equal the raw gradient/gradient², so the effective step is .
(b) Step 2, .
- . (1)
- . (1)
- . (1)
- . (1)
- . (1)
(c) AdamW: adaptive step plus decoupled decay: With : decay term , so . (2) Why (1 sentence, 2): L2 added to the loss enters the gradient and is then divided by (so decay is scaled per-parameter and coupled to gradient magnitude), whereas AdamW applies decay directly to the weight independent of the adaptive denominator, giving uniform, predictable shrinkage.
Question 2 (12)
(a)
- Warmup (): . (2)
- Cosine (): . (2)
(b)
- . (2)
- : progress . , so . (2)
(c) Early in training the second-moment estimate is built from few, high-variance gradient samples, so it can be small/unreliable → the adaptive step can be huge and unstable, worse with large batches (fewer steps, sharper early curvature); warmup keeps small until stabilizes, preventing early divergence. (4)
Question 3 (12)
(a)
- Mean . (1)
- Deviations: ; squares ; sum ; biased var ; . (2)
- Normalized : . (1)
- : . (2)
(b)
- . (1.5)
- . (1.5)
(c) Failure mode: batch norm's statistics depend on batch composition, so with very small batch sizes (or batch size 1) estimates are noisy/unreliable, and it couples examples in a batch — problematic for sequence models / variable-length data. Layer norm normalizes over features per-example, independent of batch → preferred in Transformers / RNNs. (3)
Question 4 (12)
(a) Inverted dropout: kept units scaled by ; dropped units set to 0. Keep → . (4)
(b) Rescaling by at train time keeps the expected activation equal to the no-dropout value (since a unit survives with prob , ). This guarantees that at inference dropout can simply be turned off (identity) with no rescaling needed, keeping train/test activation magnitudes consistent. (3)
(c) Val loss rose for 6 epochs > patience 5 → early stopping triggers, training halts; the deployed weights are those from epoch 12 (best validation loss), restored via checkpointing. (2)
(d) L1 () drives many weights exactly to zero → sparse solutions; L2 () shrinks all weights smoothly toward zero without exact zeros. For an interpretable sparse feature selector prefer L1. (3)
Question 5 (10)
(a) , so clip: . (4)
(b) . (2)
(c) Loss spikes/NaNs arise when exploding gradients (common in RNNs due to repeated Jacobian products) produce huge update steps that overshoot to unstable regions/overflow. Clipping caps the update norm, guaranteeing bounded steps regardless of a single anomalous gradient, preserving direction. Interaction: a lower clipping threshold has a similar stabilizing effect to a lower learning rate, so threshold and must be tuned together — too tight a clip with a large still allows the scaled step to be large per component only up to , but an overly small slows convergence like an over-shrunk lr. (4)
[
{"claim":"Adam w1 = -0.1 (step 1)","code":"a=Rational(1,10); m1=0.1*2; v1=0.001*4; mh=m1/0.1; vh=v1/0.001; w1=0 - float(a)*mh/(sqrt(vh)+1e-8); result = abs(w1 - (-0.1)) < 1e-6"},
{"claim":"Adam w2 approx -0.126634","code":"m1=0.2; v1=0.004; m2=0.9*m1+0.1*(-1); v2=0.999*v1+0.001*1; mh=m2/(1-0.9**2); vh=v2/(1-0.999**2); w1=-0.1; w2=w1-0.1*mh/(sqrt(vh)+1e-8); result = abs(float(w2)-(-0.126634))<1e-4"},
{"claim":"cosine schedule eta(5250)=2.75e-4","code":"emin=5e-5; emax=5e-4; prog=(5250-500)/(10000-500); eta=emin+0.5*(emax-emin)*(1+cos(pi*prog)); result = abs(float(eta)-2.75e-4)<1e-9"},
{"claim":"BN running var update = 3.2","code":"v=0.9*3.0+0.1*5; result = abs(v-3.2)<1e-12"},
{"claim":"gradient clip [6,8,0] to [3,4,0]","code":"import sympy as sp; g=sp.Matrix([6,8,0]); n=sp.sqrt(g.dot(g)); gc=g*(5/n); result = gc == sp.Matrix([3,4,0])"}
]