Repository navigation
Module 07
⬅️ Module 06 · 🗺️ Curriculum · 🏠 Home · Module 08 ➡️
Level: Intermediate · Prerequisites: Modules 02, 05, 06 · Time: about 8–10 hours
On this page: Overview and learning goals · Beginner: the big picture · Core concepts and derivations · Worked example · Implementation · Advanced topics · Pitfalls and best practices · Conceptual questions and answers · Sources and further reading
A feedforward network (multilayer perceptron, MLP) alternates affine maps and nonlinearities. Backpropagation is reverse-mode autodiff (Module 02) specialised to this layered structure: it computes the gradient of the loss with respect to every weight in time comparable to one forward pass. Everything later — CNNs, RNNs, Transformers — reuses these ideas.
After this module you should be able to:
- write the forward pass in scalar and vectorised batch form and count parameters;
- derive the backprop recursion and the gradients for weights, biases and activations;
- explain how hidden layers solve XOR and pick output layers/losses;
- implement and gradient-check a training loop.
Layer by layer, the network turns an input into progressively more useful hidden representations. Training repeats four steps: forward (compute prediction and loss), backward (compute how each weight affected the loss), update (move weights opposite the gradient), repeat. Backprop is just the chain rule applied from the loss back to the input, reusing intermediate results so each layer's gradient costs about one matrix product.
flowchart LR
X["x"] --> L1["Layer 1: z1=W1x+b1, a1=phi(z1)"] --> L2["Layer 2: z2=W2a1+b2, yhat"] --> Loss["Loss L"]
Loss -. "delta2 = dL/dz2" .-> L2
L2 -. "delta1 = (W2^T delta2) * phi'(z1)" .-> L1
L1 -. "dW1 = delta1 x^T" .-> X
Layer
With
For a batch (rows are examples):
Define the error signal
Interpretation:
For a mini-batch of size
A single unit cannot compute XOR (Module 04). With two or three hidden units the model learns a representation in which the classes are separable; the figure shows a network trained from scratch by exactly the recursion above.

Figure 7.1 — A 2–3–1 tanh network trained with full-batch gradient descent on XOR reaches loss ≈ 0.001 and carves a non-linear decision region. Original figure computed with NumPy backprop (figures/make_figs_1.py).
| Task | Output activation | Loss | |
|---|---|---|---|
| Regression | identity | MSE | |
| Binary | sigmoid | BCE | |
| Multiclass | softmax | CE |
Loop: shuffle → mini-batches → forward → loss → backward → optimiser step → validate. Always gradient-check a new layer (Module 02) before trusting it.
Network
Forward:
Backward:
Check:
import numpy as np
class MLP:
def __init__(self, sizes, seed=0):
r = np.random.default_rng(seed)
self.W = [r.normal(0, np.sqrt(2 / m), (n, m)) for m, n in zip(sizes[:-1], sizes[1:])]
self.b = [np.zeros(n) for n in sizes[1:]]
def forward(self, X):
self.A = [X]
for i, (W, b) in enumerate(zip(self.W, self.b)):
Z = self.A[-1] @ W.T + b
self.A.append(Z if i == len(self.W) - 1 else np.maximum(0, Z))
return self.A[-1]
def backward(self, Y): # MSE loss, mean over batch
B = len(Y); D = (self.A[-1] - Y) / B; gW, gb = [], []
for i in reversed(range(len(self.W))):
gW.insert(0, D.T @ self.A[i]); gb.insert(0, D.sum(0))
if i: D = (D @ self.W[i]) * (self.A[i] > 0)
return gW, gbValidate with num_grad from Module 02: perturb one weight, compare gW. In PyTorch the same loop is loss.backward(); optimizer.step().
- Computational cost: backward ≈ 2× forward FLOPs; activation memory scales with depth × batch.
-
Initialisation and normalisation keep
$\boldsymbol\delta$ well scaled (Module 09). - Skip connections add identity paths to the recursion (Module 09).
- Neural tangent kernel / lazy training: in the infinite-width limit with suitable scaling, training linearises; finite networks learn features.
- Per-example gradients for differential privacy and influence functions.
- Alternatives to backprop (target propagation, forward-forward, Hebbian rules) are research topics motivated by biological plausibility and hardware.
- Forgetting to apply
$\phi'$ or to transpose$W$ in the recursion. - Mixing mean and sum reductions between loss and gradient.
- Initialising all weights equally (symmetry: neurons stay identical) — Module 09.
- Not checking shapes: assert
grad.shape == param.shape. - Comparing training loss only; keep a validation set.
Q1. Why is backprop efficient compared with differentiating each weight separately?
Reverse-mode reuses
Q2. Derive $\boldsymbol\delta^{(\ell)}$ in terms of $\boldsymbol\delta^{(\ell+1)}$.
Q3. Why must weights not all be initialised to the same value?
Neurons in a layer would receive identical gradients and remain identical forever (symmetry), wasting capacity. Random initialisation breaks the symmetry.
Q4. Why does the bias gradient sum over the batch?
The same bias is broadcast to every example, so its total effect is the sum of per-example effects.
Q5. How does a hidden layer solve XOR?
It maps inputs into a feature space (e.g.
Q6. Why is softmax+cross-entropy preferred over softmax+MSE?
CE yields
Q7. What dominates the memory use of backprop?
Stored forward activations
Q8. How would you verify your own backprop implementation?
Compare with central finite differences on a tiny network in float64; expect relative error
- Rumelhart, D. E., Hinton, G. E., & Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323, 533–536.
- LeCun, Y., Bottou, L., Orr, G., & Müller, K.-R. (2012). Efficient BackProp. In Neural Networks: Tricks of the Trade. Springer.
- Goodfellow, Bengio & Courville (2016). Deep Learning, Ch. 6.
- Nielsen, M. (2015). Neural Networks and Deep Learning (online book), Ch. 2.
- Jacot, A., Gabriel, F., & Hongler, C. (2018). Neural tangent kernel. NeurIPS.
⬅️ Module 06 · 🗺️ Curriculum · 🏠 Home · Module 08 ➡️
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths · ❓ Question bank · 📖 Glossary · 📚 References · ✏️ Contributing
🏠 Home · 🗺️ Curriculum · 🧭 Learning paths
❓ Question bank · 📖 Glossary · 📚 References
Jump to module:
01 02 03 04 05 06 07 08 09 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24