Backpropagation Algorithm: Description with Worked Examples
Backpropagation (often abbreviated backprop) is the standard algorithm used to compute gradients of a neural network’s loss with respect to its parameters (weights and biases). It does so efficiently by applying the chain rule of calculus from the output layer backward through the network layers, enabling gradient-based optimization such as gradient descent.
At a high level, backprop repeats this loop for training examples (or batches):
- Forward pass: compute predictions from inputs through the network.
- Loss computation: evaluate how wrong the predictions are using a loss function.
- Backward pass: compute parameter gradients layer-by-layer using the chain rule.
- Update step: move parameters in the direction that reduces the loss.
Key concepts you should internalize while reading the rest:
- Forward propagation
- Loss function
- Gradient
- Chain rule
- Backpropagation
Backpropagation Explained (Neural Networks)
1) Mathematical setup (what backprop computes)
Consider a fully connected feedforward network with layers. For layer :
- pre-activation (often called “logits” for the last layer):
- activation:
Let the network output be . For a single training example , define a scalar loss:
Backprop’s goal is to compute:
Two additional technical objects are central:
- Error signal
- Activation derivative
In practice, we compute error signals and then use them to get gradients.
2) Core backprop recursion (output layer to input layer)
2.1 Output layer error
For the output layer :
Using the chain rule: where is elementwise (Hadamard) product.
2.2 Hidden layers error
For a hidden layer :
This recurrence captures the “backward flow” of how output error affects earlier layers.
3) Gradients for weights and biases
Once you have , the parameter gradients follow directly:
These formulas are why backprop is computationally efficient: you reuse forward-pass activations and propagate backward.
type="tip" title="Pro Tip" content="Think of as “how much the loss cares” about changing the pre-activations . Once you have , gradients for are just an outer product with the previous activations."
type="warning" title="Warning: Common implementation pitfall" content="For backprop, the derivative must match the chosen activation and loss. E.g., the form of differs for MSE vs. cross-entropy (especially when paired with softmax). Mixing them leads to wrong gradients."
Backpropagation training pass (single example)
- 1Step 1
Compute and until the output .
- 2Step 2
Evaluate .
- 3Step 3
Compute .
- 4Step 4
For down to , compute .
- 5Step 5
For each layer: and .
- 6Step 6
Apply gradient-based update, e.g., and .
4) Worked numeric example (small 2-layer network)
To make backprop concrete, consider a tiny network:
- Inputs:
- Hidden layer (): 2 neurons, activation
- Output layer (): 1 neuron, activation identity (so )
- Loss: mean squared error for a single target :
4.1 Forward pass
Compute hidden pre-activations:
\begin{bmatrix} 0.1 & 0.2\\ 0.4 & 0.3 \end{bmatrix} \begin{bmatrix}1\\2\end{bmatrix} + \begin{bmatrix}0.0\\0.1\end{bmatrix} = \begin{bmatrix} 0.1(1)+0.2(2)\\ 0.4(1)+0.3(2) \end{bmatrix} + \begin{bmatrix}0.0\\0.1\end{bmatrix} = \begin{bmatrix}0.5\\1.1\end{bmatrix}$$ Hidden activations: $$a^{(1)}=\sigma(z^{(1)})= \begin{bmatrix}\sigma(0.5)\\\sigma(1.1)\end{bmatrix}Approximate numerically:
So:
Output pre-activation (and output since identity activation):
\begin{bmatrix}0.5 & 0.6\end{bmatrix} \begin{bmatrix}0.62246\\0.75026\end{bmatrix} \approx 0.31123+0.45016=0.76139$$ Thus $\hat{y}=0.76139$. Loss: $$J=\frac{1}{2}(0.76139-1)^2=\frac{1}{2}(-0.23861)^2\approx \frac{1}{2}(0.05694)\approx 0.02847$$ ### 4.2 Backward pass Output layer error: Since $J=\frac{1}{2}(a^{(2)}-y)^2$, we have: $$\frac{\partial J}{\partial a^{(2)}} = (a^{(2)}-y) = (0.76139-1)=-0.23861$$ and with identity activation $f(z)=z$, $f'(z)=1$, so: $$\delta^{(2)}=\frac{\partial J}{\partial z^{(2)}}=-0.23861$$ Gradients for output weights: $$\frac{\partial J}{\partial W^{(2)}}=\delta^{(2)}(a^{(1)})^{T} =-0.23861\begin{bmatrix}0.62246 & 0.75026\end{bmatrix} \approx \begin{bmatrix}-0.14848 & -0.17895\end{bmatrix}$$ Gradient for output bias: $$\frac{\partial J}{\partial b^{(2)}}=\delta^{(2)}=-0.23861$$ Hidden layer error: $$\delta^{(1)}=\left(W^{(2)}\right)^T\delta^{(2)}\odot \sigma'(z^{(1)})$$ First compute $(W^{(2)})^T\delta^{(2)}$: $$\left(W^{(2)}\right)^T\delta^{(2)}= \begin{bmatrix}0.5\\0.6\end{bmatrix}(-0.23861) \approx \begin{bmatrix}-0.11931\\-0.14317\end{bmatrix}$$ For sigmoid, $\sigma'(z)=\sigma(z)(1-\sigma(z))$: - $\sigma'(0.5)\approx 0.62246(1-0.62246)=0.23500$ - $\sigma'(1.1)\approx 0.75026(1-0.75026)=0.18745$ So: $$\delta^{(1)}\approx \begin{bmatrix}-0.11931\\-0.14317\end{bmatrix}\odot \begin{bmatrix}0.23500\\0.18745\end{bmatrix} = \begin{bmatrix}-0.02803\\-0.02682\end{bmatrix}$$ Gradients for hidden weights: $$\frac{\partial J}{\partial W^{(1)}}=\delta^{(1)}(a^{(0)})^T = \begin{bmatrix}-0.02803\\-0.02682\end{bmatrix} \begin{bmatrix}1 & 2\end{bmatrix} = \begin{bmatrix} -0.02803 & -0.05606\\ -0.02682 & -0.05364 \end{bmatrix}$$ Hidden bias gradients: $$\frac{\partial J}{\partial b^{(1)}}=\delta^{(1)}\approx \begin{bmatrix}-0.02803\\-0.02682\end{bmatrix}$$ This completes one full backprop iteration with explicit numbers.5) Pseudocode and mini-batch training
For mini-batch size , backprop computes gradients per example and then averages (or sums) across the batch before updating parameters. Conceptually:
Mini-batch gradient descent Stochastic gradient descent (SGD)
Backpropagation in practice (training lifecycle)
Initialize parameters
Step 0Choose starting weights and biases ."
Repeat over batches
Step 1Run forward pass, compute loss, run backward pass."
Gradient computation
Step 2Compute all , then and ."
Parameter update
Step 3Apply optimizer update (SGD, Momentum, Adam, etc.)."
Stop criterion
Step 4Stop when validation loss plateaus or after a fixed number of epochs."
6) Conceptual example: why “reverse mode” matters
If you attempted to compute all derivatives by perturbing parameters (numerical differentiation), you’d need many evaluations of the loss. Backprop instead computes gradients in one forward + one backward pass by reusing intermediate values from the forward pass (stored activations and pre-activations).
This is why backprop scales efficiently with network depth and number of parameters: the backward pass essentially applies the chain rule in reverse.
Common variants and edge cases
Knowledge Check
In backpropagation, the error signal for a hidden layer is computed using which general relationship?