Core

RNNs and the vanishing gradient

RNNs reuse one state transition through time, but repeated Jacobian products can erase or amplify long-range learning signals.

Updated

1

Concept

A simple recurrent neural network updates one hidden state as it reads a sequence:

ht=ϕ(Whhht1+Wxhxt+b),yt=Whyht+c.h_t=\phi(W_{hh}h_{t-1}+W_{xh}x_t+b),\qquad y_t=W_{hy}h_t+c.

The same matrices are reused at every position. Unrolling the equations reveals a deep network whose layers share parameters, with sequence time acting like depth.

The hidden state offers memory, but every new update transforms the old state. To learn that an early token caused a much later error, training uses backpropagation through time. The computational graph is unrolled, and gradient contributions from every use of the shared parameters are accumulated. Exact full unrolling costs memory proportional to sequence length.

The derivative from a later state to an earlier one contains a product of recurrent Jacobians. In a simplified linear recurrence, powers of WhhW_{hh} appear directly. If relevant directions repeatedly contract by factors below one, the product approaches zero: the vanishing-gradient problem. If they expand above one, the product can grow rapidly: exploding gradients. Nonlinear derivatives multiply into the same chain.

Sigmoid and tanh saturate when pre-activations have large magnitude. In saturated regions their derivatives are small, adding another shrinking factor at each step. A vanishing gradient means early states receive almost no information about how they should change to reduce a distant loss. The forward state may still carry some signal; the critical failure is that training cannot assign long-range credit effectively.

Exploding gradients cause huge, unstable updates and non-finite values. Gradient clipping rescales or caps a gradient when its norm exceeds a threshold. This protects optimization and is widely useful, but it treats the magnitude after differentiation. It does not create a learning signal where repeated derivatives already erased one.

Truncated backpropagation limits the unrolled window, saving memory and compute. State may continue forward across chunks while gradients stop at boundaries. This explicitly limits credit assignment and can bias learning, although it may be practical when relevant dependencies are mostly local. The chunking and state-detachment policy should be part of the reported model.

Initialization can keep recurrent dynamics near a stable scale, and orthogonal matrices help preserve norms in simplified settings. Normalization and residual or skip connections can improve paths. LSTM and GRU cells introduce additive, gated state routes designed to retain information and gradients. Attention later supplies direct edges between distant positions, shortening the path dramatically.

RNNs remain useful when streaming state, bounded memory, or low per-step compute matters. Their limitation is not that they categorically forget everything, but that repeated transformations make learning long dependencies fragile and sequential execution restricts training parallelism. The unrolled view unifies both facts: one elegant shared transition becomes a very deep chain, and the chain’s Jacobian geometry controls what the past can teach the future.

A diagnostic synthetic task can place one relevant marker at increasing distances from the prediction. Plot accuracy and the gradient norm at the marker against distance. This separates a vague claim of “long memory” from measurable degradation. Run the same protocol with and without clipping: clipping may stabilize exploding runs, but a curve still collapsing toward chance reveals that distant credit was not restored.

2

Explain it like I am five

A message crosses fifty people in a whisper chain. Each person applies the same habit: soften loud words, emphasize familiar ones, and pass a shortened summary. If every handoff retains 80 percent of a detail, after many steps it nearly disappears; if every handoff amplifies it, the message blows up. An RNN's backward signal crosses repeated transformations in the same way, so long-distance credit depends on a product of local sensitivities.

3

Teach it back

Unroll a simple RNN conceptually and explain vanishing and exploding gradients using repeated Jacobian products, plus common mitigations.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

An RNN computes h_t=φ(W_hh h_{t−1}+W_xh x_t+b) with shared weights. Backpropagation through time multiplies derivatives across every intervening step. If typical singular effects are below one, distant gradients shrink; above one, they grow. Saturating activations worsen shrinkage. Gradient clipping limits explosions but does not restore vanished signal. Careful initialization, shorter unrolls, normalization, gated cells, residual paths, and attention address different parts.

4

Check your understanding

1. Why does a distant RNN gradient involve many multiplications?
Answer and explanation

The hidden state is repeatedly transformed through time — Backpropagation through time applies the chain rule across each recurrent transition.

2. What does gradient clipping primarily address?
Answer and explanation

Exploding gradient magnitude — Clipping bounds large updates; it cannot recreate a signal already multiplied toward zero.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Yoshua Bengio, Patrice Simard, and Paolo Frasconi (1994). Learning long-term dependencies with gradient descent is difficult.