Vanishing Gradients & Gated RNNs

Two-panel comic: 'The Problem' shows a robot carrying a 'MEMORY' package through shrinking doors from Step 1 to Step 100, getting smaller at each door (Vanilla RNN); 'The Fix' shows a robot with the same package on a conveyor belt, with small workers labeled 'FORGET' and 'ADD' managing items on the belt (LSTM)
What You’ll Learn

You’ll learn why repeated multiplication makes RNN gradients vanish or explode across long sequences, and why that blocks long-range learning. You’ll also see how LSTMs and GRUs preserve information with gates, and why transformers avoid the same bottleneck with direct token-to-token connections.

In the backward pass, we saw how gradients flow through time via repeated multiplication by WhhTW_{hh}^T. Now we’ll see why this causes a fundamental problem — and the elegant architectural solutions that fix it.

The Problem

Train an RNN on long sequences and plot gradient magnitude at each timestep:

Gradient Magnitude Through Time

||dh|| (log)
6e-3
0
8e-3
0.01
2
0.01
0.02
4
0.02
0.03
6
0.04
0.05
8
0.07
0.09
10
0.12
0.15
12
0.20
0.26
14
0.34
0.45
16
0.59
0.77
18
1.0
19
Timestep (t)
Gradients decaying: With |λ| = 0.90, gradients shrink to 0.006 at t=0. Short sequences work, but long-range dependencies are hard to learn.
Effective per-step factor: |λ| × tanh'(h) ≈ 0.77 — over 19 steps: 0.77^19 = 0.0062

What to look for: The bars represent gradient magnitude at each timestep. Notice how bars on the left (early tokens) are dramatically smaller than the right (near the loss). The gradient doesn’t just decrease, it drops exponentially. By t=0, almost no learning signal remains.

Try it yourself: Drag the |λ| slider below 0.9 to see gradients vanish, or above 1.2 to see them explode. Then increase the sequence length T to see how the problem gets worse with longer sequences.

Consequence: early tokens don’t learn. The network can’t capture long-range dependencies. A word at the start of a sentence has almost no influence on the loss at the end.


Why Gradients Vanish

The Core Intuition

Each backward step through time multiplies the gradient by a factor. Think of it like a game of telephone: the message (gradient) gets distorted at every step. If each step slightly shrinks the signal, after enough steps the message is gone entirely.

When you repeatedly multiply a number by a constant, the result grows or shrinks exponentially. Multiplying by 0.9 a hundred times: 0.9100≈0.000030.9^{100} \approx 0.00003. Multiplying by 1.1 a hundred times: 1.1100≈13,7801.1^{100} \approx 13{,}780.

The same thing happens with weight matrices in an RNN. The “multiplication factor” of a matrix is determined by its largest eigenvalue (how much the matrix stretches vectors). The key intuition is: repeated matrix multiplication either shrinks or grows signals exponentially, and for typical randomly-initialized weight matrices, shrinking (vanishing) is the default.

Multiplication factorGradient behaviorAfter 100 steps
0.9Vanishes exponentially0.9100≈0.000030.9^{100} \approx 0.00003
1.0Preserved (ideal but unstable)1.0100=11.0^{100} = 1
1.1Explodes exponentially1.1100≈13,7801.1^{100} \approx 13{,}780

On top of that, the tanh activation further shrinks gradients at every step. When hidden state values are large (near -1 or 1), tanh is nearly flat, so its derivative is near zero. These two effects compound: even if the weight matrix factor is near 1, saturated tanh neurons still kill the gradient.

Hidden state valueTanh derivativeEffect
0.01.0Full gradient flow
±0.50.75Mild attenuation
±0.90.19Heavy attenuation
±0.990.0298% gradient loss per step
Fill in the Blanks
If the largest eigenvalue of W_hh is 0.95, after 50 timesteps the gradient is multiplied by 0.95^50 ≈ . This means only about % of the original gradient survives.
Fill in every blank, then check your answer.

Why This Matters

Consider: “The cat, which was sitting on the mat in the corner of the room, was sleeping.”

The subject “cat” determines “was” (not “were”), but they’re 14 words apart. With vanishing gradients, the signal from “was” barely reaches “cat” — the model can’t learn this dependency.

Practical Limits of Vanilla RNNs

Sequence LengthCan RNN Learn Long-Range Dependencies?
< 20 tokensUsually works ✅
20–50 tokensStruggles ⚠️
> 50 tokensFails ❌
Quick Check
Gradient clipping solves the vanishing gradient problem. True or false?
Your turn
Choose an answer to check your understanding.

