Search

Search papers and lessons

Backpropagation Through Time

Level 3 · 11 min read

Backpropagation through time trains a recurrent network by unrolling it into one layer per time step and running backpropagation backwards along the chain of steps.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Architectures
  • ReadReady to readNot yet

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 ww shared across time, a starting state h0=1h_0 = 1, and three inputs x1=1x_1 = 1, x2=2x_2 = 2, x3=3x_3 = 3. Each step computes ht=w ht−1+xth_t = w\,h_{t-1} + x_t. We drop the usual nonlinearity so every number is hand-computable; the logic is identical with one added, as explained in the math. Use w=0.5w = 0.5 and the loss L=h3L = h_3, so the goal is ∂L/∂w\partial L / \partial w.

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
  1. Unroll. Make one copy of the cell per time step: three copies, each with the same weight ww. Check: the unrolled graph is now a plain feedforward chain of three layers.
  2. Forward pass. h1=0.5×1+1=1.5h_1 = 0.5 \times 1 + 1 = 1.5. h2=0.5×1.5+2=2.75h_2 = 0.5 \times 1.5 + 2 = 2.75. h3=0.5×2.75+3=4.375h_3 = 0.5 \times 2.75 + 3 = 4.375. Check: each value uses the previous one, so the order matters.
  3. Start the backward pass. The loss is L=h3L = h_3, so its gradient with respect to h3h_3 is δ3=1\delta_3 = 1. Check: changing h3h_3 by 1 changes LL by 1.
  4. Step back one time step. Because h3=w h2+x3h_3 = w\,h_2 + x_3, a change in h2h_2 changes h3h_3 by ww. So δ2=δ3×w=1×0.5=0.5\delta_2 = \delta_3 \times w = 1 \times 0.5 = 0.5. Check: δ2\delta_2 is smaller than δ3\delta_3 because ww is less than 1.
  5. Step back again. δ1=δ2×w=0.5×0.5=0.25\delta_1 = \delta_2 \times w = 0.5 \times 0.5 = 0.25. Check: each backward step multiplies by ww once more.
  6. Collect the weight's contributions. Each step uses the weight once, so the weight gets one contribution per step: δt×ht−1\delta_t \times h_{t-1}. The contributions are δ3h2=2.75\delta_3 h_2 = 2.75, δ2h1=0.5×1.5=0.75\delta_2 h_1 = 0.5 \times 1.5 = 0.75, and δ1h0=0.25×1=0.25\delta_1 h_0 = 0.25 \times 1 = 0.25. Their sum is ∂L/∂w=2.75+0.75+0.25=3.75\partial L / \partial w = 2.75 + 0.75 + 0.25 = 3.75. This is the gradient that gradient descent uses to update ww.
  7. Interpretation. The gradient is the sum of one term per time step, and each term is multiplied by how many times ww has been applied since. Check: direct calculation gives h3=w3+w2+2w+3h_3 = w^3 + w^2 + 2w + 3 when h0=1h_0 = 1, so ∂L/∂w=3w2+2w+2=3.75\partial L / \partial w = 3w^2 + 2w + 2 = 3.75 at w=0.5w = 0.5. The two results agree.

The math

Write the recurrence in general form. At step tt the hidden state is ht=f(Wht−1+Uxt)h_t = f(W h_{t-1} + U x_t), where WW and UU are weight matrices shared across time, xtx_t is the input and ff is an activation function. Let the total loss be a sum over time, L=∑tLtL = \sum_t L_t, with LtL_t the loss at step tt. Every gradient below comes from the chain rule applied along the unrolled chain.

Definition: Backpropagation through time

Define the backward signal at step tt as δt=∂L/∂ht\delta_t = \partial L / \partial h_t. Then the gradient with respect to the shared weight matrix is

∂L∂W=∑t∑k≤t(∂Lt∂ht)(∂ht∂hk)∂hk−∂W,\frac{\partial L}{\partial W} = \sum_{t} \sum_{k \le t} \left(\frac{\partial L_t}{\partial h_t}\right) \left(\frac{\partial h_t}{\partial h_k}\right) \frac{\partial h_k^{-}}{\partial W},

where hk−=Whk−1+Uxkh_k^{-} = W h_{k-1} + U x_k is the pre-activation at step kk, and the middle factor is a product of Jacobians (see Jacobian Matrix):

