Neural networks and backpropagation
Multi-layer perceptrons: learned features, activations, the chain rule, and training tricks.
Animated explanation
The idea
Logistic regression draws one straight boundary. To bend it, we used hand-made features (, kernels). A neural network learns the features itself: a hidden layer of units, each a little logistic-regression-like detector, computes new features; the next layer combines those; the output layer makes the prediction.
With enough hidden units, even one hidden layer can approximate any reasonable function (the universal approximation theorem). Depth makes it efficient: layers build features of features, edges → shapes → objects.
Training is gradient descent. The gradients for millions of weights are computed in one backward sweep by backpropagation, which is just the chain rule applied layer by layer, reusing intermediate results.
Backpropagation in words
Run the network forward and keep every layer's pre-activations and activations . At the output, the error signal is . Each layer's error is the next layer's error sent back through the transposed weights and multiplied by the activation's slope. A weight's gradient is "error at its output × activation at its input". The cost of the backward pass is about the same as the forward pass, which is why deep learning is practical.
Always verify a hand-written backward pass with a numerical gradient check: perturb a weight by and compare with your gradient.
Making training work
- Initialisation: small random weights scaled to the layer size (Xavier for tanh, He for ReLU). All-zero weights make every unit identical forever.
- Optimiser: mini-batch SGD with momentum, or Adam.
- Normalisation: standardise inputs; batch or layer normalisation inside deep networks.
- Regularisation: weight decay (L2), dropout (randomly silence units while training), early stopping, data augmentation.
- Vanishing gradients: saturating activations shrink gradients layer after layer; ReLU and residual connections fix most of it.
The algorithm, step by step
- 1Initialise weights randomly (He/Xavier scale) and biases to zero.
- 2For each mini-batch: forward pass, storing and .
- 3Compute the loss and the output error .
- 4Backward pass: ; gradients .
- 5Update all weights with the optimiser; repeat over epochs; stop early on validation loss.
The maths
Layer
Backpropagated error
Weight gradient (batch of n)
Gradient check
Strengths and weaknesses
Strengths
- Learns its own features; extremely flexible
- Scales to huge data with GPUs and mini-batches
- One framework for images, text, audio and tabular data
Weaknesses
- Needs lots of data and tuning
- Non-convex: local minima, sensitive to initialisation
- Hard to interpret; usually beaten by boosting on small tabular data
Reach for it when…
Check your understanding
Lambdian Lab
A two-layer network from scratch
Binary classification with one tanh hidden layer and a sigmoid output, mean log-loss.
- init(d, h, seed): W1 (d × h), b1, w2 (h), b2 with W1 ~ N(0, 1/d) and w2 ~ N(0, 1/h).
- forward(params, X) → (p, cache).
- backward(params, cache, y) → gradients with the same keys as params.
- numeric_grad(params, X, y, key, i, eps): central difference for one entry (flat index i) of one parameter.
- train(X, y, h, lr, epochs, seed): full-batch gradient descent; return params.