Skip to content
Lambdian

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 (x2x^2, 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 z\mathbf{z} and activations a\mathbf{a}. At the output, the error signal is δ=y^−y\boldsymbol\delta = \hat{\mathbf{y}} - \mathbf{y}. 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 ±ϵ\pm\epsilon and compare J(w+ϵ)−J(w−ϵ)2ϵ\frac{J(w+\epsilon)-J(w-\epsilon)}{2\epsilon} 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

  1. 1
    Initialise weights randomly (He/Xavier scale) and biases to zero.
  2. 2
    For each mini-batch: forward pass, storing z(l)\mathbf{z}^{(l)} and a(l)\mathbf{a}^{(l)}.
  3. 3
    Compute the loss and the output error δ(L)\boldsymbol\delta^{(L)}.
  4. 4
    Backward pass: δ(l)=(W(l+1)⊤δ(l+1))⊙g′(z(l))\boldsymbol\delta^{(l)} = (W^{(l+1)\top}\boldsymbol\delta^{(l+1)})\odot g'(\mathbf{z}^{(l)}); gradients δ(l)a(l−1)⊤\boldsymbol\delta^{(l)}\mathbf{a}^{(l-1)\top}.
  5. 5
    Update all weights with the optimiser; repeat over epochs; stop early on validation loss.

The maths

Layer

z(l)=W(l)a(l−1)+b(l),a(l)=g(z(l))\mathbf{z}^{(l)} = W^{(l)}\mathbf{a}^{(l-1)} + \mathbf{b}^{(l)},\quad \mathbf{a}^{(l)} = g(\mathbf{z}^{(l)})

Backpropagated error

δ(l)=(W(l+1)⊤δ(l+1))⊙g′(z(l))\boldsymbol\delta^{(l)} = \big(W^{(l+1)\top}\boldsymbol\delta^{(l+1)}\big)\odot g'(\mathbf{z}^{(l)})

Weight gradient (batch of n)

∇W(l)J=1n∑iδi(l)ai(l−1)⊤\nabla_{W^{(l)}}J = \frac1n\sum_i\boldsymbol\delta_i^{(l)}\mathbf{a}_i^{(l-1)\top}

Gradient check

∂J∂w≈J(w+ϵ)−J(w−ϵ)2ϵ\frac{\partial J}{\partial w} \approx \frac{J(w+\epsilon) - J(w-\epsilon)}{2\epsilon}

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…

Large datasets with complex structure, especially perception (images, audio) and language; or when features are hard to hand-craft.

Check your understanding

Question 1

Q1Why do we need non-linear activations in hidden layers?

Question 2

Q2If all weights start at exactly zero in an MLP…

Question 3

Q3Backpropagation computes…

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.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.