CS 460G · Week 13 → Lecture 1 · Fall 2026

RNN II — Vanishing Gradients, LSTM & GRU

The sequel to last week’s RNN page. The recurrence from W12L3 has a fatal flaw: as the sequence gets long, the learning signal fades to zero (or blows up). This lecture fixes it with gates — the LSTM and the GRU. Every gate equation below is real LaTeX you can zoom and copy, rendered in your browser — not a screenshot of an equation.

51 slides3 interactive widgetsAll gate formulas in LaTeX Prof. Abdullah-Al-Zubaer Imran
New to RNNs? This page assumes the recap material from last week. If the words “hidden state” and “the recurrence” are not yet solid, read the W12L3 RNN page first (or the picture-first version). Come back here for what this lecture adds.
2
big ideas: LSTM & GRU
4
gates in an LSTM cell
3
gates in a GRU cell
1
root cause: repeated W
One sentence that governs the whole lecture: a vanilla RNN multiplies the same weight matrix $W$ at every step, so going back $T$ steps multiplies the gradient by roughly $W^{\,T}$ — small singular values make it vanish, large ones make it explode. LSTM and GRU add learned gates (valves, values 0–1) that open a shortcut — the “cell state” or a gated skip — so the signal can travel many steps without being repeatedly squashed. Everything else on this page is that one idea made concrete.

0Orientation in 60 seconds

Answer first, details later. Each row is one question the lecture answers.

QuestionShort answer
Why does a plain RNN struggle on long sequences? Its gradient is a long product of the same matrix $W$. Multiply many numbers < 1 → vanishes; many > 1 → explodes.
What actually vanishes? The learning signal for early timesteps. The model can’t learn to connect word 1 to word 40, so it “forgets” long-range dependencies.
What is a gate? A vector in $(0,1)$ from a sigmoid, multiplied element-by-element into a signal. Gate ≈ 0 blocks; gate ≈ 1 lets everything through. It’s a learned valve.
What is the LSTM’s trick? A separate cell state $C_t$ that runs along the top with mostly addition (not repeated matrix multiply), guarded by forget / input / output gates.
What is the GRU’s trick? Merge forget+input into one update gate $z_t$, drop the separate cell state. Fewer parts, faster, nearly as good.
LSTM or GRU? GRU: simpler & faster. LSTM: stronger on very long / complex dependencies. Try both.

1Recap — the vanilla RNN in 30 seconds

This is the material the deck opens with (slides 3–15), the same recurrence from last week. Read it fast; the point is to set up the failure in Part 2.

A sequence is a list of vectors $\mathbf{x}^{(1)}, \mathbf{x}^{(2)}, \dots, \mathbf{x}^{(T)}$ — a sentence, a video’s frames, a stock’s daily returns. Unlike a normal network (which assumes every input is independent), here order matters and length varies. The RNN processes it by keeping a hidden state and updating it at every timestep:

The recurrence · slides 11–12
\mathbf{h}_t = f_W\big(\mathbf{h}_{t-1},\, \mathbf{x}_t\big)
\mathbf{h}_t = \tanh\!\Big(W_{hh}\,\mathbf{h}_{t-1} + W_{xh}\,\mathbf{x}_t\Big)

Read it out loud: “the new state is a mix of the previous state and the new input, squashed by tanh.” The output is just another linear projection of the state:

Output projection · slide 13
\mathbf{y}_t = W_{hy}\,\mathbf{h}_t
Analogy Think of the hidden state as a running summary you rewrite after each word. The same three matrices $W_{hh}, W_{xh}, W_{hy}$ are reused at every single step — one shared pen for the whole sentence. That sharing is what makes an RNN cheap to store… and what causes the trouble in Part 2.
Slide 20 — unrolled RNN
Slide 20. The unrolled RNN: each block takes the current input frame and the previous representation, and all blocks share the same parameters. tap to open full size

2Why vanilla RNNs fail — the vanishing (and exploding) gradient

Answer first When you train an RNN, the gradient for a weight is summed over timesteps, and each term has to travel back through every step in between. Because every hop multiplies by (roughly) the same $W$, a step back $T$ times carries a factor of about $W^{\,T}$. If $|W|<1$ per step the signal shrinks exponentially to nothing — vanishing. If $|W|>1$ it grows exponentially to NaN — exploding.

