4.3  Backpropagation

In Section 4.2, we trained all \(203{,}530\) parameters of our Fashion-MNIST MLP without writing a single derivative by hand: automatic differentiation computed every gradient for us. In Section 1.5, we saw the general principle—reverse-mode automatic differentiation records the forward pass as a computational graph and walks the chain rule backward from the scalar loss.

In this section, we open that black box for multilayer perceptrons. An entire MLP requires just three local matrix-calculus rules: matrix multiplication, broadcasted bias addition, and elementwise activation. Walking through them answers three essential engineering questions:

  1. How does a single backward pass compute hundreds of thousands of partial derivatives for no more than about \(2\times\) the arithmetic cost of one forward pass?
  2. Why does training consume so much more GPU memory than inference, and why does that memory scale linearly with minibatch size \(n\) and depth \(K\)?
  3. How do dead ReLU units and vanishing/exploding gradients arise mechanically from the backward recurrence?

4.3.1 Computational Graphs

4.3.1.1 A Small Graph

To review reverse-mode differentiation in its barest form, consider the three-scalar expression \(e = (a + b)\,c\) at \(a = 2, b = 1, c = -3\) (Figure 4.3.1). The forward pass (left to right) computes \(u = a + b = 3\) and \(e = u\,c = -9\). The backward pass (right to left) seeds \(\partial e / \partial e = 1\) and applies one local derivative per node:

  • At the multiplication node \(e = u\,c\): \(\partial e / \partial u = c = -3\) and \(\partial e / \partial c = u = 3\).
  • At the addition node \(u = a + b\): \(\partial u / \partial a = \partial u / \partial b = 1\), so \(\partial e / \partial a = \partial e / \partial b = -3\).
Figure 4.3.1: Computational graph of \(e = (a + b)\,c\) at \(a = 2, b = 1, c = -3\), showing forward values (black) and reverse-mode derivatives \(\partial e / \partial (\cdot)\) (red).

Notice two universal rules already visible here: (i) an addition node copies its incoming gradient unchanged to each summand, whereas a multiplication node multiplies the incoming gradient by the other factor (requiring the forward pass to cache \(u\) and \(c\)); and (ii) the backward pass traverses the graph in reverse topological order.

4.3.1.2 The Graph of an MLP

Now consider our one-hidden-layer MLP (Figure 4.3.2) on a minibatch \(\mathbf{X} \in \mathbb{R}^{n \times d}\) with parameters \(\mathbf{W}^{(1)} \in \mathbb{R}^{d \times h}\), \(\mathbf{b}^{(1)} \in \mathbb{R}^h\), \(\mathbf{W}^{(2)} \in \mathbb{R}^{h \times q}\), \(\mathbf{b}^{(2)} \in \mathbb{R}^q\), and \(\ell_2\) weight-decay penalty \(s = \frac{\lambda}{2}(\|\mathbf{W}^{(1)}\|_{\textrm{F}}^2 + \|\mathbf{W}^{(2)}\|_{\textrm{F}}^2)\):

\[ \begin{aligned} \mathbf{Z} &= \mathbf{X}\mathbf{W}^{(1)} + \mathbf{b}^{(1)}, \\ \mathbf{H} &= \phi(\mathbf{Z}), \\ \mathbf{O} &= \mathbf{H}\mathbf{W}^{(2)} + \mathbf{b}^{(2)}, \\ J &= L(\mathbf{O}, \mathbf{Y}) + s, \end{aligned} \tag{4.3.1}\]

where \(L\) is the mean cross-entropy loss over the minibatch and \(\|\mathbf{W}\|_{\textrm{F}}^2 = \sum_{r,j} w_{rj}^2\) is the squared Frobenius norm.

Figure 4.3.2: Computational graph of the forward pass of a one-hidden-layer MLP with weight decay (\(d = 784, h = 256, q = 10\)). Parameter nodes are shaded; each weight matrix feeds both its affine layer and the regularization penalty \(s\).

