Part XI — Sequence Models: RNNs, LSTMs, Attention & The Transformer Block · Chapter 3

Vanishing gradients in RNNs & LSTMs

Hook

Chapter 6 ended with a warning: pushing a recurrent weight hard enough to get a confident answer, repeated over many timesteps, is exactly the vanishing-gradient problem from Part III — just running through time instead of through layers.

Intuition
timesteps = 1 — RNN gradient ≈ 1.00e+0, LSTM cell gradient ≈ 1.00e+0

An RNN (Recurrent Neural Network) unrolled over TT timesteps is a TT-layer chain with the same weight reused at every layer — Part III's vanishing-gradient math, applied to time. Slide the timestep count and watch the plain RNN's gradient (dim line) collapse by roughly an order of magnitude every step, while an LSTM (Long Short-Term Memory)'s cell-state gradient (bold line) barely bends.

Formalize

A plain RNN's gradient reaching timestep 1 from timestep TT is a product of T1T{-}1 factors, each one a squashing derivative times the recurrent weight:

hTh1=t=2Ttanh(zt)Wh\frac{\partial h_T}{\partial h_1} = \prod_{t=2}^{T} \tanh'(z_t)\, W_h
  • hTh1\dfrac{\partial h_T}{\partial h_1} — the gradient of the last hidden state with respect to the first: how much a change at step 1 still affects step TT.
  • tanh(zt)\tanh'(z_t) — the derivative of the tanh activation at timestep tt's pre-activation, a value always less than 11 that shrinks the gradient at every step.
  • WhW_h — the recurrent weight reused at every timestep.
  • T,tT, t — the total number of timesteps, and tt the index running from 22 up to TT.

An LSTM keeps a separate cell state ct=ftct1+itgtc_t = f_t \, c_{t-1} + i_t \, g_t, and its backward path through the cell state is just:

cTc1=t=2Tft\frac{\partial c_T}{\partial c_1} = \prod_{t=2}^{T} f_t
  • cTc1\dfrac{\partial c_T}{\partial c_1} — the gradient of the final cell state with respect to the first cell state.
  • ctc_t — the LSTM's cell state at timestep tt: a separate running memory, distinct from the hidden state.
  • ftf_t — the forget gate at timestep tt: how much of the previous cell state to keep, between 00 and 11.
  1. No squashing-derivative factor

    Unlike the plain RNN's gradient, there's no tanh\tanh' factor in this product — nothing here is forced below 11 by a squashing derivative at every step.

  2. A forget gate near 1 preserves the gradient

    If the forget gate ftf_t stays close to 11, this product barely shrinks at all — the "constant error carousel" that gives LSTMs their long memory.

Play
timesteps = 5 — LSTM is 9.37e+3× larger than the plain RNN

Both curves use the exact same weight value — this isn't about the LSTM having bigger numbers to work with. The plain RNN multiplies by a squashing derivative and the weight at every step; the LSTM's cell-state path multiplies by only the forget gate. Removing one shrinking factor per timestep is the entire difference, and over enough timesteps it compounds into an astronomical gap.

Worked example

At weight (or forget gate) 0.90.9 and 1010 timesteps:

  1. Plain RNN gradient

    Each timestep multiplies by that step's own tanh(zt)wh\tanh'(z_t)\cdot w_h. The recurrence's input is constant every step, so ztz_t converges fast toward a fixed point (1.86\approx1.86), where tanh(zt)0.094\tanh'(z_t)\approx0.094 — a steady-state per-step factor of 0.094×0.90.084\approx0.094\times0.9\approx0.084. Multiplying nine such factors together (nine transitions across ten timesteps): 0.08492×10100.084^9\approx2\times10^{-10} — the same order of magnitude as the actual 2.9×1010\approx2.9\times10^{-10}, the small difference coming from the first couple of steps, before ztz_t has fully settled, contributing slightly larger factors. Nine orders of magnitude gone either way.

  2. LSTM cell-state gradient

    0.990.3870.9^9 \approx 0.387 — still very much alive.

  3. Compare

    The ratio between them is over a billion, using the same 0.90.9 on both sides.

Checkpoint

Slide the number of timesteps until the LSTM’s cell-state gradient is more than 1e+6 times larger than the plain RNN’s.

timesteps = 3 — ratio = 8.11e+1
Drag the slider to try it
Summary
cTc1=t=2Tftvs.hTh1=t=2Ttanh(zt)Wh\frac{\partial c_T}{\partial c_1} = \prod_{t=2}^{T} f_t \qquad \text{vs.} \qquad \frac{\partial h_T}{\partial h_1} = \prod_{t=2}^{T} \tanh'(z_t)\, W_h

Gating doesn't eliminate the product-of-many-factors structure that causes vanishing gradients — it just removes the squashing derivative from one of the paths, so that path can carry a signal across far more timesteps before it's gone. Everything built so far — convolution, pooling, recurrence, gating — has operated on numeric vectors. The next few chapters turn to how text becomes those vectors in the first place.