TL;DR
A Recurrent Neural Network reuses the same weights at every step of a sequence, so its loss depends on those weights many times over. Backpropagation through time (BPTT) handles this by unrolling the network into one copy per time step, which turns it into an ordinary deep network, and then running Backpropagation backwards through the copies. Each weight's gradient is the sum of its contributions from every step, and the length of the sequence sets how long the backward chain is.
Builds on: Backpropagation, Recurrent Neural Network · Leads to: Vanishing Gradient Problem, Gradient Norm Clipping
Where it's used
Language models. A recurrent language model reads a sentence one word at a time and predicts the next word. Training it means computing the gradient of the prediction error summed over every word position. Early neural language models were trained this way before attention-based models took over.
Speech recognition. Acoustic features arrive as a long sequence of frames, often hundreds or more per utterance. BPTT lets a recurrent acoustic model learn from the whole utterance, with each frame's error sent back through all earlier frames.
Time-series forecasting. Sensor readings, energy demand and financial prices are sequences. A recurrent forecaster trained with BPTT learns how a value at one time depends on its past, and the same unrolling gives the gradients for every weight.
How it works
Take a tiny recurrent unit with one weight shared across time, a starting state , and three inputs , , . Each step computes . We drop the usual nonlinearity so every number is hand-computable; the logic is identical with one added, as explained in the math. Use and the loss , so the goal is .
flowchart LR h0["h0 = 1"] -->|"w"| h1["h1"] x1["x1 = 1"] --> h1 h1 -->|"w"| h2["h2"] x2["x2 = 2"] --> h2 h2 -->|"w"| h3["h3"] x3["x3 = 3"] --> h3 h3 --> L["L = h3"] L -. "gradient flows back" .-> h3 h3 -. "times w" .-> h2 h2 -. "times w" .-> h1
- Unroll. Make one copy of the cell per time step: three copies, each with the same weight . Check: the unrolled graph is now a plain feedforward chain of three layers.
- Forward pass. . . . Check: each value uses the previous one, so the order matters.
- Start the backward pass. The loss is , so its gradient with respect to is . Check: changing by 1 changes by 1.
- Step back one time step. Because , a change in changes by . So . Check: is smaller than because is less than 1.
- Step back again. . Check: each backward step multiplies by once more.
- Collect the weight's contributions. Each step uses the weight once, so the weight gets one contribution per step: . The contributions are , , and . Their sum is . This is the gradient that gradient descent uses to update .
- Interpretation. The gradient is the sum of one term per time step, and each term is multiplied by how many times has been applied since. Check: direct calculation gives when , so at . The two results agree.
The math
Write the recurrence in general form. At step the hidden state is , where and are weight matrices shared across time, is the input and is an activation function. Let the total loss be a sum over time, , with the loss at step . Every gradient below comes from the chain rule applied along the unrolled chain.
Define the backward signal at step as . Then the gradient with respect to the shared weight matrix is
where is the pre-activation at step , and the middle factor is a product of Jacobians (see Jacobian Matrix):
Step 4 in symbols: the backward recursion
The recurrence says that is the direct loss gradient at step , plus the signal from step passed back through the local Jacobian. For the scalar example with the identity, the local Jacobian is just , so
With only the final loss, and , matching step 4 of the walkthrough.
Step 6 in symbols: summing over time
The weight appears in with coefficient , so the gradient is
Substituting the numbers from the walkthrough gives .
Adding the nonlinearity
If (the hyperbolic tangent), the local Jacobian at step becomes , and . Because , the factor can only shrink the signal further. Everything else in the walkthrough stays the same.
Why the length of the sequence matters
If every factor in the product has size at most , then the contribution from steps back is bounded by . For the scalar case with , that bound after 10 steps is . For the same product is . Repeated multiplication is why long sequences make the early part of the gradient either vanish or explode; see Vanishing Gradient Problem and Gradient Norm Clipping.
- w = 0.5
- w = 1.2
Worked examples
Example 1: one step
- Set , , and the loss .
- Forward: .
- Backward: .
- Interpretation: with only one step there is no chain to pass through, so the gradient is just the previous state, here .
Example 2: three steps, the standard case
- Forward values: , , .
- Backward signals: , , .
- Per-step contributions: , , .
- Total: .
- Check: the polynomial has derivative , which is at .
- Interpretation: the earliest step contributes the least, because its signal has been multiplied by twice on the way back.
Example 3: a realistic sequence
Suppose a recurrent language model reads a sequence of 50 tokens, and the effective per-step factor along the backward chain is in one run and in another.
- Factor after 50 steps with : . A signal from the first token reaches the loss at about half of one percent of its size.
- Factor after 50 steps with : . The same signal is amplified more than a hundredfold.
- Clipping: if the total gradient norm is and the clipping threshold is , the gradient is scaled by to bring its norm down to . The direction is kept and the step is no longer destructive; see Gradient Norm Clipping.
- Interpretation: the first case means the model learns little from early tokens; the second means a single update can wreck training. BPTT computes both correctly, and the fix must be applied to the gradient afterwards.
Try it: a per-step factor of over 20 steps gives what size? Answer: , about 1.2 percent.
Common mistakes and tradeoffs
It is tempting to think BPTT needs a separate algorithm. In fact it is ordinary backpropagation applied to the unrolled network. The only special feature is that the shared weight appears at every copy, so its gradient is summed over the copies.
It is tempting to think you can train on arbitrarily long sequences cheaply. In fact the backward pass must store the activations of every step, so memory grows with sequence length, and the backward chain is as long as the sequence. This is why people use truncated BPTT, which only unrolls a fixed number of steps.
It is tempting to think a vanishing gradient means a bug. In fact a small contribution from early steps is what the arithmetic predicts whenever the per-step factor is below 1. It is a property of the model and the sequence length, and it is the reason gated cells and clipping were developed.
When to use it. Use BPTT whenever the network is recurrent and the outputs depend on the whole sequence, and the sequence is short enough to unroll in memory.
When not to use it. For very long sequences, truncated BPTT or a model with a more stable recurrence is a better choice. Attention-based models avoid the long backward chain altogether by connecting distant positions directly.
History
Paul Werbos wrote up the method under the name backpropagation through time in a 1990 paper. Training recurrent networks stayed hard for years afterwards. In 1991 Sepp Hochreiter analysed the vanishing gradient problem in his diploma thesis. The 1986 paper by Rumelhart, Hinton and Williams popularised backpropagation for feedforward networks, and BPTT then became the standard way to train recurrent ones. In 2013 Pascanu, Mikolov and Bengio analysed exploding and vanishing gradients in recurrent networks and proposed gradient clipping, which is now common practice.
Recap
- A recurrent network shares its weights across time; BPTT unrolls it into one layer per step.
- The backward pass multiplies the signal by the recurrent weight at each step back in time.
- The gradient of a shared weight is the sum of one contribution per step.
- Repeated multiplication makes contributions from far back in time shrink or grow, which causes the vanishing and exploding gradient problems.
- Next, read Vanishing Gradient Problem for why the early signal disappears and Gradient Norm Clipping for the usual fix for explosions.
Check yourself
QuestionRecall: what is BPTT doing to a recurrent network before it applies backpropagation?Show answer
It unrolls the network into one copy per time step, so that the recurrent network becomes an ordinary deep feedforward network with shared weights. Backpropagation is then applied to that unrolled network.
QuestionComputation: with , , , , and loss , where , what is ?Show answer
Forward: , . Backward: , . Contributions: and . Total: .
QuestionWhy: why does the gradient from 50 steps back depend on a power of the per-step factor, and not on the sum of the factors?Show answer
Each step back passes the signal through the recurrent Jacobian once more, so the signal from steps back is multiplied by the factor times. Products of factors give a power, . A sum would only appear if the signal were added at each step, which is not how the chain rule combines them.
QuestionApplied: a recurrent model over 30 tokens has per-step factor 0.95. A training engineer says the early tokens will still get a strong learning signal. Is that right?Show answer
No. , so the signal from the first token is about a fifth of its size at the loss. That is a moderate but real shrinkage, and it gets much worse with longer sequences or a factor further below 1, which is the vanishing gradient problem.