Skip to main content

Backpropagation

Source: Unit 2 §8

Backpropagation iteratively compares the network's prediction to the actual target and modifies the weights to minimise the loss - working backwards from the output layer, through each hidden layer, to the first hidden layer.

inputhidden 1hidden 2outputFORWARD - compute ŷvalues flow input → outputBACKWARD - adjust the weightserror flows output → hidden → first hiddenloss E(ŷ, t)starts the backward pass
Two passes over the same wires: values travel forwards to make a prediction, blame travels backwards to change the weights.
FactsThe shape of one training step
  • For each training tuple, the weights are modified to minimise the loss between the prediction and the target.
  • The modifications flow output → hidden → … → first hidden, which is where the "back" in backpropagation comes from.

The loss function

For a single output unit, the error as a function of the weight vector is:

E(w)=12d(tdod)2E(\vec{w}) = \frac{1}{2} \sum_d (t_d - o_d)^2

For multiple output units kk, sum over the outputs as well:

E(w)=12dk(tkdokd)2E(\vec{w}) = \frac{1}{2} \sum_d \sum_k (t_{kd} - o_{kd})^2

The stochastic approximation updates per example rather than summing over the whole training set first.

The chain-rule derivation

Take a simple chain of single neurons and look at the last layer LL. We want to know how sensitive the cost EE is to each weight, that is E/w(L)\partial E / \partial w^{(L)}. A tiny nudge in w(L)w^{(L)} nudges z(L)z^{(L)}, which nudges a(L)a^{(L)}, which changes EE. Chain them together:

Ew(L)=z(L)w(L)a(L)z(L)Ea(L)\frac{\partial E}{\partial w^{(L)}} = \frac{\partial z^{(L)}}{\partial w^{(L)}} \cdot \frac{\partial a^{(L)}}{\partial z^{(L)}} \cdot \frac{\partial E}{\partial a^{(L)}}
w(L)a(L−1)previous activationz(L) = w(L)·a(L−1) + b(L)weighted suma(L) = σ(z(L))activationE(a(L) − y)²∂z(L)/∂w(L) = a(L−1)∂a(L)/∂z(L) = σ'(z(L))∂E/∂a(L) = 2(a(L) − y)∂E/∂w(L) = a(L−1) · σ'(z(L)) · 2(a(L) − y)
Each arrow owns one derivative. Multiplying the three of them is the entire chain rule for the last layer.
TermComes fromValue
∂E/∂a(L)E = (a(L) − y)²**2(a(L) − y)**
∂a(L)/∂z(L)a(L) = σ(z(L))**σ'(z(L))**
∂z(L)/∂w(L)z = w·a(L−1) + b**a(L−1)**

Multiplying the three:

Ew(L)=a(L1)σ(z(L))2(a(L)y)\frac{\partial E}{\partial w^{(L)}} = a^{(L-1)} \cdot \sigma'(z^{(L)}) \cdot 2(a^{(L)} - y)
FactsThree consequences of that one line
  • The bias derivative is almost identical. Replace z/w\partial z / \partial w with z/b\partial z / \partial b, which is 1, giving E/b(L)=σ(z(L))2(a(L)y)\partial E / \partial b^{(L)} = \sigma'(z^{(L)}) \cdot 2(a^{(L)} - y).
  • Generalise over nn examples by averaging the per-example gradients.
  • Keep iterating the chain rule backwards to get the sensitivity to the earlier weights w(L1)w^{(L-1)}, w(L2)w^{(L-2)} and so on.

Full multi-neuron notation

With many neurons per layer, the indices arrive:

FactsReading the superscripts and subscripts
  • aj(L)a_j^{(L)} is the activation of the j-th neuron in layer LL; ak(L1)a_k^{(L-1)} is the k-th neuron in layer L1L-1.
  • WjkLW_{jk}^{L} is the weight on the edge connecting the k-th neuron in layer L1L-1 to the j-th neuron in layer LL.
  • The cost for a multi-output layer is E=j(aj(L)yj)2E = \sum_j (a_j^{(L)} - y_j)^2.
a0(L−1)a1(L−1)a2(L−1)a0(L)a1(L)Elayer L − 1layer Ltwo paths from a1(L−1) to E∂E/∂a1(L−1) = sum over both
One earlier neuron feeds every later neuron, so its share of the blame is a sum over all the paths it took to reach the cost.
GotchaA neuron influences the cost through more than one path

A neuron in layer L1L-1 feeds every neuron in layer LL, so it affects the cost by several routes at once. Its E/a\partial E / \partial a is therefore a sum over those paths, not a single term. Forgetting the sum is the classic backprop derivation error.

These chain-rule expressions give the derivatives that form each component of the gradient, which is then used to minimise the cost by repeatedly stepping downhill - ordinary gradient descent, applied to every weight in the network at once.

Derivative of the sigmoid

Backpropagation needs σ\sigma' at every layer, and the sigmoid has a particularly convenient one:

σ(x)=11+exσ(x)=σ(x)(1σ(x))\sigma(x) = \frac{1}{1 + e^{-x}} \qquad \Longrightarrow \qquad \sigma'(x) = \sigma(x)\,(1 - \sigma(x))

Derived with the quotient rule. The same exercise for tanh\tanh gives:

tanh(x)=1tanh2(x)\tanh'(x) = 1 - \tanh^2(x)
Exam cueBoth derivatives are written in terms of the function itself

σ=σ(1σ)\sigma' = \sigma(1-\sigma) and tanh=1tanh2\tanh' = 1 - \tanh^2. During the backward pass the forward activations are already stored, so neither derivative costs a fresh exponential.

Overfitting in backpropagation

The situation is analogous to the decision-tree case. With many weights and many iterations, backpropagation can overfit - tuning the weights to idiosyncrasies of the training data that are not representative of the general distribution.

errortraining iterationskeep these weightsleast validation errortraining errorvalidation errorrising = overfitting has started
Training error never stops falling, so it cannot tell you when to stop. The validation minimum can, and the weights at that point are the ones to keep.
StepsThe validation-set fix
  1. Maintain a separate validation set, apart from both the training and the test sets.
  2. Periodically measure accuracy on the validation set while training continues.
  3. Choose the weights that gave the least validation-set error, not the ones training ended on.