3.5.3 · D2Sequence Models

Visual walkthrough — Vanishing gradients in RNNs

2,189 words10 min readBack to topic

This page rebuilds the central result of the parent note from absolute zero. We will draw every idea before we write a single symbol. By the end you will see why a signal that has to travel back through 50 time steps arrives as a whisper — and why that one fact broke plain RNNs.

Prerequisite ideas we lean on: Backpropagation Through Time, Activation Functions. The cures we point to at the end: LSTM Architecture, GRU Architecture, Gradient Clipping, Residual Connections, Attention Mechanisms.


Step 1 — What is a hidden state, and why a chain?

WHAT. An RNN reads a sequence one item at a time (word 1, word 2, …). After reading item it holds a memory vector called the hidden state, written . Each new memory is built from the previous memory plus the new input.

WHY. This is the only way a network with fixed size can summarise a sequence of any length: it keeps overwriting one memory slot. That overwriting is exactly what creates a long chain — and the chain is where trouble lives.

PICTURE. Look at the beads on a string below. Each bead is fed forward into the next bead by an arrow. The input (the peach box) is stirred in at every bead.

Figure — Vanishing gradients in RNNs

Step 2 — What is a gradient, and which one do we want?

WHAT. Training means: the network makes a mistake at the end, measured by a number (the loss). We want to know how much each early memory was to blame. That "how much to blame" is the gradient — literally "if I nudge a tiny bit, how much does the loss change?"

WHY. We can only fix what we can blame. If the blame signal for arrives as zero, the early part of the sequence is never corrected — the network stays deaf to long-range clues like the word cat deciding was vs were.

PICTURE. The forward beads (Step 1) now get a backward red arrow layered on top: blame flows from the loss at the far right back toward on the left.

Figure — Vanishing gradients in RNNs

Step 3 — Why the trip unfolds into a product

WHAT. A nudge at can only reach by passing through every bead in between: . The chain rule of calculus says: to compose steps, you multiply their local sensitivities.

WHY. Each bead only listens to the one just before it. There is no shortcut edge. So the total sensitivity is the product of every one-step sensitivity along the road.

PICTURE. Each link in the string is stamped with a little factor . The whole trip is all those stamps multiplied together.

Figure — Vanishing gradients in RNNs

WHAT. Open up a single link. Because with , differentiating the composition gives two pieces multiplied.

WHY. The chain rule again, but within one step: first the squasher contributes its slope, then the linear mixer contributes itself. Two dials on every link.

PICTURE. Each link is a two-stage gearbox: dial ① is the slope (a value between and ), dial ② is the memory-mixer .

Figure — Vanishing gradients in RNNs
Figure — Vanishing gradients in RNNs

WHAT. Stack identical-ish gearboxes in a row and multiply. Using the size ("norm") of a product being at most the product of sizes, we get a bound.

WHY. We cannot compute the exact matrix product for every sequence, but we can bound its magnitude — and the bound already tells the whole doom story.

PICTURE. A ladder of factors. Since dial ① , the surviving per-step multiplier is essentially (the "stretch factor" of the mixer). Multiply it against itself once per rung.

Figure — Vanishing gradients in RNNs

Step 6 — Feel the numbers (the worked example)

WHAT. Put in real values. Take a scalar toy memory , so , over a gap of steps.

WHY. Abstract "exponential decay" only bites once you watch nose-dive. Then add saturation on top and watch it hit machine zero.

PICTURE. A curve of vs the gap , on a log axis, with the two markers and dropped in.

Figure — Vanishing gradients in RNNs

Step 7 — The degenerate & edge cases (nothing left unshown)

WHAT. We now sweep every regime so no reader meets a surprise. The figure below draws the four decay-curve regimes (vanish, ridge, explode, zero-gap); the table then adds two sub-cases of vanishing (deep vs mild) and the zero-input check for completeness.

WHY. The Contract: cover all signs and limits.

PICTURE. A four-panel map of the four core regimes, each panel a tiny gradient-over-gap sketch.

Figure — Vanishing gradients in RNNs

Step 8 — The cure in one glance: turn into

WHAT. Every fix replaces the multiplicative chain with an additive highway. In an LSTM Architecture the cell state updates as , whose backward link is just .

WHY. If the forget gate , the trip becomes — a gradient highway with no toll. GRU Architecture, Residual Connections, and Attention Mechanisms all exploit the same "add, don't repeatedly multiply" trick.

PICTURE. Two tracks side by side: the RNN's shrinking multiply-chain vs the LSTM's flat add-highway.

Figure — Vanishing gradients in RNNs

The one-picture summary

Figure — Vanishing gradients in RNNs

This final figure compresses everything: blame enters at the loss (right), passes through gearboxes each with dials and , gets multiplied into , and arrives at the early step as a whisper — unless we swap the chain for an additive highway.

Recall Feynman retelling — say it back in plain words

An RNN keeps one memory bead per step, each fed from the last. To learn from a mistake at the end, we send a "blame signal" backward through every bead. Because each bead only touches its neighbour, the total blame is a product of one-step sensitivities — one factor per step apart. Each factor is a slope (never bigger than 1) times the memory-mixer . If 's stretch factor is below 1, multiplying it times gives , which crashes toward zero as the gap grows. Saturated piles on more shrinkage. So blame for distant beads arrives as essentially nothing, and the early steps never learn. Fixes (LSTM, GRU, residuals, attention) all break the multiply-chain into an add-highway where the per-step factor is , so blame travels far intact.


Self-test:

Which factor in a single backward link can only shrink the gradient, never grow it?
The derivative , since everywhere.
What is when , and why?
It is — an empty product — because there are no links to traverse; a state is perfectly sensitive to itself.
Which single quantity must be for the Step 5 bound to guarantee decay?
The spectral norm .
Why does the LSTM cell state dodge the problem?
Its backward link is the additive gate ; with the trip stays near instead of shrinking.