Backpropagation Through Time › A Worked Example
Language AI HandbookAll chapters Backpropagation Through Time › A Worked ExampleOne section · Sections in this chapter6 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Derivations and code can make BPTT seem abstract. Working through a tiny numerical example by hand is the fastest way to build intuition about what the equations compute, where the values come from, and why the backward pass has to visit timesteps in reverse order.
Let's trace through BPTT on a minimal example to make the mechanics concrete.
Consider an RNN with hidden dimension , processing a sequence of three steps. The recurrent weight matrix is:
After the forward pass, the hidden states are:
The error signals flowing back from the output layer (local gradients from each timestep's loss) are:
These local gradients come from the output layer: specifically, they are the derivatives of each timestep's loss with respect to the hidden state at that step. In practice, this derivative is computed by backpropagating through the output weight matrix and then through the softmax (if applicable). For this example, we take them as given to focus on the backward-through-time portion.
Step 1: Initialize at the last timestep
The backward pass starts from the final timestep. The initial error signal is simply the local gradient:
The tanh derivative (elementwise) at timestep 3 is:
The modified error (tanh derivative applied before backpropagating) is:
Step 2: Propagate backward to
The recurrence carries the error signal one step further back:
Computing the matrix-vector product:
Note that is with rows and columns swapped, because we are differentiating with respect to .
Adding the local gradient:
Step 3: Accumulate the gradient contribution to
At each timestep, the contribution to is an outer product of the modified error signal and the previous hidden state. For timestep 3:
where:
- : the modified error vector at time 3
- : the previous hidden state as a row vector
- The outer product gives a gradient matrix matching the shape of
The total gradient for is the sum of these outer products across all three timesteps:
This example illustrates several important properties of BPTT. First, the backward pass must run in strict reverse order: you cannot compute until you have computed . Second, each timestep's gradient contribution is an outer product that has exactly the shape of the weight matrix, which is why the gradient accumulation is just a simple sum. Third, the magnitude of the gradient contribution from each timestep depends both on the local error signal and on how much that signal has been attenuated by the chain of Jacobians from later steps.
Understanding What the Gradients Tell Us
It is instructive to think about what the gradient says. The entry at position in this matrix tells you: if you increase the weight connecting hidden dimension of the previous state to hidden dimension of the current state, how does the total loss change? A large positive value means increasing that weight increases the loss. A large negative value means increasing it decreases the loss. A near-zero value means that weight has minimal effect on the loss, perhaps because the gradient signal has been attenuated by vanishing gradients along its path.
When the gradient for is very small (close to the zero matrix), gradient descent will barely update the recurrent weights. This is the vanishing gradient problem: the model cannot effectively learn from gradient signal that traveled a long path through the unrolled network. When the gradient is very large, a single gradient step will make a drastic change to the weights, destabilizing training. The next chapter examines both pathologies in rigorous detail.