2.2 Vanishing and Exploding Gradients
BPTT sums copies of the shared matrix. If the largest |eigenvalue| is below 1, early tokens stop training.
These notes match the lecture slides. Use (slides) on the course hub for the deck.
Backprop through an unrolled RNN multiplies one Jacobian per time step. If those matrices shrink, the early-token gradient vanishes; if they grow, it explodes—either way, long-range learning dies. By the end you should be able to write the product of Jacobians, compare with , and say why clipping does not restore a vanished signal.
1. What vanishing and exploding gradients are
Vanishing (and exploding) gradients are why vanilla RNNs forget. The loss at each step depends on the shared . Backprop through the unrolled chain multiplies the same Jacobian many times. If those factors shrink, the gradient on an early token dies. If they grow, activations and gradients explode into NaNs.
The multivariable chain rule says: the gradient with respect to a repeated weight is the sum of the gradient at each copy. That algorithm is backpropagation through time (Werbos 1988).
In practice people often truncate after a few tens of steps so a batch fits in memory. Truncation is an efficiency choice. It does not fix vanishing: a truncated window never sees the token that sat 200 steps back. You do not implement truncated BPTT by hand this week. loss.backward() on nn.RNN is BPTT over the sequence you fed it.
Same product, opposite problem: vanishing is a dead arrow; exploding is a huge one. They are different bugs.

2. Why we use it
You asked the RNN to carry a bit across steps. The gradient is the only teacher for those early weights. A vanilla RNN can learn “the next word looks like the last few.” It struggles with “the negation was at the start of the review.”
A long-range example, shortened: she tried to print her tickets, left, bought toner, came back, and finally printed her _. The model has to bind the last word to tickets many steps earlier. If the gradient from that last NLL never reaches the early embedding, the net never learns the dependency, and at test time it cannot use it.
Lab 2 is a copy task: the first bit must survive many steps. Vanilla RNNs fail as the gap grows; that is this product, not a PyTorch bug.
This is not only an RNN problem. Any deep stack of Jacobians can vanish or explode. Recurrence is just the case where you multiply by the same over and over (Bengio et al. 1994; Pascanu et al. 2013). Transformers side-step the chain: every token attends to every other token in one step (Week 3). Gates (Week 2.3) were the fix inside the recurrent family.
3. Architecture
Unroll the RNN: copies of . The gradient of with respect to goes through multiplies. There is no extra layer. The architecture is the unrolled chain from note 2.1, now read as a computational graph for BPTT.
The loss at the last token, , depends on through
Each factor contains and . For , , and it is unless the pre-activation sits near 0. Multiplying by the activation Jacobian is almost always a shrink. A product of twenty numbers smaller than 1 is tiny.

The gated curve is the slogan for LSTM/GRU: keep a path whose multiplier can stay near 1.
4. How it works, step by step
Diagnose with gradient norms versus . In a project, if you still train an RNN on long documents, watch the gradient norm and (if you can) the contribution from early positions. If it is always on the start of the sequence, the net is not using the beginning.
Ignore the nonlinearity for a minute. A linear recurrence scales, for large , like , where is the largest-magnitude eigenvalue of . The same fact is often stated with the largest singular value.
- : the forward state and the backward signal vanish.
- : they explode.
- Exactly 1 in every direction is the unitary dream. Vanilla RNNs do not learn that matrix.

In 1-D, if each factor is , ten steps give . The early token still sits in the forward state as a faint trace, but the learning signal is gone.
ReLU is not a rescue: if the pre-activation is negative the unit dies; if is large it blows up. Tanh is the least-bad vanilla default. It is still short memory.
If is large, the signal blows up, weights become NaNs, and training stops. Gradient clipping (Pascanu et al. 2013): if , replace by . Same direction, shorter step.

Clipping is the bandage for exploding. It does not fix vanishing. A vanished vector is already near ; multiplying it by 1 does nothing useful. Mitigate exploding with clipping; mitigate vanishing with gates (next note), not by “more layers” of the same cell.
5. Mathematical formulas
The product along the unrolled chain:
Each factor involves and the activation Jacobian. For a cell, involves , where and .
Linear picture: scales like .
Gradient clipping: if ,
That is the same as in one dimension. Direction preserved; length capped at .
6. Positive points and negative points
Positive.
- Clipping is cheap: one norm, one possible rescale, and exploding steps stop becoming NaNs.
- The diagnosis is plottable: gradient norms versus , and (in Lab 2) the contribution from early positions.
- Naming vanishing versus exploding as different bugs keeps you from applying the explode bandage to a vanish wound.
- The same product explains why Week 3’s path length of 1 is a big deal.
Negative.
- Clipping does not fix vanishing. A near-zero vector stays near zero.
- Truncated BPTT saves memory; it does not save a dependency that sits outside the window.
- means the activation Jacobian is almost always a shrink, even before is small.
- ReLU is not a rescue: dead units or explosions, depending on the pre-activation and .
- Summing gradients over time copies of does not, by itself, save an early token whose Jacobian product is .
When not to. If the sequence is short enough that the product stays healthy, a vanilla RNN can learn local patterns. Lab 2’s growing gap is the case where you should not keep stacking the same cell and hoping.
7. Teaching this note
~18 minutes. BPTT as a sum over copies, then the product of Jacobians, then the 1-D geometric example ( vs ), then , then clipping as a cap on a tall arrow. Play the vanishing/exploding segment of the assigned video (about 45:00–58:00; if chapter marks differ, jump to the Jacobian-product board). LSTM details wait for 2.3.
8. Worked example
Scalar RNN: . Then with .
Take and pretend (best case). The factor per step is :
Now : . Same chain, opposite disaster.
Linear picture: , . Twenty steps already separate “almost gone” from “several times larger.”
Clipping: gradient , threshold . New gradient . If instead , clipping leaves it at .
Vector clip: has Euclidean norm . With , the clipped vector is .
9. Where students get stuck
- Thinking clipping “fixes RNNs,” including vanishing.
- Forgetting and blaming only .
- Treating truncated BPTT as a vanishing-gradient solution.
- Plotting loss and missing a per-position gradient plot in Lab 2.
10. Video
Watch RNNs and vanishing gradients.
Play the vanishing and exploding segment (roughly 45:00–58:00). Pause on the product of Jacobians and on the plot of gradient size vs. steps back in time. Skip n-gram language modeling and skip NMT.
11. Practice
-
Why is a problem when the state saturates at ?
-
Clipping stops explosions. Why does it not restore a vanished signal?
-
In one sentence: what would you plot in TensorBoard to see this failure in Lab 2?
-
If each Jacobian factor has operator norm , the bound on is . Compute and (calculator is fine). Is the 19-step bound closer to or to ?
-
A clip threshold meets a gradient of (Euclidean norm ). Write the clipped vector.
-
A linear RNN has . After 30 steps, is closer to or to ?
-
Why does summing gradients over time copies of not, by itself, save an early token whose Jacobian product is ?