skip to content

Recurrence and BPTT

A hidden state carried across time steps with one reused weight matrix, unrolled backwards for training. Interviewers start here because the gradient failure it creates is what gating exists to fix.

on this pageshow

explore

questions

19

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

open as a page

Why must a batch of variable-length sequences be padded, and what does the mask do?

level: juniorimportance: must knowfreq 72%

basics

~20 s

A batch must be one rectangular array, so shorter sequences are padded out to the longest length. The mask marks which steps are real, keeping pad steps out of the loss, out of pooling, and out of the state you read.

open as a page

In a vanilla RNN, how does the hidden state at step t depend on earlier inputs?

level: juniorimportance: must knowfreq 78%

basics

~20 s

A vanilla RNN computes h_t = tanh(W_xh x_t + W_hh h_(t-1) + b). Since h_(t-1) was built the same way from h_(t-2), the state at step t is a fixed-size summary of every input so far.

open as a page

How do you window a half-hourly electricity load series into training examples for a recurrent forecaster?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Slide a fixed-length input window over the series, pairing each window with the next H values as its target. Size the window to cover the dominant seasonal cycle, split chronologically first, and drop any window whose target crosses the split.

open as a page

How do sequence-to-label, per-step tagging and sequence-to-sequence framings of a recurrent model differ?

level: juniorimportance: must knowfreq 70%

basics

~20 s

They differ in output shape. Sequence-to-label emits one prediction for the whole input, per-step tagging emits one prediction aligned to each input step, and sequence-to-sequence emits a new sequence whose length need not match the input.

open as a page

Why does a vanilla RNN reuse the same weight matrices at every time step?

level: middleimportance: must knowfreq 62%

basics

~20 s

Reusing one input-to-hidden matrix, one hidden-to-hidden matrix and one bias makes the layer a single function applied repeatedly. The parameter count is then independent of sequence length, any length runs, and what is learned at step 1 applies at step 900.

open as a page

Why can a bidirectional recurrent layer not be used in a live captioning system?

level: middleimportance: must knowfreq 58%

basics

~20 s

A bidirectional layer runs a second recurrence from the end of the sequence back to the start, so its output at any step depends on steps that come after it. Live captioning has no future available yet.

open as a page

Why do gradients vanish across time steps in a simple RNN trained with BPTT?

level: middleimportance: must knowfreq 72%

basics

~10 s

Backprop through time multiplies by the same recurrent Jacobian once per step. Factors below one in magnitude shrink that product geometrically, so gradients from distant steps arrive at zero; factors above one explode it.

open as a page

Why does backprop through time sum, not average, the per-step gradients of shared recurrent weights?

level: middleimportance: should knowfreq 46%

basics

~20 s

The same matrix is used at every time step, so the loss depends on it along T separate paths and its total derivative is the sum of those contributions. Averaging would give the gradient of a rescaled objective instead.

open as a page

Should a classifier that reads an RNN's final hidden state use pre- or post-padding?

level: middleimportance: should knowfreq 45%

basics

~20 s

Post-padding puts pad steps last, so the state at the final index is the state after the pads, not after the last real token. Pre-padding hides that, but the real fix is to read each sequence's state at index length minus one.

open as a page

Why does a recurrent forecaster that feeds its own predictions back in drift over a 14-day horizon?

level: middleimportance: should knowfreq 58%

basics

~20 s

Because every step after the first is conditioned on a predicted value rather than an observed one. Small one-step errors re-enter as inputs and accumulate across the horizon, and a one-step training loss never penalised the 14-step trajectory at all.

open as a page

How do you choose the truncation window length in truncated backprop through time?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Choose the shortest window that still spans the dependency the model must learn, because the gradient reaches back only that far. Backward work and retained per-step values grow roughly linearly with the window, so longer is not free.

open as a page

When should an RNN carry its hidden state across mini-batches on a never-ending accelerometer stream?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Carry it only when each batch row truly continues that row's stream from the previous batch: feed batches in order, stop the gradient at each handoff, and reset the state at real breaks such as the device coming off.

open as a page

How should you scale 3,000 SKU series whose volumes span four orders of magnitude for one shared forecaster?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Scale each series by its own statistics, not the pooled distribution. A global scaler squashes a two-unit-a-day SKU toward a constant while a 20,000-unit SKU dominates the loss. Fit each scale on training data only, and invert it on forecasts.

open as a page

A third stacked recurrent layer barely improves your tagger - what do you check?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Check whether depth is the bottleneck at all: that the stack is wired correctly, that the extra layer is actually training, and whether the remaining errors come from missing data or missing context rather than from too little capacity.

open as a page

Why does a sentiment RNN learn the final clause of a review but ignore the opening sentence?

level: seniorimportance: should knowfreq 46%

basics

~20 s

Gradients from early time steps decay geometrically on the way back, so the opening sentence gets almost no learning signal. Recent steps keep full-size gradients, so the model quietly becomes a short-memory model reading the ending.

open as a page

With 120 daily observations from a single ATM, would you ship a recurrent forecaster or a classical model?

level: principalimportance: should knowfreq 44%

basics

~20 s

Ship the classical model. One short series yields roughly a hundred overlapping windows and about seventeen weekly cycles, far too little to fit thousands of recurrent weights. Deep forecasters earn their keep by pooling many related series, not on one short one.

open as a page

Why group a speech corpus of 1-30 second utterances into similar-length batches?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

Batching similar lengths together shrinks each batch's padded width, so far less compute goes into pad steps. The price is correlated batches: examples no longer arrive in random order, so you shuffle inside buckets and randomise bucket order every epoch.

open as a page

When a long-sequence RNN ignores distant context, how do you tell a gradient-reach limit from a data problem?

level: principalimportance: nice to knowfreq 27%

basics

~10 s

Separate them with controlled experiments: a synthetic task with a known dependency lag measures the model's reach independently of your data, and a short-context baseline measures whether distant context carries signal at all.

open as a page