Equations · Rumelhart, Hinton and Williams, “Learning representations by back-propagating errors” (1986)

Backpropagation for one layer: the chain rule, explained

Given the gradient of the loss with respect to a layer's output, the chain rule gives the gradients for its weights, its bias and its input. The last one is passed to the layer below.

z=Wx+b,a=σ(z)δ=∂L∂z=∂L∂a⊙σ′(z)∂L∂W=δ x⊤,∂L∂b=δ,∂L∂x=W⊤δ\begin{aligned} \mathbf{z} &= \mathbf{W}\mathbf{x} + \mathbf{b}, \quad \mathbf{a} = \sigma(\mathbf{z}) \\ \boldsymbol{\delta} &= \frac{\partial \mathcal{L}}{\partial \mathbf{z}} = \frac{\partial \mathcal{L}}{\partial \mathbf{a}} \odot \sigma'(\mathbf{z}) \\ \frac{\partial \mathcal{L}}{\partial \mathbf{W}} &= \boldsymbol{\delta}\, \mathbf{x}^\top, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{b}} = \boldsymbol{\delta}, \quad \frac{\partial \mathcal{L}}{\partial \mathbf{x}} = \mathbf{W}^\top \boldsymbol{\delta} \end{aligned}

Open in the decoder →

Term by term

  1. z=Wx+b\mathbf{z} = \mathbf{W}\mathbf{x} + \mathbf{b}

    The forward pass: weights times input plus bias, the pre-activation.

  2. a=σ(z)\mathbf{a} = \sigma(\mathbf{z})

    The activation, applied elementwise. σ\sigma here is a function (often the sigmoid).

  3. δ=∂L∂z\boldsymbol{\delta} = \frac{\partial \mathcal{L}}{\partial \mathbf{z}}

    The error signal: the loss's gradient with respect to the pre-activation.

  4. ∂L∂a⊙σ′(z)\frac{\partial \mathcal{L}}{\partial \mathbf{a}} \odot \sigma'(\mathbf{z})

    The chain rule through an elementwise function is an elementwise product with its derivative σ′\sigma'.

  5. δ x⊤\boldsymbol{\delta}\, \mathbf{x}^\top

    An outer product: entry (i,j)(i, j) is δixj\delta_i x_j, so the weight gradient has W\mathbf{W}'s shape.

  6. W⊤δ\mathbf{W}^\top \boldsymbol{\delta}

    The gradient sent back to the input, through the transposed weights.

Shapes

A small worked example

One weight: w=0.5w = 0.5, x=2x = 2, b=0b = 0, sigmoid, loss 12(a−y)2\frac{1}{2}(a - y)^2 with y=1y = 1. Forward: z=1z = 1, a=σ(1)≈0.731a = \sigma(1) \approx 0.731. Backward: ∂L∂a=a−y≈−0.269\frac{\partial \mathcal{L}}{\partial a} = a - y \approx -0.269; σ′(z)=a(1−a)≈0.197\sigma'(z) = a(1 - a) \approx 0.197; δ≈−0.053\delta \approx -0.053; ∂L∂w=δx≈−0.106\frac{\partial \mathcal{L}}{\partial w} = \delta x \approx -0.106. The negative gradient says: increase ww.

In code

import numpy as np

def dense_backward(W, x, z, dL_da, dsigma):
    delta = dL_da * dsigma(z)        # (m,)
    dW = np.outer(delta, x)          # (m, n)
    db = delta                       # (m,)
    dx = W.T @ delta                 # (n,)
    return dW, db, dx

Every symbol in it

W∈Rm×n\mathbf{W} \in \mathbb{R}^{m \times n}Bold uppercase (matrix)x∈Rd\mathbf{x} \in \mathbb{R}^{d}Bold lowercase (vector)σ(z)\sigma(z)Sigma (lowercase)δ\deltaDelta (lowercase)∂L∂w\frac{\partial \mathcal{L}}{\partial w}Partial derivativeL(θ)\mathcal{L}(\theta)Loss (ℒ)a⊙b\mathbf{a} \odot \mathbf{b}Hadamard (elementwise) productf′(x)f'(x)Primex⊤y\mathbf{x}^\top \mathbf{y}Transpose

Other equations