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.
Answer first, details later. Each row is one question the lecture answers.
| Question | Short 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. |
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:
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:
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:
and the forward pass it backpropagates is the concrete vanilla cell the deck writes with matrices $U, W, V$ (slide 27):
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:
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.
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.
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.
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.
$\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.
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.
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.
| Gate | Activation | Job (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 |
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.
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.
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.
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.
| LSTM | GRU | |
|---|---|---|
| Gates | 3 (+ candidate) | 2 (+ candidate) |
| States | hidden $\mathbf{h}_t$ + cell $\mathbf{C}_t$ | hidden $\mathbf{h}_t$ only |
| Parameters | more | ~25% fewer |
| Speed | slower | faster |
| Strength | very long / complex dependencies | simpler tasks, limited data |
| Gradient path | additive cell state — long shortcut | cross-fade $1-z$ — also gentle |
Slides 44–47 show four real applications — one classic pattern each:
| Application | Architecture sketch | Slide |
|---|---|---|
| Text classification | RNN/LSTM over word embeddings → pooled vector → label | 44 |
| Video classification | Frame features fed as a sequence → many-to-one label | 45 |
| mi-RNA classification | Hybrid CNN (features) → LSTM (sequence) | 46 |
| Intrusion detection | CNN-GRU over network-flow sequences | 47 |
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.
| Concept | In words | Symbols |
|---|---|---|
| Recurrence | new state from old state + input | $\mathbf{h}_t = \tanh(W_{hh}\mathbf{h}_{t-1}+W_{xh}\mathbf{x}_t)$ |
| BPTT | sum per-step gradients | $\partial E/\partial W = \sum_t \partial E_t/\partial W$ |
| Gradient link | product 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$ |
| Gate | learned valve 0–1 | $g=\sigma(\cdot)$ |
| LSTM forget | how much of old cell to keep | $f_t=\sigma(W_f[h_{t-1},x_t]+b_f)$ |
| LSTM input+cand | how much new info to write | $i_t=\sigma(\cdot),\ \tilde{C}_t=\tanh(\cdot)$ |
| LSTM cell | erase-then-add (the fast lane) | $C_t=f_t\otimes C_{t-1}+i_t\otimes\tilde{C}_t$ |
| LSTM output | what part of cell leaks out | $h_t=o_t\otimes\tanh(C_t)$ |
| GRU update | merged forget+input, cross-fade | $h_t=(1-z_t)\otimes h_{t-1}+z_t\otimes\tilde{h}_t$ |
| GRU reset | how much past to use for candidate | $r_t=\sigma(W_r[h_{t-1},x_t]+b_r)$ |
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.


















