4.3.2 Backpropagation through an MLP

4.3.2.1 Local Rules for Matrices

For any matrix \(\mathbf{A}\), we write \(\partial J / \partial \mathbf{A}\) for the matrix of partial derivatives \((\partial J / \partial a_{ij})\) of the exact same shape as \(\mathbf{A}\). Three local vector–Jacobian rules (Section 34.4.3.2) handle every layer of an MLP:

  1. Matrix multiplication (\(\mathbf{A} = \mathbf{X}\mathbf{W}\) with \(\mathbf{X} \in \mathbb{R}^{n \times d}, \mathbf{W} \in \mathbb{R}^{d \times h}\)): Given upstream gradient \(\mathbf{G} = \partial J / \partial \mathbf{A} \in \mathbb{R}^{n \times h}\), since \(a_{ij} = \sum_r x_{ir} w_{rj}\), the chain rule yields \(\partial J / \partial w_{rj} = \sum_i x_{ir} g_{ij}\) and \(\partial J / \partial x_{ir} = \sum_j g_{ij} w_{rj}\), or in matrix form: \[ \frac{\partial J}{\partial \mathbf{W}} = \mathbf{X}^\top \mathbf{G}, \qquad \frac{\partial J}{\partial \mathbf{X}} = \mathbf{G}\,\mathbf{W}^\top. \tag{4.3.2}\] Notice how the shapes lock into place: \(\mathbf{X}^\top\mathbf{G}\) is \((d \times n)(n \times h) = d \times h\) (matching \(\mathbf{W}\)), and \(\mathbf{G}\mathbf{W}^\top\) is \((n \times h)(h \times d) = n \times d\) (matching \(\mathbf{X}\)). Just like scalar multiplication, computing \(\partial J / \partial \mathbf{W}\) requires the cached input \(\mathbf{X}\), while computing \(\partial J / \partial \mathbf{X}\) propagates the error signal backward through \(\mathbf{W}^\top\).
  2. Broadcasted bias addition (\(\mathbf{Z} = \mathbf{A} + \mathbf{b}\) with \(\mathbf{b} \in \mathbb{R}^h\)): Since \(b_j\) is added to every row \(i\) of column \(j\), \(\partial J / \partial \mathbf{A} = \mathbf{G}\) passes straight through unchanged, while \(\partial J / \partial \mathbf{b} = \mathbf{G}^\top\mathbf{1}_n\) sums \(\mathbf{G}\) across the \(n\) examples (rows).
  3. Elementwise activation (\(\mathbf{H} = \phi(\mathbf{Z})\)): Since \(h_{ij} = \phi(z_{ij})\) depends only on entry \((i, j)\), the upstream gradient is gated entrywise (Hadamard product \(\odot\)) by the local activation slope: \[\frac{\partial J}{\partial \mathbf{Z}} = \frac{\partial J}{\partial \mathbf{H}} \odot \phi'(\mathbf{Z}).\]

4.3.2.2 The Backward Pass

Starting from \(\partial J / \partial \mathbf{O} = \frac{1}{n}(\hat{\mathbf{Y}} - \mathbf{Y})\) (Section 3.1.2.2, where \(\hat{\mathbf{Y}} = \mathrm{softmax}(\mathbf{O})\) and \(\mathbf{Y} \in \{0, 1\}^{n \times q}\) is the one-hot label matrix) and \(\partial s / \partial \mathbf{W}^{(k)} = \lambda \mathbf{W}^{(k)}\), applying our three rules in reverse topological order from output to input gives the complete MLP backward pass:

\[ \begin{aligned} \frac{\partial J}{\partial \mathbf{O}} &= \frac{1}{n}\bigl(\hat{\mathbf{Y}} - \mathbf{Y}\bigr), \\ \frac{\partial J}{\partial \mathbf{W}^{(2)}} &= \mathbf{H}^\top \frac{\partial J}{\partial \mathbf{O}} + \lambda \mathbf{W}^{(2)}, \qquad \frac{\partial J}{\partial \mathbf{b}^{(2)}} = \Bigl(\frac{\partial J}{\partial \mathbf{O}}\Bigr)^\top \mathbf{1}, \\ \frac{\partial J}{\partial \mathbf{H}} &= \frac{\partial J}{\partial \mathbf{O}}\, \mathbf{W}^{(2)\top}, \\ \frac{\partial J}{\partial \mathbf{Z}} &= \frac{\partial J}{\partial \mathbf{H}} \odot \phi'(\mathbf{Z}), \\ \frac{\partial J}{\partial \mathbf{W}^{(1)}} &= \mathbf{X}^\top \frac{\partial J}{\partial \mathbf{Z}} + \lambda \mathbf{W}^{(1)}, \qquad \frac{\partial J}{\partial \mathbf{b}^{(1)}} = \Bigl(\frac{\partial J}{\partial \mathbf{Z}}\Bigr)^\top \mathbf{1}. \end{aligned} \tag{4.3.3}\]

For a general \(K\)-layer MLP (\(\mathbf{Z}^{(k)} = \mathbf{H}^{(k-1)}\mathbf{W}^{(k)} + \mathbf{b}^{(k)}, \mathbf{H}^{(k)} = \phi(\mathbf{Z}^{(k)})\)), each layer \(k = K-1, \ldots, 1\) executes the exact same two-step module:

\[ \begin{aligned} \frac{\partial J}{\partial \mathbf{Z}^{(k)}} &= \Bigl(\frac{\partial J}{\partial \mathbf{Z}^{(k+1)}}\, \mathbf{W}^{(k+1)\top}\Bigr) \odot \phi'\bigl(\mathbf{Z}^{(k)}\bigr), \\ \frac{\partial J}{\partial \mathbf{W}^{(k)}} &= \mathbf{H}^{(k-1)\top} \frac{\partial J}{\partial \mathbf{Z}^{(k)}} + \lambda \mathbf{W}^{(k)}. \end{aligned} \]

4.3.2.3 A Worked Example

Let us trace Equation 4.3.3 by hand on a tiny network (\(n = 1, d = h = 2, q = 1\), ReLU activation, zero biases, \(\lambda = 0\), and squared loss \(L = \frac{1}{2}(o - y)^2\)):

\[ \mathbf{X} = \begin{bmatrix} 1 & 2 \end{bmatrix},\quad \mathbf{W}^{(1)} = \begin{bmatrix} \phantom{-}1 & 0 \\ -1 & 1 \end{bmatrix},\quad \mathbf{W}^{(2)} = \begin{bmatrix} \phantom{-}2 \\ -1 \end{bmatrix},\quad y = 0. \tag{4.3.4}\]

In the forward pass (Figure 4.3.3), \(\mathbf{Z} = \mathbf{X}\mathbf{W}^{(1)} = \begin{bmatrix} -1 & 2 \end{bmatrix}\), \(\mathbf{H} = \mathrm{ReLU}(\mathbf{Z}) = \begin{bmatrix} 0 & 2 \end{bmatrix}\) (unit 1 is inactive), \(o = \mathbf{H}\mathbf{W}^{(2)} = -2\), and \(L = \frac{1}{2}(-2)^2 = 2\). In the backward pass, \(\partial L / \partial o = o - y = -2\), and

\[ \begin{aligned} \frac{\partial L}{\partial \mathbf{W}^{(2)}} &= \mathbf{H}^\top \frac{\partial L}{\partial o} = \begin{bmatrix} 0 \\ 2 \end{bmatrix}(-2) = \begin{bmatrix} \phantom{-}0 \\ -4 \end{bmatrix}, \\ \frac{\partial L}{\partial \mathbf{H}} &= \frac{\partial L}{\partial o}\,\mathbf{W}^{(2)\top} = \begin{bmatrix} -4 & 2 \end{bmatrix}, \\ \frac{\partial L}{\partial \mathbf{Z}} &= \begin{bmatrix} -4 & 2 \end{bmatrix} \odot \begin{bmatrix} 0 & 1 \end{bmatrix} = \begin{bmatrix} 0 & 2 \end{bmatrix}, \\ \frac{\partial L}{\partial \mathbf{W}^{(1)}} &= \mathbf{X}^\top \frac{\partial L}{\partial \mathbf{Z}} = \begin{bmatrix} 1 \\ 2 \end{bmatrix}\begin{bmatrix} 0 & 2 \end{bmatrix} = \begin{bmatrix} 0 & 2 \\ 0 & 4 \end{bmatrix}. \end{aligned} \]

Notice that the entire first column of \(\partial L / \partial \mathbf{W}^{(1)}\) is zero: because \(z_1 = -1 < 0\), \(\phi'(z_1) = 0\) blocks the upstream gradient \(\partial L / \partial h_1 = -4\) from reaching any weight feeding unit 1.

Figure 4.3.3: Forward values (black) and backward gradients (red) for the worked example Equation 4.3.4. Because \(z_1 = -1 < 0\), the ReLU gate zeroes out \(\partial L / \partial z_1\) and the first column of \(\partial L / \partial \mathbf{W}^{(1)}\).

4.3.2.4 Checking the Backward Pass on a Minibatch

Let us implement Equation 4.3.3 in mlp_backward and verify it in 64-bit floating point against each framework’s automatic differentiation on a random \(256 \times 784\) minibatch (\(h = 256, q = 10, \lambda = 10^{-3}\)):

def mlp_backward(X, Y, W1, b1, W2, b2, lambd):
    """Gradients of J = cross-entropy + weight decay, by backprop."""
    Z = X @ W1 + b1  # forward pass, keeping X, Z, H, and Y_hat
    H = torch.relu(Z)
    Y_hat = torch.softmax(H @ W2 + b2, dim=1)
    G_O = (Y_hat - Y) / X.shape[0]  # dJ/dO
    dW2, db2 = H.T @ G_O + lambd * W2, G_O.sum(dim=0)
    G_Z = (G_O @ W2.T) * (Z > 0)  # dJ/dH, then dJ/dZ
    dW1, db1 = X.T @ G_Z + lambd * W1, G_Z.sum(dim=0)
    return dW1, db1, dW2, db2
def mlp_backward(X, Y, W1, b1, W2, b2, lambd):
    """Gradients of J = cross-entropy + weight decay, by backprop."""
    Z = X @ W1 + b1  # forward pass, keeping X, Z, H, and Y_hat
    H = tf.nn.relu(Z)
    Y_hat = tf.nn.softmax(H @ W2 + b2, axis=1)
    G_O = (Y_hat - Y) / X.shape[0]  # dJ/dO
    dW2 = tf.matmul(H, G_O, transpose_a=True) + lambd * W2
    G_Z = tf.matmul(G_O, W2, transpose_b=True) * tf.cast(Z > 0, Z.dtype)
    dW1 = tf.matmul(X, G_Z, transpose_a=True) + lambd * W1
    return dW1, tf.reduce_sum(G_Z, 0), dW2, tf.reduce_sum(G_O, 0)
def mlp_backward(X, Y, W1, b1, W2, b2, lambd):
    """Gradients of J = cross-entropy + weight decay, by backprop."""
    Z = X @ W1 + b1  # forward pass, keeping X, Z, H, and Y_hat
    H = jax.nn.relu(Z)
    Y_hat = jax.nn.softmax(H @ W2 + b2, axis=1)
    G_O = (Y_hat - Y) / X.shape[0]  # dJ/dO
    dW2, db2 = H.T @ G_O + lambd * W2, G_O.sum(axis=0)
    G_Z = (G_O @ W2.T) * (Z > 0)  # dJ/dH, then dJ/dZ
    dW1, db1 = X.T @ G_Z + lambd * W1, G_Z.sum(axis=0)
    return dW1, db1, dW2, db2
def mlp_backward(X, Y, W1, b1, W2, b2, lambd):
    """Gradients of J = cross-entropy + weight decay, by backprop."""
    Z = X @ W1 + b1  # forward pass, keeping X, Z, H, and Y_hat
    H = npx.relu(Z)
    Y_hat = npx.softmax(H @ W2 + b2, axis=1)
    G_O = (Y_hat - Y) / X.shape[0]  # dJ/dO
    dW2, db2 = H.T @ G_O + lambd * W2, G_O.sum(axis=0)
    G_Z = (G_O @ W2.T) * (Z > 0)  # dJ/dH, then dJ/dZ
    dW1, db1 = X.T @ G_Z + lambd * W1, G_Z.sum(axis=0)
    return dW1, db1, dW2, db2

Let us generate a random 256-example minibatch with the exact dimensions and initialization of Section 4.2 plus \(\lambda = 10^{-3}\), and compare mlp_backward against each framework’s built-in automatic differentiation. Following best practice for gradient checks, we run in 64-bit floating point (whose ~\(10^{-16}\) rounding floor catches even tiny algebraic bugs cleanly) and report the maximum relative discrepancy per parameter tensor.

n, d, h, q, lambd = 256, 784, 256, 10, 1e-3
f64 = torch.float64  # 64-bit floating point throughout the check
X, y = torch.randn(n, d, dtype=f64), torch.randint(0, q, (n,))
params = [torch.randn(d, h, dtype=f64) * 0.01, torch.zeros(h, dtype=f64),
          torch.randn(h, q, dtype=f64) * 0.01, torch.zeros(q, dtype=f64)]
for p in params:
    p.requires_grad_(True)
W1, b1, W2, b2 = params
J = (F.cross_entropy(torch.relu(X @ W1 + b1) @ W2 + b2, y)
     + lambd / 2 * ((W1 ** 2).sum() + (W2 ** 2).sum()))
J.backward()  # automatic differentiation
with torch.no_grad():
    grads = mlp_backward(X, F.one_hot(y, q).to(f64), *params, lambd)
for name, p, g in zip(['W1', 'b1', 'W2', 'b2'], params, grads):
    rel = (p.grad - g).abs().max() / p.grad.abs().max()
    print(f'{name}: relative difference {float(rel):.1e}')
W1: relative difference 4.4e-16
b1: relative difference 2.9e-16
W2: relative difference 3.8e-16
b2: relative difference 2.1e-16
n, d, h, q, lambd = 256, 784, 256, 10, 1e-3
f64 = tf.float64  # 64-bit floating point throughout the check
X = tf.random.normal((n, d), dtype=f64)
y = tf.random.uniform((n,), 0, q, dtype=tf.int32)
params = [tf.Variable(tf.random.normal((d, h), stddev=0.01, dtype=f64)),
          tf.Variable(tf.zeros(h, f64)),
          tf.Variable(tf.random.normal((h, q), stddev=0.01, dtype=f64)),
          tf.Variable(tf.zeros(q, f64))]
W1, b1, W2, b2 = params
with tf.GradientTape() as tape:
    O = tf.nn.relu(X @ W1 + b1) @ W2 + b2
    J = (tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, O))
         + lambd / 2 * (tf.reduce_sum(W1 ** 2) + tf.reduce_sum(W2 ** 2)))
auto = tape.gradient(J, params)  # automatic differentiation
grads = mlp_backward(X, tf.one_hot(y, q, dtype=f64), *params, lambd)
for name, a, g in zip(['W1', 'b1', 'W2', 'b2'], auto, grads):
    rel = tf.reduce_max(tf.abs(a - g)) / tf.reduce_max(tf.abs(a))
    print(f'{name}: relative difference {float(rel):.1e}')
W1: relative difference 3.0e-16
b1: relative difference 1.8e-16
W2: relative difference 1.1e-16
b2: relative difference 1.0e-16
jax.config.update('jax_enable_x64', True)  # 64-bit is off by default
n, d, h, q, lambd = 256, 784, 256, 10, 1e-3
f64 = jnp.float64  # 64-bit floating point throughout the check
keys = jax.random.split(d2l.get_key(), 4)
X = jax.random.normal(keys[0], (n, d), f64)
y = jax.random.randint(keys[1], (n,), 0, q)
params = [jax.random.normal(keys[2], (d, h), f64) * 0.01, jnp.zeros(h, f64),
          jax.random.normal(keys[3], (h, q), f64) * 0.01, jnp.zeros(q, f64)]

def objective(params):
    W1, b1, W2, b2 = params
    O = jax.nn.relu(X @ W1 + b1) @ W2 + b2
    return (optax.softmax_cross_entropy_with_integer_labels(O, y).mean()
            + lambd / 2 * ((W1 ** 2).sum() + (W2 ** 2).sum()))

auto = jax.grad(objective)(params)  # automatic differentiation
grads = mlp_backward(X, jax.nn.one_hot(y, q, dtype=f64), *params, lambd)
for name, a, g in zip(['W1', 'b1', 'W2', 'b2'], auto, grads):
    rel = jnp.abs(a - g).max() / jnp.abs(a).max()
    print(f'{name}: relative difference {float(rel):.1e}')
W1: relative difference 3.5e-16
b1: relative difference 1.7e-16
W2: relative difference 2.2e-16
b2: relative difference 1.8e-16
n, d, h, q, lambd = 256, 784, 256, 10, 1e-3
f64 = 'float64'  # 64-bit floating point throughout the check
X = mnp.random.normal(size=(n, d), dtype=f64)
y = mnp.random.randint(0, q, (n,))
params = [mnp.random.normal(0, 0.01, (d, h), dtype=f64), mnp.zeros(h, f64),
          mnp.random.normal(0, 0.01, (h, q), dtype=f64), mnp.zeros(q, f64)]
for p in params:
    p.attach_grad()
W1, b1, W2, b2 = params
with autograd.record():
    O = npx.relu(X @ W1 + b1) @ W2 + b2
    J = (-npx.pick(npx.log_softmax(O), y).mean()
         + lambd / 2 * ((W1 ** 2).sum() + (W2 ** 2).sum()))
J.backward()  # automatic differentiation
grads = mlp_backward(X, npx.one_hot(y, q, dtype=f64), *params, lambd)
for name, p, g in zip(['W1', 'b1', 'W2', 'b2'], params, grads):
    rel = mnp.abs(p.grad - g).max() / mnp.abs(p.grad).max()
    print(f'{name}: relative difference {float(rel):.1e}')
W1: relative difference 4.8e-16
b1: relative difference 4.9e-16
W2: relative difference 2.8e-16
b2: relative difference 1.9e-16

Every parameter gradient matches automatic differentiation to within \(10^{-15}\) relative error, a few units of the double-precision machine epsilon \(2.2 \times 10^{-16}\).

4.3.3 Memory and Computation

4.3.3.1 Stored Activations vs. Parameter Memory

Looking at Equation 4.3.3, computing \(\partial J / \partial \mathbf{W}^{(2)}\) and \(\partial J / \partial \mathbf{W}^{(1)}\) during the backward pass requires the forward pass to retain \(\mathbf{X}\), \(\mathbf{H}\) (which for ReLU also encodes the mask \(\mathbf{Z} > 0\)), and \(\hat{\mathbf{Y}}\). On our Fashion-MNIST MLP (\(d = 784, h = 256, q = 10\)) with minibatch size \(n = 256\), these cached tensors hold

\[256 \times 784 + 256 \times 256 + 256 \times 10 = 268{,}800\textrm{ floats} \approx 1.1\textrm{ MB}, \tag{4.3.5}\]

already exceeding the \(203{,}530\) parameters (\(\approx 0.8\textrm{ MB}\)). In a \(K\)-layer network, activation memory grows linearly with both batch size \(n\) and depth \(K\), whereas inference can discard each layer’s activations immediately after computing the next layer.

4.3.3.2 Arithmetic Cost

In the forward pass, each example performs \(d h + h q = 784 \times 256 + 256 \times 10 = 203{,}264\) multiply-adds. In the backward pass, each interior matrix multiply \(\mathbf{H}^{(k-1)}\mathbf{W}^{(k)}\) spawns two backward matrix multiplies of the same size (\(\mathbf{H}^{(k-1)\top}\mathbf{G}\) for the weights and \(\mathbf{G}\mathbf{W}^{(k)\top}\) for the activations below), while the first layer needs only \(\mathbf{X}^\top\mathbf{G}_Z\) (since \(\partial J / \partial \mathbf{X}\) is not needed). For our two-layer MLP the backward pass therefore takes \(d h + 2 h q = 205{,}824\) multiply-adds, about the same as the forward pass: the first layer holds almost all of the work, and it needs a single backward product. In a deep network nearly every layer is interior, so the backward pass costs \(\sim 2\times\) the forward pass and a full training step roughly \(3\times\) forward inference. Either way, all \(203{,}530\) derivatives cost one or two extra forward passes, whereas a one-sided finite difference spends one extra forward pass on a single scalar parameter.

4.3.4 Summary

Backpropagation through a multilayer perceptron reduces to three local matrix rules applied in reverse topological order: \(\partial J / \partial \mathbf{W} = \mathbf{X}^\top\mathbf{G}\) and \(\partial J / \partial \mathbf{X} = \mathbf{G}\mathbf{W}^\top\) for matrix products \(\mathbf{A} = \mathbf{X}\mathbf{W}\), row summation \(\mathbf{G}^\top\mathbf{1}\) for broadcasted biases, and elementwise gating \(\mathbf{G} \odot \phi'(\mathbf{Z})\) for activations, summing incoming gradients wherever a parameter feeds both the data loss and weight decay. Because \(\mathbf{X}^\top\mathbf{G}\) depends on the forward inputs to each layer, training must cache intermediate activations across the forward sweep, requiring \(O(nKh)\) memory that grows linearly with batch size and depth. In return, a single backward sweep computes all parameter gradients in at most about twice the FLOPs of one forward pass.

4.3.5 Exercises

  1. Jacobian of a layer. The gradient of the scalar \(J\) with respect to \(\mathbf{W}^{(2)} \in \mathbb{R}^{h \times q}\) has the shape of \(\mathbf{W}^{(2)}\). The derivative of the matrix \(\mathbf{O} = \mathbf{H}\mathbf{W}^{(2)} \in \mathbb{R}^{n \times q}\) with respect to \(\mathbf{W}^{(2)}\) is a larger object, with one entry \(\partial o_{ij} / \partial w_{rt}\) for every combination of the four indices.

    1. How many entries does \(\partial \mathbf{O} / \partial \mathbf{W}^{(2)}\) have, and how many of them can be nonzero?
    2. Evaluate the chain rule \(\partial J / \partial w_{rt} = \sum_{i, j} (\partial J / \partial o_{ij})(\partial o_{ij} / \partial w_{rt})\) to recover \(\mathbf{H}^\top \partial J / \partial \mathbf{O}\).
    3. How much memory would \(\partial \mathbf{O} / \partial \mathbf{W}^{(2)}\) take in 32-bit floating point for the MLP of Section 4.2 with \(n = 256\)? Explain why backpropagation never forms it.
  2. Memory of a deeper network. Consider an MLP with \(d\) inputs, \(q\) outputs, and \(K\) layers, of which the \(K - 1\) hidden ones are ReLU layers of width \(h\), trained on minibatches of \(n\) examples.

    1. Using the step for \(K\) layers in the text, list the values that the backward pass reads, and count them. Count the parameters.
    2. Prediction can discard each value as soon as the next layer has used it. Count the largest number of values that prediction holds at any moment.
    3. For \(d = 784\), \(h = 256\), and \(q = 10\), find the smallest minibatch size at which the stored values outnumber the parameters, for \(K = 2\) and for \(K = 11\).
  3. [code] Gradient checking. Before automatic differentiation, a hand-written backward pass was checked against finite differences.

    1. Pick one entry \(w\) of \(\mathbf{W}^{(1)}\) and compare its gradient from mlp_backward with the central difference \(\bigl(J(w + \epsilon) - J(w - \epsilon)\bigr) / (2\epsilon)\) for \(\epsilon = 10^{-1}, 10^{-2}, \ldots, 10^{-7}\), once in 32-bit and once in 64-bit floating point. Plot the relative error against \(\epsilon\) on logarithmic axes.
    2. Explain the shape of the curves. Which error decreases with \(\epsilon\), which increases, and where do they balance in each precision?
    3. The check can fail at a correct gradient when a pre-activation lies within \(\epsilon\) of zero. Explain why, and how to avoid it.
  4. Forward mode. Backpropagation seeds \(\partial J / \partial J = 1\) at the output and sweeps the graph backward. Forward-mode differentiation instead seeds \(\partial w / \partial w = 1\) at one chosen scalar parameter \(w\) and sweeps forward: each node computes the derivative of its value with respect to \(w\) from the derivatives of its inputs.

    1. Use forward mode to recompute \(\partial L / \partial \mathbf{W}^{(1)}\) for the network in Equation 4.3.4, one sweep per entry of \(\mathbf{W}^{(1)}\).
    2. How many sweeps does each mode need to obtain all six weight gradients of this network? Which products does forward mode compute again in every sweep that reverse mode computes only once?

    Adapted from Simon Prince, Understanding Deep Learning, Problems 7.12 and 7.13.

  5. [extended] Autograd engine. ● Build a miniature automatic differentiation engine and use it to recompute the gradients of the network in Equation 4.3.4.

    1. Write a scalar Value class that records, for each result, its inputs and the operation that produced it (the computational graph), supporting +, *, and relu. (Hint: implement __add__ and __mul__ so that each returns a new Value holding references to its parents and a small function that propagates the gradient one step.)
    2. Implement backward(): sort the graph topologically, seed the output’s gradient with \(1\), and visit the nodes in reverse order, letting each node pass its gradient to its parents. Make sure that gradients accumulate (+=, not =) when a value is used more than once.
    3. Recompute the graph of Figure 4.3.1 and the worked example Equation 4.3.4 with your engine (unroll the matrix products into scalars), and compare the gradients with those derived in this section.
    4. Consider the chain in which a scalar input \(x\) produces \(v_i = f_i(x)\) for \(i = 1, 2, 3\), the intermediate value \(m = v_1 + v_2 + v_3\), the values \(p_j = g_j(m)\) for \(j = 1, 2, 3\), and the loss \(L = p_1 + p_2 + p_3\). Write \(\alpha_i = f_i'(x)\) and \(\beta_j = g_j'(m)\). The chain rule as a sum over paths enumerates \(3 \times 3 = 9\) paths from \(x\) to \(L\), each contributing the product \(\alpha_i \beta_j\). Show that your engine instead computes \((\alpha_1 + \alpha_2 + \alpha_3)(\beta_1 + \beta_2 + \beta_3)\) by accumulating the gradient at \(m\) once, and explain why this sharing keeps backpropagation affordable in deep networks.

    Adapted from Andrej Karpathy, micrograd, micrograd/engine.py.

Discussions