Level 3 — ProductionTraining Deep Networks

Training Deep Networks

45 minutes60 marksprintable — key stays hidden on paper

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 θ\theta, given gradient gtg_t, hyperparameters β1,β2,α,ϵ\beta_1, \beta_2, \alpha, \epsilon. Include the bias-correction step. (4)

(b) Explain why bias correction is needed by deriving E[mt]\mathbb{E}[m_t] in terms of E[gt]\mathbb{E}[g_t], assuming stationary gradients with E[gi]=E[g]\mathbb{E}[g_i]=\mathbb{E}[g]. Show it equals (1β1t)E[g](1-\beta_1^t)\,\mathbb{E}[g]. (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 vt=μvt1+gtv_t = \mu v_{t-1} + g_t with constant gt=gg_t=g, derive the steady-state value vv_\infty 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 {x1,,xm}\{x_1,\dots,x_m\} (mean, variance, normalize, scale-shift). Give all equations. (4)

(b) Derive the gradient Lxi\frac{\partial \mathcal{L}}{\partial x_i} of the loss w.r.t. an input, given Lx^i\frac{\partial \mathcal{L}}{\partial \hat{x}_i}. Express it in terms of x^i\hat{x}_i, σ2\sigma^2, and mm. (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 ηt\eta_t over TT steps between ηmax\eta_{max} and ηmin\eta_{min}. (3)

(b) A schedule uses linear warmup for the first ww steps from 00 to ηmax\eta_{max}, then cosine decay to ηmin\eta_{min} over the remaining TwT-w steps. Write ηt\eta_t 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 pp 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 λ2θ2\frac{\lambda}{2}\|\theta\|^2 to the loss yields the SGD update θ(1ηλ)θηg\theta \leftarrow (1-\eta\lambda)\theta - \eta g. 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 mtm_t, vtv_t, bias-correction pair, final update) mt=β1mt1+(1β1)gtm_t = \beta_1 m_{t-1} + (1-\beta_1) g_t vt=β2vt1+(1β2)gt2v_t = \beta_2 v_{t-1} + (1-\beta_2) g_t^2 m^t=mt1β1t,v^t=vt1β2t\hat{m}_t = \frac{m_t}{1-\beta_1^t}, \qquad \hat{v}_t = \frac{v_t}{1-\beta_2^t} θt=θt1αm^tv^t+ϵ\theta_t = \theta_{t-1} - \alpha \frac{\hat{m}_t}{\sqrt{\hat{v}_t}+\epsilon}

(b) (4 marks) Unrolling with m0=0m_0=0: mt=(1β1)i=1tβ1tigi.m_t = (1-\beta_1)\sum_{i=1}^{t}\beta_1^{t-i} g_i. Taking expectation with E[gi]=E[g]\mathbb{E}[g_i]=\mathbb{E}[g]: E[mt]=(1β1)E[g]i=1tβ1ti=(1β1)E[g]1β1t1β1=(1β1t)E[g].\mathbb{E}[m_t] = (1-\beta_1)\,\mathbb{E}[g]\sum_{i=1}^{t}\beta_1^{t-i} = (1-\beta_1)\mathbb{E}[g]\cdot\frac{1-\beta_1^{t}}{1-\beta_1} = (1-\beta_1^{t})\mathbb{E}[g]. Since mtm_t is biased toward 0 (factor 1β1t<11-\beta_1^t <1), dividing by (1β1t)(1-\beta_1^t) corrects it. (2 for unroll+geometric sum, 2 for result & reasoning)

(c) (4 marks) In Adam-with-L2, the penalty enters the gradient (gtgt+λθg_t \leftarrow g_t + \lambda\theta), so decay is scaled by the adaptive 1/v^1/\sqrt{\hat v} — parameters with large historical gradients get less effective decay. AdamW decouples decay: it applies θθαλθ\theta \leftarrow \theta - \alpha\lambda\theta directly to the weights, outside the adaptive step: θt=θt1α(m^tv^t+ϵ+λθt1).\theta_t = \theta_{t-1} - \alpha\Big(\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon} + \lambda\theta_{t-1}\Big). This makes decay uniform/independent of gradient magnitudes → better generalization.

Question 2 (10)

(a) (4) Momentum: vt=μvt1+gt,θt=θt1ηvt.v_t = \mu v_{t-1} + g_t,\qquad \theta_t = \theta_{t-1} - \eta v_t. Nesterov: vt=μvt1+θf(θt1ημvt1),θt=θt1ηvt.v_t = \mu v_{t-1} + \nabla_\theta f(\theta_{t-1} - \eta\mu v_{t-1}),\qquad \theta_t=\theta_{t-1}-\eta v_t. (gradient evaluated at the look-ahead point).

(b) (4) Steady state v=μv+gv=g1μv_\infty = \mu v_\infty + g \Rightarrow v_\infty = \dfrac{g}{1-\mu}. Effective step is ηv=ηg1μ\eta v_\infty = \dfrac{\eta g}{1-\mu}, i.e. a multiplier of 11μ\dfrac{1}{1-\mu} vs plain SGD (ηg\eta g). For μ=0.9\mu=0.910×10\times.

(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=1mixi,σB2=1mi(xiμB)2,\mu_B=\frac1m\sum_i x_i,\quad \sigma_B^2=\frac1m\sum_i(x_i-\mu_B)^2, x^i=xiμBσB2+ϵ,yi=γx^i+β.\hat x_i=\frac{x_i-\mu_B}{\sqrt{\sigma_B^2+\epsilon}},\quad y_i=\gamma\hat x_i+\beta.

(b) (6) With σ2σB2+ϵ\sigma^2\equiv\sigma_B^2+\epsilon, standard result: Lxi=1mσ2(mLx^ijLx^jx^ijLx^jx^j).\frac{\partial\mathcal L}{\partial x_i}=\frac{1}{m\sqrt{\sigma^2}}\left(m\frac{\partial\mathcal L}{\partial\hat x_i}-\sum_{j}\frac{\partial\mathcal L}{\partial\hat x_j}-\hat x_i\sum_j\frac{\partial\mathcal L}{\partial\hat x_j}\hat x_j\right). (3 for identifying the three paths xix^i, μ, σ2x_i\to\hat x_i,\ \to\mu,\ \to\sigma^2; 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) ηt=ηmin+12(ηmaxηmin)(1+cos(tTπ)).\eta_t=\eta_{min}+\tfrac12(\eta_{max}-\eta_{min})\Big(1+\cos\big(\tfrac{t}{T}\pi\big)\Big).

(b) (4) ηt={twηmax,twηmin+12(ηmaxηmin)(1+cos(twTwπ)),t>w.\eta_t=\begin{cases}\dfrac{t}{w}\,\eta_{max}, & t\le w\\[2mm]\eta_{min}+\tfrac12(\eta_{max}-\eta_{min})\Big(1+\cos\big(\tfrac{t-w}{T-w}\pi\big)\Big), & t>w.\end{cases}

(c) (3) Early in training, adaptive optimizers have unreliable variance estimates (v^\hat v) 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 pp, and divide surviving activations by pp (scale factor 1/p1/p), so expected activation is preserved; at test time no scaling/masking is applied.

(b) (3) Loss L=L+λ2θ2L'=L+\frac{\lambda}{2}\|\theta\|^2, so θL=g+λθ\nabla_\theta L'=g+\lambda\theta. Update: θθη(g+λθ)=(1ηλ)θηg.\theta\leftarrow\theta-\eta(g+\lambda\theta)=(1-\eta\lambda)\theta-\eta g.

(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)"}
]