Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 16 — Lab: Training Deep Networks

Open in Colab

Lab Overview

You will write, in NumPy, the pieces that make deep networks trainable: the optimisers’ updates, an initialisation, batch normalisation, dropout, gradient clipping, and a monitor that reads a loss curve.

ExerciseTopicPoints
1A momentum step10
2An Adam step20
3He initialisation10
4Batch normalisation, training and evaluation20
5Inverted dropout15
6Clipping by the global norm10
7Reading a loss curve15
ChallengeWarm-up and cosine decay+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Use NumPy only. Do not change the check cells.


Exercise 1: A Momentum Step (10 points)

momentum_step(w, g, v, lr, mu) updates the velocity, v←μv+gv \leftarrow \mu v + g, then the weights, w←w−ηvw \leftarrow w - \eta v, and returns (w, v). Do not modify the arrays you are given: return new ones.


Exercise 2: An Adam Step (20 points)

adam_step(w, g, m, v, t, lr, b1, b2, eps) is step number t (counted from 1) of Adam:

m←β1m+(1−β1)g,v←β2v+(1−β2)g2,m^=m1−β1t,v^=v1−β2t,w←w−ηm^v^+ϵm \leftarrow \beta_1 m + (1-\beta_1) g, \quad v \leftarrow \beta_2 v + (1-\beta_2) g^2, \quad \hat m = \frac{m}{1-\beta_1^t}, \quad \hat v = \frac{v}{1-\beta_2^t}, \quad w \leftarrow w - \eta \frac{\hat m}{\sqrt{\hat v}+\epsilon}

It returns (w, m, v), with the uncorrected m and v (the next step needs them).


Exercise 3: He Initialisation (10 points)

he_init(n_in, n_out, rng) returns an (n_in, n_out) weight matrix drawn from a normal distribution with mean 0 and standard deviation 2/nin\sqrt{2 / n_{\text{in}}}.


Exercise 4: Batch Normalisation (20 points)

batchnorm_train(x, gamma, beta, eps) normalises each column of the batch x (shape (n, d)) with that column’s mean and variance over the batch (the variance divides by nn), then returns gamma * x_hat + beta together with the batch’s mean and var.

batchnorm_eval(x, gamma, beta, mean, var, eps) does the same with the given (running) mean and var.


Exercise 5: Inverted Dropout (15 points)

dropout(x, p, rng, train): in training, keep each value with probability 1−p1-p (draw rng.random(x.shape) >= p) and divide the kept values by 1−p1-p; return (out, mask) where mask is the 0/1 array of kept values. In evaluation, return (x, None) unchanged.


Exercise 6: Clipping by the Global Norm (10 points)

clip_by_norm(grads, c) takes a list of gradient arrays. Their global norm is ∑∥gi∥2\sqrt{\sum \|g_i\|^2} over all arrays. If it exceeds c, scale every array by c / norm; otherwise return them unchanged. Return (clipped_list, norm).


Exercise 7: Reading a Loss Curve (15 points)

diagnose(losses, n_classes) takes the training loss after each epoch and returns one word, checking in this order:

  1. 'diverged' if any value is not finite, or the last value is more than twice the first;

  2. 'stuck' if every value is within 0.02 of ln⁡(n_classes)\ln(\text{n\_classes});

  3. 'unstable' if the loss went up from one epoch to the next more than a third of the time;

  4. 'converging' otherwise.


Challenge: Warm-Up and Cosine Decay (+10 points)

lr_at(t, base, warm, total) gives the learning rate at step t (from 0): linear warm-up base⋅t/warm\text{base} \cdot t / \text{warm} for t<warmt < \text{warm}, then cosine decay 12base(1+cos⁡(π(t−warm)/(total−warm)))\tfrac12 \text{base} (1 + \cos(\pi (t - \text{warm}) / (\text{total} - \text{warm}))).


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: in Exercise 2, why does the first Adam step have size lr whatever the gradient? What would happen to the first steps without the bias correction?