2.3 LSTMs and GRUs
An additive cell path. Copy when forget is open and input is closed. GRU is two gates.
These notes match the lecture slides. Use (slides) on the course hub for the deck.
Gates are learned valves: they let a recurrent cell keep a bit of memory for many steps instead of overwriting it every time. LSTM and GRU are the two you will meet; they do not remove vanishing gradients, but they make a near-1 path available. By the end you should be able to write the cell update, run the copy regime in numbers, and say why a bidirectional LSTM is illegal for next-token generation.
1. What LSTMs and GRUs are
LSTMs and GRUs are gated RNNs. In a vanilla RNN the hidden state is constantly rewritten by . Unless is essentially a wire (eigenvalues on the unit circle and no saturating ), memory is short. You cannot ask SGD to discover a unitary and a linear activation at once.
What you want instead: a cell that can copy, plus input-dependent switches that say when to write, when to erase, and when to report. That is an LSTM (Hochreiter and Schmidhuber 1997; forget gate from Gers et al. 2000). An additive cell path can copy a value when the forget gate is open and the input gate is closed. GRU is the two-gate cousin: the hidden state is the memory.
They do not guarantee that every coordinate copies. They make a copy path learnable. In practice you get on the order of steps rather than , not infinite context.
2. Why we use it
Vanilla RNNs vanish. You need a highway whose gradient is along the copy. Lab 2 compares an LSTM to a vanilla RNN on a long copy: the first bit should sit in the cell until the last step.
For this course, gates are the reason we can talk about “memory” before Week 3, and a baseline in speech and time-series projects. Both LSTM and GRU are still sequential. You still cannot fill a GPU the way a transformer can. Long context is better than vanilla, worse than attention.
3. Architecture
A cell state moves down the chain with mostly multiplies and adds, not a stack of s. Three sigmoid gates, each in :
| Gate | Role |
|---|---|
| Forget | How much of to keep |
| Input | How much new candidate to write |
| Output | How much of becomes |
Each gate is a sigmoid of a linear mix of and (plus a bias). You do not need every matrix name on the first pass. You need the cell update:
The candidate is a of a linear mix of and .

Peephole connections (cell into the gates) exist in some papers. We do not use them. nn.LSTM is the three-gate cell above.
GRU (Cho et al. 2014): reset and update . No extra cell.
Read as “how much to replace.” If , you copy . Reset controls how much past goes into the candidate .

Fewer parameters, often similar quality on medium sequences. Use LSTM when you want an explicit cell; use GRU when you want a smaller default. Gates share weights across time, just as did: there is not a new forget-gate matrix at each step.
4. How it works, step by step
If and , the cell copies. That is the long-range path. Along that coordinate,
which can stay near 1. That is why the vanishing plot in 2.2 had a gated curve.

Scalar cell (drop the output gate for a minute). Suppose , , , :
Most of the old memory survived; a little new content landed. If instead , and the is gone.
Copy regime: , . Ten such steps: . Gradient along that path is .
That is Lab 2’s hope: the first bit sits in and the forget gate learns to leave it there until the last step.
5. Mathematical formulas
LSTM cell and hidden state:
Copy-path Jacobian:
GRU mix:
Gates are elementwise sigmoids of linear mixes of and . The candidate (LSTM) or (GRU) is a of a linear mix; GRU’s candidate is further gated by the reset .
6. Positive points and negative points
Positive.
- The copy regime (, ) gives a highway whose gradient can stay near 1, so long-range copy becomes learnable.
- LSTM’s explicit cell is a readable “memory tape”; GRU’s two gates are cheaper and often enough on medium sequences.
- Lab 2 can show the gated cell beating a vanilla RNN as the gap grows.
- You get a recurrent baseline for speech and time series before Week 3’s attention.
Negative.
- More parameters than a vanilla RNN (three gates plus a candidate, or two gates for GRU).
- Still sequential: you cannot fill a GPU the way a transformer can.
- LSTM does not guarantee that every coordinate copies, or that a 500-word review still trains.
- Mixing up forget and input (zeroing when you meant “write nothing new”) erases the cell.
- Bidirectional LSTMs peek at the future; they are illegal for next-token generation.
When not to. If you need all-to-all mixing in parallel, pick attention. If the whole sentence is allowed and you are classifying, a bidirectional LSTM is a legitimate encoder, not a generator.
7. Bidirectional and stacked
Bidirectional LSTM: run one net forward and one backward, concatenate. Fine for classification when the whole sentence is allowed. Illegal for next-token generation (you would peek at the future).
Stacked LSTM: of layer is the input to layer . Deeper mixing, still a loop. Do not stack four layers in Lab 2.
8. Teaching this note
~16 minutes. Table of three LSTM gates, then the copy regime , then the numeric cell update. GRU as “two knobs, hidden state is the memory.” One sentence on bidirectional-is-illegal-for-LMs. Play StatQuest LSTM 0:00–12:00 (forget/input intuition). Assign the GRU video as a 10-minute clip after class, or play 0:00–8:00 if the room is still with you.
9. Worked example
The scalar LSTM cell update is in section 4. GRU: , , gives
You kept 75% of the past.
Vector GRU with , , :
Coordinate 1 replaced (); coordinate 2 copied ().
10. Where students get stuck
- Mixing up forget vs. input (zeroing when they meant “write nothing new”).
- Claiming LSTMs “solved vanishing gradients” in every coordinate, always.
- Using a bidirectional LSTM for a generator that must not see .
- Drawing a new forget-gate matrix at each time step (gates share weights too).
11. Video
Watch StatQuest: Long Short-Term Memory (LSTM), Clearly Explained.
Pause when the forget gate is a number in multiplying the cell, and when the cell is drawn as a highway. Then StatQuest: Gated Recurrent Units (GRU), Clearly Explained — play the update-gate mix; skip if time is gone.
12. Practice
-
If the forget gate is stuck at 0, what happens to ?
-
Why is a bidirectional LSTM the wrong cell for a language model that must emit token ?
-
Name one reason you might still pick a GRU over a transformer in a project.
-
Compute for , , , . Then compute if and .
-
GRU with , , . Compute . Which coordinate copied, and which replaced?
-
In one line: why can stay near 1 when a vanilla usually cannot?
-
Does an LSTM guarantee that the first token of a 500-word review still trains? What would you measure in Lab 2 to check?