2.1 What BPTT sums up

Backpropagation Through Time (slides 24–27) is ordinary backprop run on the unrolled graph. For one sequence, the total gradient for a shared weight is the sum of the per-step gradients:

BPTT: sum the gradients · slides 25–26
\frac{\partial E}{\partial W} \;=\; \sum_{t} \frac{\partial E_t}{\partial W}

and the forward pass it backpropagates is the concrete vanilla cell the deck writes with matrices $U, W, V$ (slide 27):

The vanilla cell + SGD · slide 27
\mathbf{z}_t = U\,\mathbf{x}_t + W\,\mathbf{h}_{t-1}, \qquad \mathbf{h}_t = \tanh(\mathbf{z}_t)
\mathbf{y}_t = V\,\mathbf{h}_t
\theta \;\leftarrow\; \theta - \alpha\, \Delta_\theta \quad \text{for } \theta \in \{U, V, W\}

2.2 The chain that shrinks

To send a gradient from the loss at step $t$ back to an early step $k$, you multiply one Jacobian per hop. Each Jacobian is a diagonal of $\tanh'$ times the recurrent matrix $W$, so the whole link is a product — the source of both diseases:

The gradient product · slides 28–29
\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}} = \operatorname{diag}\!\big(1-\tanh^2(\cdot)\big)\, W
\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_k} \;=\; \prod_{j=k+1}^{t} \frac{\partial \mathbf{h}_j}{\partial \mathbf{h}_{j-1}}

Two independent things shrink that product: (a) multiplying the matrix $W$ many times, and (b) the tanh derivative $1-\tanh^2$ which is already $\le 1$ and drops to $\approx 0$ once a unit saturates. Slide 29’s curve is exactly this: the derivative is a bump that peaks at 1 (when $z=0$) and flattens to zero in the tails.

tanh and its derivative · slide 29
\tanh(z) = \frac{e^{z}-e^{-z}}{e^{z}+e^{-z}}
\frac{\mathrm{d}}{\mathrm{d}z}\tanh(z) = 1-\tanh^2(z) \;\le\; 1
This is the whole reason LSTM/GRU exist The recurrence that gives an RNN its memory is the same mechanism that kills its gradient. You cannot just “train harder” — past a few dozen steps the early timesteps receive a learning signal of essentially zero, so the network literally cannot learn to connect word 1 to word 40.

📉 Widget 1 — watch a gradient vanish (or explode) with sequence length

Pick a per-step factor r (how much the gradient is multiplied each hop, like |W|·tanh′) and a sequence length T. The plot is the gradient magnitude on a log scale as it travels back.

…
Slide 28 — exploding and vanishing gradient
Slide 28. In an RNN, W is multiplied repeatedly along the input sequence; that repeated multiplication is the exploding / vanishing gradient problem. tap to open full size
Slide 29 — vanishing gradient, tanh
Slide 29. tanh’s derivative is 0 at both ends (the tails of the blue curve). Saturated neurons pass almost no gradient, so values shrink exponentially fast and vanish after a few steps. tap to open full size

2.3 The symptom you actually notice: long-term dependency

Slides 31–33 name the practical cost. Short gaps are fine (“The sky is blue” — predict the last word from recent context). But when the answer depends on something said many steps earlier, the vanishing gradient means the model can’t learn the connection. The deck’s example sentence makes the point: to resolve “this” in a long sentence, the network must look back across a big gap — which is exactly where the gradient has already died.

3The gating idea

The fix, in one line from slide 34: add gates so the gradient has a path where it can flow more constantly — without vanishing or exploding.

What a gate is
g = \sigma(\,\cdot\,) \in (0,1)
\text{output} = g \otimes \text{signal} \quad(\text{element-wise})

$\sigma$ is the sigmoid, which squashes any real number into $(0,1)$. Read each entry of $g$ as a valve dial for one channel: near $0$ it shuts that channel off, near $1$ it lets it through, in between it dims it. Crucially, the gates are learned — training decides when to remember and when to forget.