∂ht∂hk=∏j=k+1tdiag⁡ ⁣(f′(hj−)) W.\frac{\partial h_t}{\partial h_k} = \prod_{j=k+1}^{t} \operatorname{diag}\!\big(f'(h_j^{-})\big)\, W.

Step 4 in symbols: the backward recursion

The recurrence says that δt\delta_t is the direct loss gradient at step tt, plus the signal from step t+1t+1 passed back through the local Jacobian. For the scalar example with ff the identity, the local Jacobian is just ww, so

δt=∂Lt∂ht+w δt+1.\delta_t = \frac{\partial L_t}{\partial h_t} + w\, \delta_{t+1}.

With only the final loss, δ3=1\delta_3 = 1 and δ2=w δ3=0.5\delta_2 = w\,\delta_3 = 0.5, matching step 4 of the walkthrough.

Step 6 in symbols: summing over time

The weight appears in ht=w ht−1+xth_t = w\,h_{t-1} + x_t with coefficient ht−1h_{t-1}, so the gradient is

∂L∂w=∑t=13δt ht−1=δ1h0+δ2h1+δ3h2.\frac{\partial L}{\partial w} = \sum_{t=1}^{3} \delta_t\, h_{t-1} = \delta_1 h_0 + \delta_2 h_1 + \delta_3 h_2.

Substituting the numbers from the walkthrough gives 0.25×1+0.5×1.5+1×2.75=3.750.25 \times 1 + 0.5 \times 1.5 + 1 \times 2.75 = 3.75.

Adding the nonlinearity

If f=tanh⁡f = \tanh (the hyperbolic tangent), the local Jacobian at step jj becomes w tanh⁡′(hj−)w\,\tanh'(h_j^{-}), and tanh⁡′(z)=1−tanh⁡2(z)\tanh'(z) = 1 - \tanh^2(z). Because tanh⁡′(z)≤1\tanh'(z) \le 1, 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 γ\gamma, then the contribution from kk steps back is bounded by γk\gamma^{k}. For the scalar case with w=0.5w = 0.5, that bound after 10 steps is 0.510=1/10240.5^{10} = 1/1024. For w=2w = 2 the same product is 210=10242^{10} = 1024. 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.

02468100246steps back in time, ksize of the k-step factor w^k
  • w = 0.5
  • w = 1.2
With w below 1 the signal from far back in time fades to almost nothing; with w above 1 it grows fast. Both are the same repeated multiplication.

Worked examples

Example 1: one step

Example
A single step, hand-computable
  1. Set h0=1h_0 = 1, w=0.5w = 0.5, x1=1x_1 = 1 and the loss L=h1L = h_1.
  2. Forward: h1=wh0+x1=0.5×1+1=1.5h_1 = w h_0 + x_1 = 0.5 \times 1 + 1 = 1.5.
  3. Backward: ∂L/∂w=∂h1/∂w=h0=1\partial L / \partial w = \partial h_1 / \partial w = h_0 = 1.
  4. Interpretation: with only one step there is no chain to pass through, so the gradient is just the previous state, here 11.

Example 2: three steps, the standard case

Example
The walkthrough from the How it works section
  1. Forward values: h1=1.5h_1 = 1.5, h2=2.75h_2 = 2.75, h3=4.375h_3 = 4.375.
  2. Backward signals: δ3=1\delta_3 = 1, δ2=0.5\delta_2 = 0.5, δ1=0.25\delta_1 = 0.25.
  3. Per-step contributions: δ3h2=2.75\delta_3 h_2 = 2.75, δ2h1=0.75\delta_2 h_1 = 0.75, δ1h0=0.25\delta_1 h_0 = 0.25.
  4. Total: 2.75+0.75+0.25=3.752.75 + 0.75 + 0.25 = 3.75.
  5. Check: the polynomial h3=w3+w2+2w+3h_3 = w^3 + w^2 + 2w + 3 has derivative 3w2+2w+23w^2 + 2w + 2, which is 3.753.75 at w=0.5w = 0.5.
  6. Interpretation: the earliest step contributes the least, because its signal has been multiplied by ww twice on the way back.

Example 3: a realistic sequence

Example
A language model over 50 tokens

Suppose a recurrent language model reads a sequence of 50 tokens, and the effective per-step factor along the backward chain is 0.90.9 in one run and 1.11.1 in another.

  1. Factor after 50 steps with 0.90.9: 0.950=0.005150.9^{50} = 0.00515. A signal from the first token reaches the loss at about half of one percent of its size.
  2. Factor after 50 steps with 1.11.1: 1.150=117.41.1^{50} = 117.4. The same signal is amplified more than a hundredfold.
  3. Clipping: if the total gradient norm is 117.4117.4 and the clipping threshold is 55, the gradient is scaled by 5/117.4=0.04265 / 117.4 = 0.0426 to bring its norm down to 55. The direction is kept and the step is no longer destructive; see Gradient Norm Clipping.
  4. 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 0.80.8 over 20 steps gives what size? Answer: 0.820=(0.810)2=0.107372=0.011530.8^{20} = (0.8^{10})^2 = 0.10737^2 = 0.01153, 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 w=0.5w = 0.5, h0=1h_0 = 1, x1=1x_1 = 1, x2=2x_2 = 2, and loss L=h2L = h_2, where ht=wht−1+xth_t = w h_{t-1} + x_t, what is ∂L/∂w\partial L / \partial w?Show answer

Forward: h1=1.5h_1 = 1.5, h2=0.5×1.5+2=2.75h_2 = 0.5 \times 1.5 + 2 = 2.75. Backward: δ2=1\delta_2 = 1, δ1=wδ2=0.5\delta_1 = w \delta_2 = 0.5. Contributions: δ2h1=1.5\delta_2 h_1 = 1.5 and δ1h0=0.5\delta_1 h_0 = 0.5. Total: 1.5+0.5=2.01.5 + 0.5 = 2.0.

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 kk steps back is multiplied by the factor kk times. Products of kk factors give a power, γk\gamma^k. 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. 0.9530=0.21460.95^{30} = 0.2146, 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.

Sources