3.5.3 · D4Sequence Models

Exercises — Vanishing gradients in RNNs

3,038 words14 min readBack to topic

This page is a self-test. Read each problem, try it on paper, THEN open the collapsible solution. The exercises climb from recognising the vanishing-gradient pattern to designing fixes.

Before anything, let us define every symbol used on this page, in plain words, so you never meet a piece of notation cold:

Figure s01 below plots the tanh curve and its slope — Exercise 1.2 refers to it directly.

Figure — Vanishing gradients in RNNs

Level 1 — Recognition

Recall Solution 1.1

When every step multiplies the gradient by the same number , doing it times means raising to that power — that is just what "multiply by , times over" means. The gradient shrinks to about of its size — this is vanishing (each step multiplies by a number below 1, so 20 of them push toward zero).

Recall Solution 1.2

Look at figure s01 (embedded near the top of this page, titled "s01 Activation factor: tanh and its slope"). The slope is largest where the tanh curve is steepest — at , where so . That is the maximum, value . As , , so and . The tanh saturates (goes flat) and its slope dies to 0. This is why large pre-activations are dangerous: the activation factor collapses.

Recall Solution 1.3

(a) vanishing (weight factor shrinks each step). (b) exploding (weight factor grows each step). See Exploding Gradients. (c) Saturation makes vanishing (activation factor shrinks). (d) Weight factor and slope product stays near 1 neither (the borderline "healthy" case).


Level 2 — Application

Recall Solution 2.1

With a linear recurrence the local derivative is exactly every step (the coefficient in front of ), so chaining 40 of them gives . The update scale is — small but not dead yet. decays slowly; the problem bites harder for longer sequences or smaller .

Recall Solution 2.2

Why group first? Each single step contributes the product . So one step's multiplier is . Because multiplication is associative and every step has the same , the whole -fold product is just raised to the power of the gap — we are allowed to collapse the two same-valued factors into one number before exponentiating, since and here repeat identically every step. Numerically vanished. Note came from tanh's slope and from the weight norm — separate factors that just happen to multiply, exactly as the L1 trap warned.

Recall Solution 2.3

LSTM cell path (see LSTM Architecture): — about survives. This is the gradient highway: the additive cell update means the multiplier is the learned gate, kept near 1. Plain RNN: — essentially gone. The LSTM preserves roughly 13,000× more gradient over 100 steps.


Level 3 — Analysis

Recall Solution 3.1

Dividing two powers of the same base subtracts exponents, so the unknown (a possible constant) cancels: . So . Check: ✓ and ✓. The measured base is .

Recall Solution 3.2

We need . Why the 30th root is legal: the function is strictly increasing for , so applying it to all three sides of the inequality preserves the order (it never flips ). That is why we may root through: So the largest spectral norm that still avoids explosion at 30 steps is , and the smallest that avoids vanishing is . The healthy window is the narrow band around 1. Push above and you enter Exploding Gradients; drop below and you vanish. Real saturation () shifts the whole band upward (you need a bigger just to break even), squeezing plain RNNs from both sides — which is why they are stuck.

Recall Solution 3.3

With the activation factor is exactly 1, so the whole product is : The gradient exploded by a factor of ~700. Removing tanh's damping did not fix credit assignment — it just handed control entirely to , and blows up. See Activation Functions and Gradient Clipping.

Recall Solution 3.4

Only the magnitude of an eigenvalue drives vanishing/exploding, but the sign and phase shape the path:

  • Negative eigenvalue (e.g. ): each step also flips the sign, so the gradient's sign oscillates while its magnitude still decays like . The envelope vanishes, but non-monotonically.
  • Complex eigenvalues (they come in conjugate pairs for real ): the gradient rotates in the plane spanned by that pair. The norm follows (so still vanishes if , explodes if ), but the components spiral — the norm can rise and fall along the way rather than dropping smoothly. Takeaway: the curve is an envelope; the true per-component gradient can wobble or spiral inside it. This is why measured gradient norms are often bumpy, not clean exponentials.

Level 4 — Synthesis

Recall Solution 4.1

We need . Why we may root through: is strictly increasing on , so it preserves the direction of both signs. Take 50th roots of all sides: An extremely narrow band around 1. Real training can't hold the spectral norm in a window for 50 steps — hence the need for gated highways where the effective multiplier () is learned rather than luck.

Recall Solution 4.2

About survives over 200 steps — usable for learning. The forget gate (LSTM Architecture) is doing the heavy lifting: it keeps the per-step multiplier near 1 additively. Gradient Clipping never activates on the vanishing side (clipping only caps large norms) — it is insurance against the occasional exploding spike, not a cure for decay. Two different jobs, two different tools.

Recall Solution 4.3

Differentiate: , where is the identity matrix (leaves any vector unchanged). The identity term is the key: even if the tanh-weight part shrinks to 0, the derivative floors at , so the product never collapses to zero. This is exactly the LSTM's additive cell path ( with ) and the idea behind Residual Connections and Attention Mechanisms — give gradients an un-multiplied shortcut back through time.


Level 5 — Mastery

Recall Solution 5.1

(a) Plain RNN: . LSTM: . (b) Threshold is . Plain RNN gives ✗ — it cannot learn the cat→was agreement; the singular subject signal has vanished. LSTM gives ✓ — the highway keeps ~ of the signal, so the agreement is learnable. This is the parent note's motivating example made quantitative.

Recall Solution 5.2

(a) , so . Then . (b) Solve . Taking logs is legal because is strictly increasing (preserves ), and dividing by flips the inequality since : . So beyond a gap of about 90 steps the gradient drops below .

Recall Solution 5.3

What axes does BatchNorm act on? In an RNN the hidden state at step for one example is a vector . During training we process a batch of sequences at once, so at each time step we hold a tensor of shape (batch , features ). BatchNorm computes, for each feature channel, the mean and variance across the examples and rescales that channel to zero-mean/unit-variance. Its averaging axis is the batch axis ( different sequences), one normalisation per time step.

Steel-man (why it seems plausible): in feedforward nets this keeps each pre-activation near 0, so stays near its max of 1 — it genuinely fights the activation-factor shrinkage.

Refutation — the dimension mismatch: the vanishing product accumulates along the time axis () within a single sequence. BatchNorm never touches that axis — it only averages across sequences at a fixed time step. So it operates on the batch dimension while the decay lives on the time dimension: normalising one sequence-vs-another does nothing to the chain of multiplications running down a single sequence's timeline, and it does nothing to (a weight, not an activation). It can slightly reduce saturation, but the temporal weight-factor decay is fully intact. The real fix is architectural — gates (LSTM Architecture, GRU Architecture), residual shortcuts (Residual Connections), or Attention Mechanisms.