Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Training through time
To teach a recurrent network, we “unroll” it: draw one copy of the network per time step, each passing its memory to the next. That picture is an ordinary deep network, so we can backpropagate through it, and every copy’s gradient adds to the same shared weights. For very long sequences we cut the unrolled picture into short windows. The memory still flows forward across the cuts, but blame (the gradient) stops at each cut. The demo shows how the gradient shrinks as it travels back.
Streams from sensors or engines never end. Truncated BPTT trains recurrent models on them with fixed memory, at the price of not learning dependencies longer than the window.
- 1. BPTT applies backpropagation to…
- 2. Shared weights receive…
- 3. Truncated BPTT stops…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Gradient of shared weights: for L = Σₜ Lₜ,
- ∂L/∂W = Σₜ Σ(s ≤ t) (∂Lₜ/∂hₜ)(∂hₜ/∂hₛ)(∂⁺hₛ/∂W);
- ∂⁺ is the immediate dependence at step s;
- ∂hₜ/∂hₛ is a product of Jacobians, which vanishes or explodes (E11-1).
- Scalar example:
- for hₜ = tanh(w hₜ₋₁ + u xₜ), each step back multiplies the error by w(1 − hₜ²);
- with w = 0.5 and hₜ = 0 for three steps, ∂h₃/∂h₀ = 0.125.
- Memory: BPTT stores every hidden state. 1000 steps × batch 32 × 512 units × 4 bytes = 65.5 MB per layer, against 3.3 MB for 50 steps.
- Truncated BPTT(k₁, k₂) (Williams and Peng): every k₁ steps, backpropagate k₂ steps.
- k₁ = k₂ = k (chunks) costs about T backward steps per stream;
- k₁ = 1, k₂ = 20 costs 20T but gives every step a full window;
- a stream of 4000 in chunks of 8 gives 500 updates.
- Reach: with chunks of k, an output’s error reaches at most k − 1 earlier inputs within its chunk. To learn a lag of L you need k ≥ L + 1, at least for outputs at the end of a chunk.
- 1. ∂h₃/∂h₀ for w = 0.5 and h = 0:
- 2. BPTT memory grows with…
- 3. Smallest chunk length to learn a lag of 4:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.BPTT in code
Exercise 1.
- Hand-written BPTT for a scalar RNN matched central differences (1.24529).
- Truncating each error to k steps gave errors of 0.034, 0.104, 0.022 and 0.003 for k = 1, 2, 4 and 8. These are not monotone, because dropped terms have mixed signs.
Practice.
- Autograd does BPTT automatically when you loop over steps or use
nn.RNN. - For truncation, carry the state between chunks with
h = h.detach(), which keeps the value but cuts the graph. - Keep the stream order within each batch row (stateful training), and reset states at sequence boundaries.
- Clip gradients, since sums over many steps can be large.
h = torch.zeros(1, B, H)
for chunk_x, chunk_y in chunks(stream, k): # consecutive pieces of each stream
h = h.detach() # keep the state, cut the gradient
out, h = rnn(chunk_x, h)
loss = crit(head(out), chunk_y); opt.zero_grad(); loss.backward(); opt.step()- 1. In PyTorch, cut the graph between chunks with…
- 2. Truncation errors in exercise 1 were…
- 3. Stateful training keeps…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
What truncation loses.
- Truncated BPTT drops every term ∂hₜ/∂hₛ with t − s ≥ k. The gradient is biased.
- The bias is small if long-range terms are small anyway (vanishing gradients).
- It is fatal for genuinely needed long dependencies: the network cannot learn to write information into its state for later use.
- It can still learn to read whatever the carried state happens to contain, because the first step of each chunk depends on the carried state and the weights.
Real-time recurrent learning (RTRL).
- Williams and Zipser (1989) carry the sensitivities ∂hₜ/∂W forward in time, giving exact online gradients with no truncation.
- They cost O(n⁴) time and O(n³) memory per step for n units, impractical except for tiny networks.
- Approximations such as UORO (Tallec and Ollivier, 2017) trade bias for variance.
Cost. Full BPTT costs O(T) time and memory per sequence. Checkpointing stores only some states and recomputes the rest, trading compute for O(√T) memory.
- 1. Truncated BPTT's gradient is…
- 2. RTRL's cost per step for n units:
- 3. Checkpointing reduces BPTT memory to about…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How long must the window be?
History.
- P. Werbos (1990) gave the classic account, “Backpropagation through time: what it does and how to do it”.
- R. Williams and D. Zipser (1989) introduced RTRL. R. Williams and J. Peng (1990) analysed truncated BPTT(h; h′).
- I. Sutskever’s thesis (2013) popularised k₁/k₂ schedules for language models.
Exercise 2.
- Task (echo): a 16-unit tanh RNN reads a stream of random ±1 inputs and must output the input from 4 steps earlier.
- Training: truncated BPTT on 32 streams of length 4000 with chunk length k, carrying the hidden state across chunks. The same data is used for every k, so smaller k means more updates.
- Measurement: test accuracy, averaged over 2 seeds.
| truncation k | updates | accuracy |
|---|---|---|
| 2 | 1998 | 0.608 |
| 4 | 999 | 0.768 |
| 5 | 799 | 1.000 |
| 8 | 499 | 0.999 |
| 16 | 249 | 0.999 |
- One step beyond the lag is enough. k = 5 links every chunk-final output to its target input and learns the task perfectly.
- Shorter windows learn only partially, not at chance. No gradient ever connects an output to the input it must echo. But the carried state happens to hold traces of past inputs, and the network learns to read them (0.61 and 0.77).
- More updates do not compensate: k = 2 had 4 times the updates of k = 8.
BPTT in AI, robotics and aerospace
- Online learning: truncated BPTT or RTRL approximations let a controller adapt during operation without storing whole histories.
- Long telemetry streams: choose the window from the longest dependency you need to learn, not from memory limits alone.
- Hardware: BPTT’s memory dominates training cost for long sequences; checkpointing and truncation are standard tools.
References
- P. J. Werbos (1990). “Backpropagation through time: what it does and how to do it.” Proc. IEEE 78(10).
- R. J. Williams, D. Zipser (1989). “A learning algorithm for continually running fully recurrent neural networks.” Neural Computation 1(2).
- R. J. Williams, J. Peng (1990). “An efficient gradient-based algorithm for on-line training of recurrent network trajectories.” Neural Computation 2(4).
- C. Tallec, Y. Ollivier (2018). “Unbiased online recurrent optimization.” Proc. ICLR.