Attempted Fixes (Partial)

FixHelps withLimitation
Gradient clippingExplosion ✅Can’t fix vanishing ❌
Orthogonal initializationInitial gradient flow ✅Training changes WhhW_{hh}, orthogonality lost ❌
Careful learning rateStability ✅Doesn’t address root cause ❌

The real fix requires a change in architecture — replacing multiplicative state updates with additive ones.


The Solution: LSTM

The Key Insight: Addition vs Multiplication

In vanilla RNN, the hidden state is overwritten at each step via matrix multiplication. Gradients flow through tanh and decay. What if instead of overwriting, we added to the state?

LSTM introduces a cell state, a separate memory track updated via addition instead of multiplication. Think of it like a conveyor belt running above the main processing: information can be added to the belt or removed from it, but by default it just passes through unchanged. The ++ creates a gradient highway: if the forget gate is near 1, gradients pass through unchanged.

Step through one timestep to see how it works:

LSTM Cell: One Timestep

1/6
Initial state
Cell state (memory)
ht-1
[+0.40, -0.20]
xt
[1.00, 0.00]
The cell carries forward two things: the cell state c (long-term memory on the conveyor belt) and the hidden state h (short-term output). Both feed into the gates along with the new input x.

What to look for: Watch how the cell state (top row) flows through. The forget gate scales the old memory, then new information gets added on top. That + sign is the whole trick. In a vanilla RNN, information passes through matrix multiply and tanh at every step (shrinking). Here, it passes through a simple addition (preserving).

The Four Gates

Each gate looks at the previous hidden state and the current input, then outputs a value between 0 and 1 (via sigmoid): 0 means “block everything,” 1 means “let everything pass.”

GateWhat it does
Forget gate (ftf_t)Decides how much old memory to keep (0 = forget all, 1 = keep all)
Input gate (iti_t)Decides how much new information to write to memory
Candidate (c~t\tilde{c}_t)Proposes new information to potentially write
Output gate (oto_t)Decides what part of the memory to expose as output

Worked Example

Walk through one timestep with the values from the visualization above. Previous cell state is [0.80,0.50][0.80, 0.50]:

Step 1: Forget gate decides what to keep. ft=[0.90,0.10]f_t = [0.90, 0.10]. Keep 90% of dimension 1, discard 90% of dimension 2. ft⊙ct−1=[0.90×0.80, 0.10×0.50]=[0.72, 0.05]f_t \odot c_{t-1} = [0.90 \times 0.80,\ 0.10 \times 0.50] = [0.72,\ 0.05]

Step 2: Input gate + candidate decide what to write. it=[0.30,0.80]i_t = [0.30, 0.80] and c~t=[−0.60,0.70]\tilde{c}_t = [-0.60, 0.70]. it⊙c~t=[0.30×(−0.60), 0.80×0.70]=[−0.18, 0.56]i_t \odot \tilde{c}_t = [0.30 \times (-0.60),\ 0.80 \times 0.70] = [-0.18,\ 0.56]

Step 3: Cell state update via addition. ct=[0.72, 0.05]+[−0.18, 0.56]=[0.54, 0.61]c_t = [0.72,\ 0.05] + [-0.18,\ 0.56] = [0.54,\ 0.61]

Step 4: Output gate controls what to expose. ot=[0.70,0.50]o_t = [0.70, 0.50]. ht=ot⊙tanh⁡(ct)=[0.70×0.49, 0.50×0.54]=[0.35, 0.27]h_t = o_t \odot \tanh(c_t) = [0.70 \times 0.49,\ 0.50 \times 0.54] = [0.35,\ 0.27]

The cell state updated through addition, not through a weight matrix and tanh. That’s why gradients survive.

Fill in the Blanks
The LSTM cell state is updated via (not multiplication), which creates a gradient that prevents vanishing.
Fill in every blank, then check your answer.

Why LSTM Prevents Vanishing Gradients

Because the cell state is updated by adding (not multiplying through tanh), the gradient flows straight back through time along the cell state highway. The only thing that can weaken it is the forget gate, which is typically learned to be close to 1 for important information.

Compare the two architectures over 100 timesteps:

ArchitectureWhat happens to gradientsAfter 100 steps
Vanilla RNNMultiplied through weight matrix and tanh at each step≈0.00003\approx 0.00003 (vanished)
LSTMOnly scaled by the forget gate at each step0.99100≈0.370.99^{100} \approx 0.37 (10,000x better)

