Interactive Explainer
Backprop Through Time
To train an RNN you unroll it into a deep feed-forward graph with shared weights, then run ordinary backprop across it. Walk the backward pass step by step: at each state, watch its gradient accumulate two pieces — one from the loss right here, one carried back from the future — and see how truncation cuts the chain and how the per-step product quietly vanishes or explodes.
Unroll, then backprop
An RNN reuses one cell at every timestep, so in time it looks like a very deep network whose layers all share the weights $w_h, w_x, b$. Draw it unrolled and it is just a chain: inputs $x_t$ come in from below, an output/loss $\mathcal{L}_t$ is read off above, and the hidden state is threaded left to right.
Backprop on this graph has a name — backpropagation through time. Its one wrinkle: every hidden state feeds two places, so its gradient is a sum of two incoming contributions.
Walk the backward pass
Seven timesteps, unrolled. Press Step backward to move the gradient frontier from the last step toward the first. At each state the two contributions light up: the vertical arrow is the local loss gradient, the horizontal arrow is credit carried back from $h_{t+1}$. The truncation $K$ slider severs the chain — greyed states are cut off, and no credit ever reaches them.
Why long-range credit is so fragile
Trace the credit that reaches an early state $h_t$ from a late loss $h_T$. It has to pass through every recurrent edge in between, and each edge contributes the same Jacobian factor. The whole path is therefore a product:
- If $|w_h\,\sigma'| < 1$, that product decays exponentially — the vanishing gradient. Distant timesteps get essentially zero learning signal.
- If $|w_h\,\sigma'| > 1$, it grows exponentially — the exploding gradient, which gradient clipping papers over but never truly fixes.
- Only $|w_h\,\sigma'| \approx 1$ preserves credit over long ranges — a razor's edge that plain RNNs cannot reliably sit on.