Forward and backpropagation
Forward propagation computes outputs of a neural network given inputs and parameters; backpropagation uses the chain rule to compute gradients of a loss with respect to parameters so gradient-based optimisers can train the network.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Forward and backpropagation are the core algorithms an engineer uses to run and train neural networks. Forward propagation produces predictions from inputs and current parameters; backpropagation computes gradients of a loss so optimisers (e.g. gradient descent, Adam) can update weights and biases to reduce error.
Key ideas
Bias — a trainable scalar parameter added to the weighted sum. Units: same as pre-activation units. Provides an affine offset.
Pre-activation (z) — the affine combination z = w·x + b (dot product plus bias).
Activation function (φ) — an elementwise nonlinear function applied to z to produce the neuron output y = φ(z). Examples: sigmoid, tanh, ReLU. Define each when used.
Forward propagation — procedure of computing activations layer by layer from input to output using current parameters. Assumption: architecture and parameters fixed.
Loss function (L) — scalar function measuring mismatch between network output and target (label). Example: mean squared error (MSE), cross-entropy. The training objective is to minimise L with respect to parameters.
Gradient — vector of partial derivatives of the loss with respect to parameters: ∇_θ L = (∂L/∂θ_i). It points in direction of steepest increase; negative gradient is direction of steepest decrease.
Chain rule (single-variable and multivariable) — fundamental calculus rule used in backpropagation. Exact statement used in networks: if L depends on a via y and a depends on x, then ∂L/∂x = (∂L/∂y)·(∂y/∂a)·(∂a/∂x). For multivariate nodes, sum over children is required. Assumption: all intermediate functions are differentiable where evaluated.
Backpropagation — algorithm to compute gradients efficiently by applying the chain rule through the computational graph from outputs back to inputs. It caches intermediate activations during forward pass and computes local gradients (partial derivatives) at each node, multiplying and summing as required by the chain rule.
Computational graph — directed acyclic graph showing how variables and functions compose. To draw for a simple single neuron: draw input nodes x1, x2; arrows to a sum node labelled z = Σ w_i x_i + b; arrow from z to activation node y = φ(z); arrow to loss node L = loss(y, t). Label each arrow with the computed value in a forward pass and indicate gradients flowing backward with dashed arrows in reverse.
Local gradient — derivative of a node's output with respect to its input(s). Backprop multiplies local gradients along paths.
Vectorised backprop — use matrix algebra to compute gradients for a whole layer at once. For a layer with input vector x, weight matrix W, bias vector b, pre-activation z = W x + b, and activation a = φ(z): dL/dW = (dL/da) ⊙ φ'(z) outer x (or using matrix forms dL/dW = (δ) x^T), where δ = (dL/da) ⊙ φ'(z). Assumptions: shapes must align and operations are elementwise where noted.
Update rule (gradient descent) — parameters θ are updated by θ ← θ - η ∇_θ L where learning rate (η) is a small positive scalar (unitless) controlling step size.
Practical issues — numerical stability, vanishing/exploding gradients (when derivatives become extremely small or large), and need for careful weight initialisation and normalisation (see other topics like weight initialisation, batch normalisation).
Derivation universities commonly ask for (single-hidden-layer network): derive dL/dW2 and dL/dW1 step by step. Procedure to derive: 1) write forward equations; 2) compute loss derivative w.r.t. output; 3) apply chain rule layer-by-layer; 4) express ∂L/∂W as outer products of local errors and activations. When asked to draw, show layer boxes (input, hidden, output), arrows for W1, W2, label activations and pre-activations, and show δ terms flowing backward.
Formulas
z = w · x + b — pre-activation of a neuron; w is weight vector, x is input vector, b is bias; units: dimensionless or same as x times w; applies per neuron during forward pass.
y = φ(z) — activation; φ is activation function (sigmoid, ReLU, tanh). Units: for sigmoid/tanh y is dimensionless and z must be dimensionless; for ReLU, units equal to z.
L = 1/2 (y - t)^2 — mean squared error for a single-sample single-output case; y and t are dimensionless (target); applies for regression loss example.
∂L/∂y = y - t — derivative of MSE with respect to network output y; units: same as output.
σ(z) = 1 / (1 + e^{-z}) — sigmoid activation; produces values in (0,1).
σ'(z) = σ(z) · (1 - σ(z)) — derivative of sigmoid. For binary cross-entropy L = -[t log y + (1 - t) log(1 - y)] with y = σ(z), the gradient simplifies to ∂L/∂z = y - t. For softmax y = softmax(z) with cross-entropy L = -∑_i t_i log y_i, the output-layer gradient is ∂L/∂z = y - t (vector).
ReLU(z) = max(0, z) — rectified linear unit.
ReLU'(z) = 1 if z > 0 else 0 (undefined at z = 0; use subgradient) — derivative of ReLU.
∂L/∂w = (∂L/∂y) · (dy/dz) · (dz/dw) — chain rule for single weight. Here dz/dw = x (input associated with that weight).
For a layer: z = W x + b, a = φ(z), δ = (∂L/∂a) ⊙ φ'(z), ∂L/∂W = δ x^T, ∂L/∂b = δ — vectorised backprop step. Units: δ has units of dL/dz (inverse of pre-activation units if L is dimensionless); W units: output unit per input unit. For a mini-batch with inputs X (columns are samples) and errors Δ, use ∂L/∂W = Δ X^T and ∂L/∂b = Δ · 1 (sum over batch), optionally averaged by batch size.
Weight update: W ← W - η ∂L/∂W — gradient descent update; η is learning rate (unitless).
If next layer has weights U, then ∂L/∂a = U^T δ_next — how error is propagated backward through layers.
Worked examples
Example 1 — single neuron, sigmoid activation, MSE loss (numerical, step-by-step)
Given: inputs x1 = 1, x2 = 2 (dimensionless); weights w1 = 0.5, w2 = -1.0 (dimensionless); bias b = 0.0; activation φ = sigmoid; target t = 1; learning rate η = 0.1.
Step 1 — forward pass:
z = w1*x1 + w2*x2 + b = 0.5*1 + (-1.0)*2 + 0 = -1.5 (dimensionless)
y = σ(z) = 1 / (1 + e^{1.5}) ≈ 0.1824255 (dimensionless)
L = 1/2 (y - t)^2 = 0.5*(0.1824255 - 1)^2 ≈ 0.334094 (dimensionless)
Step 2 — backward pass (compute gradients):
∂L/∂y = y - t = 0.1824255 - 1 = -0.8175745
σ'(z) = y*(1 - y) = 0.1824255 * 0.8175745 ≈ 0.149146
∂L/∂z = (∂L/∂y) * σ'(z) ≈ -0.8175745 * 0.149146 ≈ -0.12194
∂L/∂w1 = ∂L/∂z * x1 ≈ -0.12194 * 1 = -0.12194
∂L/∂w2 = ∂L/∂z * x2 ≈ -0.12194 * 2 = -0.24388
∂L/∂b = ∂L/∂z ≈ -0.12194
Step 3 — gradient descent update with η = 0.1:
w1_new = w1 - η * ∂L/∂w1 = 0.5 - 0.1 * (-0.12194) = 0.512194 (updated weight)
w2_new = -1.0 - 0.1 * (-0.24388) = -0.975612 (updated weight)
b_new = 0 - 0.1 * (-0.12194) = 0.012194 (updated bias)
Final answers: Forward loss ≈ 0.334094, gradients (∂L/∂w1, ∂L/∂w2) ≈ (-0.12194, -0.24388), updated weights ≈ (0.512194, -0.975612).
Example 2 — small 2-layer network (ReLU hidden, linear output) with manual backprop (numerical)
Network: input x = [1, 2]^T (dimensionless). Hidden layer W1 (2×2) and b1 (2×1): W1 = [[0.5, -1.0],[1.0, 0.5]] (rows = neurons), b1 = [0, 0]. Activation φ1 = ReLU. Output layer W2 = [1.0, -1.0] (1×2), b2 = 0. Output y = W2·h + b2. Target t = 1. Learning rate η = 0.01.
Step 1 — forward pass:
z1 = W1 x + b1 = [0.5*1 + (-1.0)*2, 1.0*1 + 0.5*2] = [-1.5, 2.0]
h = ReLU(z1) = [0, 2]
y = W2 h + b2 = 1.0*0 + (-1.0)*2 + 0 = -2
L = 1/2 (y - t)^2 = 0.5 * (-3)^2 = 4.5
Step 2 — backward pass:
∂L/∂y = y - t = -3
∂L/∂W2 = (∂L/∂y) * h = -3 * [0, 2] = [0, -6]
∂L/∂b2 = -3
dL/dh = W2^T * (∂L/∂y) = [1.0, -1.0]^T * (-3) = [-3, 3]
ReLU'(z1) = [0, 1] because z1 = [-1.5, 2.0]
δ1 = dL/dh ⊙ ReLU'(z1) = [-3, 3] ⊙ [0, 1] = [0, 3]
∂L/∂W1 = δ1 x^T = [[0*1, 0*2], [3*1, 3*2]] = [[0, 0], [3, 6]]
∂L/∂b1 = δ1 = [0, 3]
Step 3 — parameter updates (η = 0.01):
W2_new = W2 - η ∂L/∂W2 = [1.0, -1.0] - 0.01*[0, -6] = [1.0, -0.94]
b2_new = 0 - 0.01*(-3) = 0.03
W1_new = W1 - η ∂L/∂W1 = [[0.5, -1.0],[1.0, 0.5]] - 0.01*[[0,0],[3,6]] = [[0.5, -1.0],[0.97, 0.44]]
b1_new = b1 - η ∂L/∂b1 = [0, 0] - 0.01*[0, 3] = [0, -0.03]
Final answers: Loss = 4.5, ∂L/∂W2 = [0, -6], ∂L/∂W1 = [[0,0],[3,6]], updated W2 ≈ [1.0, -0.94], updated W1 ≈ [[0.5, -1.0],[0.97, 0.44]].
(These are the exact intermediate numbers an examiner expects; show all steps in the same order.)
Common mistakes
- Applying chain rule incorrectly: missing a multiplication by an intermediate derivative (common when moving from ∂L/∂a to ∂L/∂W).
- Shape errors in vectorised forms: mixing row/column vectors; always write shapes and ensure ∂L/∂W has same shape as W.
- Wrong derivative of sigmoid or ReLU (remember σ'(z) = σ(z)(1-σ(z)), ReLU' = 0 for z<0, 1 for z>0).
- Updating parameters with wrong sign (adding gradient instead of subtracting η times gradient).
- Using derivative at ReLU z = 0 without mentioning subgradient—the exam often expects to note it is undefined but a subgradient of 0 or 1 may be used.
- Forgetting to average gradients over a batch if using batch training (MSE per sample vs total loss).
For your exam
Question types you will see about forward/backpropagation:
- Definitions and short notes (define forward pass, backpropagation, computational graph, local gradient).
- Derivations (derive ∂L/∂W for a single neuron; derive backprop equations for a two-layer network). Full-marks answer: show forward equations, compute local derivatives, apply chain rule step-by-step, show final matrix/vector forms with correct shapes.
- Numericals (small networks with given numbers). Full-marks answer: show each forward value, compute loss, show each derivative, and show updated parameters with units and rounding.
- Diagrams (draw computational graph). Full-marks answer: draw nodes for x, z, a, loss; label arrows with forward values and backward arrows with δ values and explicit derivative expressions.
- Programs (rare in theory exam papers) — if asked, write compact vectorised gradient computations (e.g., NumPy expressions) and show sample run on small data.
How to score full marks:
- Always state assumptions (activation differentiable at point or use subgradient for ReLU).
- Show shapes of matrices/vectors when using matrix forms.
- Keep numerical precision consistent and state rounding.
- Explain each chain-rule multiplication and any summation over multiple children in the computational graph.
Quick check
- State the formula for a neuron's pre-activation z.
- What is σ'(z) for the sigmoid function in terms of σ(z)?
- If L = 1/2 (y - t)^2 and y = φ(z), write ∂L/∂w in terms of ∂L/∂y, φ'(z) and input x.
Answers: 1) z = w · x + b. 2) σ'(z) = σ(z) (1 - σ(z)). 3) ∂L/∂w = (∂L/∂y) · φ'(z) · x.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?