Sigmoid — the valve function
\sigma(z) = \frac{1}{1+e^{-z}}
\sigma'(z) = \sigma(z)\big(1-\sigma(z)\big) \;\le\; \tfrac{1}{4}
Slide 34 — gating mechanism
Slide 34. “Add gates to produce paths where gradients can flow more constantly in longer-term, without vanishing nor exploding.” tap to open full size
Analogy A vanilla RNN is a single conveyor belt that must pass every item through one narrow, squashing gate at each step. An LSTM adds a fast lane on top (the cell state) where items ride along almost untouched, joined and left only through side valves (the gates). The gradient, like a passenger, can take the fast lane and travel the whole line.

4LSTM — Long Short-Term Memory

Answer first An LSTM keeps two running states: the hidden state $\mathbf{h}_t$ (the short-term output) and a cell state $\mathbf{C}_t$ (the long-term memory) that flows across the top of the cell through mostly addition. Four gates decide what to do with it: forget (what to erase from the old cell), input (how much new info to add), a candidate (the new info itself), and output (what part of the cell to expose as $\mathbf{h}_t$).
Slide 35 — LSTM memory cells and gates
Slide 35. The LSTM models longer-term dependencies with memory cells and gates that control the flow of information along those cells. tap to open full size

4.1 The four gates, in order

Everything is a linear map of the concatenated $[\mathbf{h}_{t-1}, \mathbf{x}_t]$, followed by either $\sigma$ (for gates — values 0–1) or $\tanh$ (for the candidate — values −1–1). Same shape every time; only the weight matrix and bias differ per gate.

