skip to content

What does backpropagation through time do to a recurrent network's computation graph?

level: juniorimportance: must knowfreq 66%

answer

  1. the loop has to become a chain
  2. one copy of the cell per step
  3. copies share parameters, not activations
  4. backward runs last step to first
  5. depth in time equals sequence length

basics

~10 s

Backpropagation through time unrolls the recurrent loop into a chain with one copy of the cell per time step, then runs ordinary backpropagation over that finite graph. All copies share one set of weights.

solid answer

~50 s

Training a recurrent network starts by unrolling it. The loop `h_t = f(W h_(t-1) + U x_t + b)` over a length-T sequence is rewritten as a feed-forward chain of T copies of the same cell, one per step, with the inputs and any per-step losses attached. That graph is finite and acyclic, so ordinary backpropagation applies: gradient enters at the loss (at the last step for a sequence label, at every step for per-step losses) and flows backwards from step T down to step 1. The crucial detail is that all T copies share one set of parameters — they are T *uses* of one `W`, not T independent layers — so each copy contributes gradient to the same matrices. Both the backward work and the per-step forward values that must be retained grow with T, which is why long sequences get truncated.

go deeper

for a junior

Be ready to say that training unrolls the loop into a chain of per-step copies, that every copy uses the same weights, and that the backward pass walks from the last step to the first.

for a middle

Explain where per-step losses inject gradient, why the forward pass must keep each step's activations, and how the unrolled depth grows with the sequence length rather than with the parameter count.

for a senior

Show that unroll length drives step time and retained forward state, and that this is exactly what forces windowed training on long streams. Interviewers want the cost consequence, not just the picture.

for a principal

Own the training-cost argument for recurrence itself: the backward sweep over time is strictly sequential and its depth is the sequence length. Be able to say when that constraint justifies a different modelling family.

## What "unrolling" means A recurrent network is written as a loop. At each time step `t` it takes the input `x_t` and the previous hidden state `h_(t-1)` and produces a new hidden state: `h_t = f(W h_(t-1) + U x_t + b)` where `f` is an elementwise nonlinearity such as tanh, `W` is the hidden-to-hidden matrix, `U` the input-to-hidden matrix, `b` the bias, and `h_0` is a zero (or learned) starting state. Written this way the graph has a cycle: `h` feeds back into itself. Gradient-based training needs a graph without cycles, because a derivative has to be taken along a definite path from parameters to loss. Backpropagation through time (BPTT) removes the cycle by *unrolling*. For a concrete sequence of length T, the loop is expanded into T copies of the cell laid out in a chain: copy 1 consumes `h_0` and `x_1` and emits `h_1`; copy 2 consumes `h_1` and `x_2` and emits `h_2`; and so on to copy T. Nothing feeds backwards, so the result is an ordinary feed-forward network that happens to be T layers deep — deep in *time* rather than in stacked layers. Backpropagation then applies to it exactly as it applies to any other feed-forward network. ## What the copies share, and what they do not The copies share **parameters**. There is one `W`, one `U`, one `b`, and every copy uses those same tensors. Unrolling does not create T layers with T sets of weights; it creates T *uses* of one set of weights. This is the single most common misunderstanding of the picture, and it is what makes the backward pass interesting: a parameter that is used T times receives a gradient contribution from each of those uses. The copies do **not** share activations. Each step has its own `x_t`, its own `h_t`, and its own intermediate pre-activation values. Those per-step values are what the backward pass needs in order to evaluate the local derivatives, so the forward pass has to keep them around until the backward pass consumes them. ## Where the loss enters Two shapes are common, and they change where gradient is injected: - **Sequence-to-label.** One loss is computed from the final hidden state (or a pooled summary). The gradient enters at step T only, and travels backwards from there through T-1, T-2, ... to step 1. - **Per-step output.** A loss is computed at every step, for example next-token prediction over a paragraph of text or a tag per token. Every step injects its own gradient at its own output, and the total loss is a sum (or mean) over steps. Step t's parameters then accumulate gradient from step t's own loss *and* from every later loss whose path runs back through `h_t`. In both shapes the backward pass runs from the last step to the first, because the gradient at step t depends on the gradient already computed at step t+1. That strict ordering is why the backward pass over time is inherently sequential. ## What it costs Both the forward and the backward pass do work proportional to T, and the forward values that must be retained for the backward pass also grow with T. Doubling the number of unrolled steps roughly doubles the backward work and roughly doubles the retained per-step values. For a 30-step sentence that is nothing. For an hour of a physiological signal sampled at 250 Hz — 900,000 steps in one "sequence" — a full unroll is not something you can build at all, and training instead processes the stream in windows with the gradient cut at the window boundary. Note that sequence length also has to be handled per batch: sequences in the wild are not all the same length, so the unrolled depth differs from batch to batch. Nothing about BPTT requires a fixed T; the unrolled chain is simply built to whatever length the current batch needs. ## Why interviewers start here Almost every hard fact about recurrent training falls out of this picture. The gradient of the shared weights is a sum over steps because the weights are used once per step. Long-range learning is hard because the gradient must survive a trip back through many steps. Truncation exists because the unrolled chain's cost is linear in its length. Gating architectures exist because of what happens along that long backward path. A candidate who can draw the unrolled chain and point at where the weights are shared and where the loss enters can reason their way to all of it; a candidate who cannot will guess. ## A compact summary Unroll the loop into T copies of one cell; attach inputs and losses; run backpropagation over the resulting acyclic graph from step T down to step 1; every copy contributes gradient to the same shared parameters; the cost of all of this is linear in how many steps you unrolled.

  • Where does the gradient enter the unrolled graph when there is a loss at every time step?
    Each step's loss injects its own gradient at that step's output. Step t's parameters then accumulate gradient from step t's own loss and from every later loss whose backward path runs through `h_t`. With a single sequence-level loss instead, gradient enters only at the final step and everything earlier is reached by travelling backwards along the chain.
  • Does the forward pass have to store anything for the backward pass here?
    Yes. Each step's hidden state and the intermediate values the cell's local derivatives need must survive until the backward pass consumes them. Parameters are shared across steps, but activations are not — every step has its own. That is why what you retain scales with how many steps you unrolled, not with the parameter count.
  • Does BPTT require every sequence in the dataset to have the same length?
    No. The unrolled chain is built to whatever length the current batch needs, so T can differ from batch to batch; the parameters are the same objects regardless. Fixed-length batching is a convenience for packing sequences together, not a requirement of the algorithm.

It is like photocopying one layer T times and stapling the copies into a deep feed-forward network: deep in time rather than in stacked layers, with every copy printed from the same original.

saying these in an interview costs you the question

  • Says the recurrent loop is differentiated in place, without unrolling
  • Treats each time step as having its own separate weight matrices
  • Thinks the gradient only ever reaches the final time step
  • Claims the sequence length must be fixed before training
  • Believes unrolling multiplies the parameter count by the sequence length

context