The forget gate is a learned value near 1, not a full matrix multiplication through tanh. This is the fundamental difference.


GRU: A Simpler Alternative

GRU achieves similar results with only 2 gates and 1 state (no separate cell state):

How GRU Works

GRU has two gates:

  • Reset gate (rtr_t) — controls how much of the previous hidden state to use when computing the new candidate. Setting it to 0 lets the model “forget” the past and start fresh.
  • Update gate (ztz_t) — controls the balance between keeping the old state and adopting the new candidate. It replaces LSTM’s separate forget and input gates — they’re tied together since keeping old info (1−zt1 - z_t) and writing new info (ztz_t) must sum to 1.

The update gate ztz_t works like a slider: zt=0z_t = 0 means “keep the old state entirely” (maximum memory), while zt=1z_t = 1 means “replace with the new candidate entirely.”

LSTM vs GRU

AspectLSTMGRU
Gates4 (forget, input, candidate, output)2 (reset, update)
States2 (hh and cc)1 (hh only)
ParametersMore~25% fewer
PerformanceSimilarSimilar
Best forMaximum capacitySpeed / simplicity
Fill in the Blanks
GRU ties the forget and input gates into a single gate. When this gate is 0, the hidden state is unchanged.
Fill in every blank, then check your answer.
Quick Check
What happens in an LSTM if the forget gate f_t = 1 for all timesteps?
Your turn
Choose an answer to check your understanding.


Connection to Transformers

You’ll learn about self-attention in detail in Self-Attention. For now, here’s a preview of why Transformers solve the gradient problem.

In self-attention (covered later), every token can directly “look at” every other token — no matter how far apart they are. The gradient from any token to any other token passes through a single multiplication, not a chain of multiplications that grows with distance. This is the key difference:

ArchitectureHow gradients travelMultiplications for distance dd
Vanilla RNNThrough dd timesteps, each with matrix multiply + tanhdd (vanishes)
LSTMThrough dd forget gatesdd (much better)
TransformerDirect connection between any two tokens1 (constant!)

This is a fundamental reason why Transformers replaced RNNs for most tasks.

Quick Check
A vanilla RNN, an LSTM, and a Transformer all process a 200-token sentence. For the gradient to flow from token 200 back to token 1, how many multiplicative steps does each require?
Your turn
Choose an answer to check your understanding.

When to Use What

Need to model sequences?
├── Sequence length < 50? → Vanilla RNN might work
├── Can parallelize?
│   ├── Yes → Transformer (preferred)
│   └── No → LSTM/GRU
└── Training speed critical?
    ├── Yes → GRU
    └── No → LSTM

Modern Perspective

Since 2017, Transformers have largely replaced LSTMs for most NLP tasks. LSTMs remain useful for:

  • Streaming/online processing (one token at a time)
  • Memory-constrained deployments
  • Time series with strict ordering

Quick Summary

ConceptWhat HappensIntuition
Vanishing gradientsRepeated multiplication shrinks gradients exponentially over timestepsLike a game of telephone — the message fades with each retelling
Exploding gradientsRepeated multiplication grows gradients exponentiallyGradient clipping caps the magnitude, but can’t fix vanishing
LSTM cell stateA separate memory track updated via addition, not multiplicationA conveyor belt — information flows through by default, gates add or remove items
Forget gateLearned value near 1 that controls how much old memory to keepThe “keep or discard” dial for each piece of stored information
GRUSimplified 2-gate design that ties forget and input into one update gateSame idea as LSTM but with a single slider between “keep old” and “write new”
Transformer advantageDirect connections between all tokens — constant gradient pathNo chain of multiplications, so no vanishing regardless of distance

Learning Checklist
  • I can explain why repeated multiplication causes gradients to vanish (or explode) over long sequences
  • I can describe how the LSTM cell state uses addition to create a gradient highway
  • I can name the four LSTM gates and explain what each one controls
  • I can explain how GRU simplifies the LSTM design by tying forget and input gates together
  • I understand why gradient clipping helps with explosion but not vanishing
  • I can explain why Transformers have an advantage over both vanilla RNNs and LSTMs for long-range dependencies

Next Steps

Before building full transformer blocks, there’s one more building block: normalization and skip connections. These techniques stabilize training and appear in every modern architecture. Next: Layer Norm & Residuals →