Forget gate ft · slide 37 — how much of Ct−1 to erase
f_t = \sigma\!\Big(W_f \cdot [\,\mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b_f\Big)
Input gate it + candidate C̃t · slide 38
i_t = \sigma\!\Big(W_i \cdot [\,\mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b_i\Big)
\tilde{\mathbf{C}}_t = \tanh\!\Big(W_c \cdot [\,\mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b_c\Big)
Update the cell state · slide 39 — the fast lane
\mathbf{C}_t = f_t \otimes \mathbf{C}_{t-1} \;+\; i_t \otimes \tilde{\mathbf{C}}_t

That single line is the heart of the LSTM. Notice it is an add, not a repeated matrix multiply: the old memory is scaled by the forget gate and the new memory is scaled by the input gate, then the two are added. Along this path $\partial \mathbf{C}_t / \partial \mathbf{C}_{t-1}$ is just the forget-gate diagonal — so if the forget gate stays near 1, the gradient rides straight back with almost no decay. That is the shortcut a vanilla RNN lacks.

Output gate ot + hidden state · slide 40
o_t = \sigma\!\Big(W_o \cdot [\,\mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b_o\Big)
\mathbf{h}_t = o_t \otimes \tanh(\mathbf{C}_t)
Slide 36 — four gates overview
Slide 36. The contents of the memory cells are regulated by four things: the forget gate ft, the input gate it, the candidate gate, and the output gate ot. tap to open full size
GateActivationJob (one line)Slide
Forget $f_t$$\sigma$How much of the old cell $\mathbf{C}_{t-1}$ to keep (1) vs erase (0).37
Input $i_t$$\sigma$How much of the new candidate to write into the cell.38
Candidate $\tilde{\mathbf{C}}_t$$\tanh$The actual new content being proposed (−1…1).38
Output $o_t$$\sigma$Which part of $\tanh(\mathbf{C}_t)$ to expose as $\mathbf{h}_t$.40

🎛️ Widget 2 — the LSTM cell as three valves you can turn

Start the old cell at Ct−1 = 1.0. Drag the forget, input and output gates and the candidate value. Watch how the cell state is edited and what part of it leaks out as ht.

…

5GRU — Gated Recurrent Unit

Answer first A GRU is the LSTM with parts removed. The forget and input gates are merged into one update gate $z_t$ (with $z$ controlling update and $1-z$ controlling keep — so they always sum to 1), and there is no separate cell state — the hidden state carries the memory. A second reset gate $r_t$ decides how much of the past to use when computing the new candidate. Fewer gates → fewer parameters → faster, and usually within a hair of an LSTM.
GRU — all four equations · slides 41–42
z_t = \sigma\!\Big(W_z \cdot [\,\mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b_z\Big)
r_t = \sigma\!\Big(W_r \cdot [\,\mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b_r\Big)
\tilde{\mathbf{h}}_t = \tanh\!\Big(W \cdot [\,r_t \otimes \mathbf{h}_{t-1},\, \mathbf{x}_t\,] + b\Big)
\mathbf{h}_t = (1 - z_t) \otimes \mathbf{h}_{t-1} \;+\; z_t \otimes \tilde{\mathbf{h}}_t

Read the last line as a cross-fade: $z_t$ is a dial between “keep the old state” (weight $1-z_t$) and “use the new candidate” (weight $z_t$), per channel. Because the two weights sum to 1 it is a true interpolation — no information is created or destroyed, which is another gentle path for gradients.

Slide 41 — GRU update gate
Slide 41. The LSTM’s forget and input gates merge into one update gate zt, which controls both the forgetting factor and the decision to update the state. tap to open full size
Slide 42 — GRU reset gate
Slide 42. The reset gate rt controls which parts of the state are used to compute the next target state. tap to open full size

🔀 Widget 3 — the GRU cross-fade

Fix the old hidden state at 0.30 and the candidate at −0.60. Drag the update gate z to see ht slide between them.

…

6LSTM vs GRU

The deck’s verdict (slide 43) is two lines: GRUs are simpler and faster than LSTMs; LSTMs are superior for long-term dependencies. Here is the same comparison spelled out.

 LSTMGRU
Gates3 (+ candidate)2 (+ candidate)
Stateshidden $\mathbf{h}_t$ + cell $\mathbf{C}_t$hidden $\mathbf{h}_t$ only
Parametersmore~25% fewer
Speedslowerfaster
Strengthvery long / complex dependenciessimpler tasks, limited data
Gradient pathadditive cell state — long shortcutcross-fade $1-z$ — also gentle
Slide 43 — LSTM vs GRU diagrams
Slide 43. Side by side: the LSTM carries a separate cell-state line plus four gating stages; the GRU is the compact version with two gates and no cell line. tap to open full size

7Where these are used

Slides 44–47 show four real applications — one classic pattern each:

ApplicationArchitecture sketchSlide
Text classificationRNN/LSTM over word embeddings → pooled vector → label44
Video classificationFrame features fed as a sequence → many-to-one label45
mi-RNA classificationHybrid CNN (features) → LSTM (sequence)46
Intrusion detectionCNN-GRU over network-flow sequences47

The recurring pattern: a CNN or embedding turns raw input into vectors, then an LSTM/GRU handles the order over time. That CNN→RNN stack is the standard recipe these two architectures unlocked.

✓Cheat sheet — everything on one screen

ConceptIn wordsSymbols
Recurrencenew state from old state + input$\mathbf{h}_t = \tanh(W_{hh}\mathbf{h}_{t-1}+W_{xh}\mathbf{x}_t)$
BPTTsum per-step gradients$\partial E/\partial W = \sum_t \partial E_t/\partial W$
Gradient linkproduct of Jacobians back to k$\prod_{j=k+1}^{t}\partial\mathbf{h}_j/\partial\mathbf{h}_{j-1}$
Vanishing$|W|<1$ → signal → 0$W^{\,t-k}\to 0$
Exploding$|W|>1$ → signal → ∞$W^{\,t-k}\to\infty$
Gatelearned valve 0–1$g=\sigma(\cdot)$
LSTM forgethow much of old cell to keep$f_t=\sigma(W_f[h_{t-1},x_t]+b_f)$
LSTM input+candhow much new info to write$i_t=\sigma(\cdot),\ \tilde{C}_t=\tanh(\cdot)$
LSTM cellerase-then-add (the fast lane)$C_t=f_t\otimes C_{t-1}+i_t\otimes\tilde{C}_t$
LSTM outputwhat part of cell leaks out$h_t=o_t\otimes\tanh(C_t)$
GRU updatemerged forget+input, cross-fade$h_t=(1-z_t)\otimes h_{t-1}+z_t\otimes\tilde{h}_t$
GRU resethow much past to use for candidate$r_t=\sigma(W_r[h_{t-1},x_t]+b_r)$

▦All 51 slides

Every slide in the deck, in order — tap any one to open it full size. The ones quoted above are the argument-carrying slides; the rest are here so you can check nothing was omitted.

Next lecture: this page fixes the RNN. The one after it removes it — attention and the Transformer, W13L2 →, then W13L3 (ViT) →.