Level 4 — ApplicationTraining Deep Networks

Training Deep Networks

60 minutes60 marksprintable — key stays hidden on paper

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 ww with the Adam optimizer using hyperparameters α=0.1\alpha = 0.1, β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ϵ=108\epsilon = 10^{-8}. The parameter starts at w0=0w_0 = 0, and the first two observed gradients are g1=2g_1 = 2 and g2=1g_2 = -1.

(a) Compute the bias-corrected first and second moment estimates m^1,v^1\hat m_1, \hat v_1 after step 1, and give the resulting w1w_1. (5)

(b) Compute m2,v2m_2, v_2 (uncorrected), then m^2,v^2\hat m_2, \hat v_2, and give w2w_2. (5)

(c) A colleague proposes switching to AdamW with decoupled weight decay λ=0.01\lambda = 0.01. Write the modified update rule for w2w_2 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 T=10,000T = 10{,}000 steps. You must design a schedule with a linear warmup for the first W=500W = 500 steps up to a peak learning rate ηmax=5×104\eta_{\max} = 5\times10^{-4}, followed by cosine decay down to a floor of ηmin=5×105\eta_{\min} = 5\times10^{-5}.

(a) Write closed-form expressions η(t)\eta(t) for both phases. (4)

(b) Compute η(250)\eta(250) and η(5250)\eta(5250). (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: x=[4,  8,  6,  2].x = [4,\; 8,\; 6,\; 2]. Learned parameters are γ=2\gamma = 2, β=1\beta = 1. Use ϵ=0\epsilon = 0 and the biased variance (divide by NN).

(a) Compute the batch mean, variance, and the normalized+scaled outputs yiy_i. (6)

(b) The running statistics accumulated so far are μrun=5.5\mu_{\text{run}} = 5.5, σrun2=3.0\sigma^2_{\text{run}} = 3.0, updated with momentum ρ=0.9\rho = 0.9 via μρμrun+(1ρ)μbatch\mu \leftarrow \rho\,\mu_{\text{run}} + (1-\rho)\mu_{\text{batch}}. 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 a=[1.0,0.5,2.0,1.0,0.0]a = [1.0, 0.5, 2.0, -1.0, 0.0]. Using inverted dropout with keep probability p=0.8p = 0.8, and a dropout mask that keeps units {1,3,4}\{1,3,4\} and drops {2,5}\{2,5\} (1-indexed), compute the output vector. (4)

(b) Explain why inverted dropout rescales by 1/p1/p 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 g=[6,8,0]g = [6, 8, 0].

(a) Apply global-norm gradient clipping with threshold c=5c = 5. Give the clipped gradient. (4)

(b) With learning rate η=0.1\eta = 0.1 and plain SGD, give the parameter update Δθ\Delta\theta 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, g1=2g_1 = 2.

  • m1=0.9(0)+0.1(2)=0.2m_1 = 0.9(0) + 0.1(2) = 0.2; v1=0.999(0)+0.001(4)=0.004v_1 = 0.999(0) + 0.001(4) = 0.004. (1)
  • Bias correction: m^1=m1/(1β11)=0.2/0.1=2.0\hat m_1 = m_1/(1-\beta_1^1) = 0.2/0.1 = 2.0. (1)
  • v^1=v1/(1β21)=0.004/0.001=4.0\hat v_1 = v_1/(1-\beta_2^1) = 0.004/0.001 = 4.0. (1)
  • Update: w1=w0αm^1/(v^1+ϵ)=00.12/(2+108)0.1w_1 = w_0 - \alpha\,\hat m_1/(\sqrt{\hat v_1}+\epsilon) = 0 - 0.1\cdot 2/( 2+10^{-8}) \approx -0.1. (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 αsign(g)-\alpha\cdot\text{sign}(g).

(b) Step 2, g2=1g_2 = -1.

  • m2=0.9(0.2)+0.1(1)=0.08m_2 = 0.9(0.2) + 0.1(-1) = 0.08. (1)
  • v2=0.999(0.004)+0.001(1)=0.003996+0.001=0.004996v_2 = 0.999(0.004) + 0.001(1) = 0.003996 + 0.001 = 0.004996. (1)
  • m^2=0.08/(10.92)=0.08/0.19=0.421053\hat m_2 = 0.08/(1-0.9^2) = 0.08/0.19 = 0.421053. (1)
  • v^2=0.004996/(10.9992)=0.004996/0.001999=2.499250\hat v_2 = 0.004996/(1-0.999^2) = 0.004996/0.001999 = 2.499250. (1)
  • w2=w10.10.421053/(2.499250+108)=0.10.10.421053/1.5809010.10.026634=0.126634w_2 = w_1 - 0.1\cdot 0.421053/(\sqrt{2.499250}+10^{-8}) = -0.1 - 0.1\cdot0.421053/1.580901 \approx -0.1 - 0.026634 = -0.126634. (1)

(c) AdamW: adaptive step plus decoupled decay: w2=w1α(m^2v^2+ϵ+λw1)  or  =w1αm^2v^2+ϵαλw1.w_2 = w_1 - \alpha\Big(\tfrac{\hat m_2}{\sqrt{\hat v_2}+\epsilon} + \lambda w_1\Big)\;\text{or}\;= w_1 - \alpha\tfrac{\hat m_2}{\sqrt{\hat v_2}+\epsilon} - \alpha\lambda w_1. With w1=0.1w_1=-0.1: decay term αλw1=0.10.01(0.1)=+104-\alpha\lambda w_1 = -0.1\cdot0.01\cdot(-0.1) = +10^{-4}, so w20.126534w_2 \approx -0.126534. (2) Why (1 sentence, 2): L2 added to the loss enters the gradient and is then divided by v^\sqrt{\hat v} (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 (tWt\le W): η(t)=ηmaxt/W\eta(t) = \eta_{\max}\cdot t/W. (2)
  • Cosine (W<tTW<t\le T): η(t)=ηmin+12(ηmaxηmin)(1+cos(πtWTW))\eta(t) = \eta_{\min} + \tfrac12(\eta_{\max}-\eta_{\min})\big(1+\cos(\pi\,\tfrac{t-W}{T-W})\big). (2)

(b)

  • η(250)=5×104250/500=2.5×104\eta(250) = 5\times10^{-4}\cdot 250/500 = 2.5\times10^{-4}. (2)
  • η(5250)\eta(5250): progress =(5250500)/(10000500)=4750/9500=0.5= (5250-500)/(10000-500) = 4750/9500 = 0.5. cos(π0.5)=0\cos(\pi\cdot0.5)=0, so η=ηmin+12(ηmaxηmin)(1+0)=5×105+0.5(4.5×104)=5×105+2.25×104=2.75×104\eta = \eta_{\min} + \tfrac12(\eta_{\max}-\eta_{\min})(1+0) = 5\times10^{-5} + 0.5(4.5\times10^{-4}) = 5\times10^{-5}+2.25\times10^{-4} = 2.75\times10^{-4}. (2)

(c) Early in training the second-moment estimate v^\hat v is built from few, high-variance gradient samples, so it can be small/unreliable → the adaptive step m^/v^\hat m/\sqrt{\hat v} can be huge and unstable, worse with large batches (fewer steps, sharper early curvature); warmup keeps η\eta small until v^\hat v stabilizes, preventing early divergence. (4)


Question 3 (12)

(a)

  • Mean μ=(4+8+6+2)/4=5\mu = (4+8+6+2)/4 = 5. (1)
  • Deviations: 1,3,1,3-1,3,1,-3; squares 1,9,1,91,9,1,9; sum =20=20; biased var =20/4=5= 20/4 = 5; σ=52.23607\sigma = \sqrt5 \approx 2.23607. (2)
  • Normalized x^i=(xi5)/5\hat x_i = (x_i-5)/\sqrt5: [0.44721,1.34164,0.44721,1.34164][-0.44721, 1.34164, 0.44721, -1.34164]. (1)
  • yi=γx^i+β=2x^i+1y_i = \gamma\hat x_i + \beta = 2\hat x_i + 1: [0.10557,3.68328,1.89443,1.68328][0.10557, 3.68328, 1.89443, -1.68328]. (2)

(b)

  • μnew=0.9(5.5)+0.1(5)=4.95+0.5=5.45\mu_{\text{new}} = 0.9(5.5)+0.1(5) = 4.95+0.5 = 5.45. (1.5)
  • σnew2=0.9(3.0)+0.1(5)=2.7+0.5=3.2\sigma^2_{\text{new}} = 0.9(3.0)+0.1(5) = 2.7+0.5 = 3.2. (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 1/p=1/0.8=1.251/p = 1/0.8 = 1.25; dropped units set to 0. Keep {1,3,4}\{1,3,4\}[1.01.25,  0,  2.01.25,  1.01.25,  0]=[1.25,  0,  2.5,  1.25,  0][1.0\cdot1.25,\;0,\;2.0\cdot1.25,\;-1.0\cdot1.25,\;0] = [1.25,\;0,\;2.5,\;-1.25,\;0]. (4)

(b) Rescaling by 1/p1/p at train time keeps the expected activation equal to the no-dropout value (since a unit survives with prob pp, E[output]=p(a/p)=a\mathbb E[\text{output}] = p\cdot(a/p) = a). 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 (λw1\lambda\|w\|_1) drives many weights exactly to zero → sparse solutions; L2 (λw22\lambda\|w\|_2^2) shrinks all weights smoothly toward zero without exact zeros. For an interpretable sparse feature selector prefer L1. (3)


Question 5 (10)

(a) g2=36+64+0=100=10>5\|g\|_2 = \sqrt{36+64+0} = \sqrt{100} = 10 > 5, so clip: g=g(c/g)=g0.5=[3,4,0]g' = g\cdot(c/\|g\|) = g\cdot0.5 = [3,4,0]. (4)

(b) Δθ=ηg=0.1[3,4,0]=[0.3,0.4,0]\Delta\theta = -\eta g' = -0.1\cdot[3,4,0] = [-0.3,-0.4,0]. (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 η\eta must be tuned together — too tight a clip with a large η\eta still allows the scaled step to be large per component only up to cc, but an overly small cc 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])"}
]