1 The problem & the fix
- Vanilla RNNs suffer vanishing gradients β can't learn dependencies beyond ~20 steps.
- Cause: backprop multiplies by
Whhrepeatedly β gradient decays to ~0. - Need: an architecture where gradients flow without repeated multiplication by Whh.
- Roadmap: Vanilla RNN (broken) β LSTM (3 gates) β GRU (2 gates) β Bidirectional β Deep/Stacked.
2 Cell state: the conveyor belt
- Cell state Ct: a "conveyor belt" carrying memory across time. Info rides it unchanged unless a gate intervenes.
- Gates are sigmoid networks: output 0 (closed) to 1 (open).
- Only element-wise operations on the belt β no matrix multiplication (this is the key to gradient flow).
- LSTM keeps two states: hidden
hand cellC(vs RNN's singleh).
3 The three LSTM gates
Forget gate β what to erase from old memory:
f_t = Ο( W_f Β· [h_{t-1}, x_t] + b_f )
- 0 = completely forget, 1 = completely keep; applied element-wise to
Ct-1. - Detail:
bfinitialized to 1 β biased toward remembering by default.
Input gate β what new info to write (two parts):
i_t = Ο( W_i Β· [h_{t-1}, x_t] + b_i ) (which dims to update)
C~_t = tanh( W_C Β· [h_{t-1}, x_t] + b_C ) (candidate values, in [-1,1])
it(sigmoid) picks which dimensions to write;C~t(tanh) is the candidate. Togetherit β C~t= new info to add.
Output gate β what to reveal as this step's hidden state:
o_t = Ο( W_o Β· [h_{t-1}, x_t] + b_o )
h_t = o_t β tanh( C_t )
htis a filtered view of the cell state β used for predictions AND passed to next timestep.- 3 sigmoid activations per cell (forget, input, output); tanh used for candidate and on
Ct.
4 Cell-state update equation
C_t = f_t β C_{t-1} + i_t β C~_t
- Left term: old memory Γ forget gate (some stuff erased).
- Right term: new candidate Γ input gate (some stuff written).
- The combine is ADDITION, not multiplication β gradients flow freely.
C_t = f_t β C_{t-1} + i_t β C~_t. If f_t = all 1s β memory preserved perfectly (C_t = C_{t-1} + new). If f_t = all 0s β cell wiped clean, total amnesia (C_t = i_t β C~_t).
5 Why LSTMs fix vanishing gradients
βC_t / βC_{t-1} = f_t (just the forget gate!)
- If
ft β 1: gradient flows perfectly β no decay. - No Whh multiplication in the cell-state path; the forget gate learns when to let gradients through.
- Same principle as ResNet skip connections (invented 18 years earlier).
| Gradient through time | Timestep-1 gradient | |
|---|---|---|
| RNN | βh_t/βh_{t-1} = WhhTΒ·diag(tanhβ²) β repeated matmul β vanishes | β 0.001Γ loss gradient |
| LSTM | βC_t/βC_{t-1} = f_t β element-wise, can stay near 1 β survives | β 0.6Γ loss gradient |
Result: handles sequences 10β100Γ longer than vanilla RNN, at ~4Γ the parameters (4 weight matrices vs 1).
6 GRU: fewer gates
- Two simplifications: (1) merge cell & hidden state into one state
h; (2) use 2 gates instead of 3 β the update gate does both forget + input.
z_t = Ο( W_z Β· [h_{t-1}, x_t] ) (update gate)
r_t = Ο( W_r Β· [h_{t-1}, x_t] ) (reset gate)
h~_t = tanh( W Β· [r_t β h_{t-1}, x_t] ) (candidate)
h_t = (1 - z_t) β h_{t-1} + z_t β h~_t (final state)
- Update gate
ztdoes both in one shot:(1-zt)keeps from old state (like forget),zttakes from candidate (like input). - Reset gate
rtcontrols how much past state feeds the candidate. - When
zt = 0βh_t = h_{t-1}: perfect memory, gradient flows unchanged (identity mapping).
7 LSTM vs GRU
| LSTM | GRU | |
|---|---|---|
| Gates | 3 | 2 |
| Separate cell state | Yes (h + C) | No (one state h) |
| Parameters | 4Γ RNN | 3Γ RNN (~75% of LSTM) |
| Training speed | Slower | ~20% faster |
| Performance | β similar on most tasks | |
- Rule of thumb: start with GRU (simpler, faster). Switch to LSTM for very long dependencies, when GRU underperforms, or with lots of data/compute. In practice the difference is often negligible.
8 Bidirectional RNNs
- Problem: standard RNN/LSTM sees only past context at each position. Sometimes future context helps ("Apple released a phone" β fruit or company?).
- Run two RNNs: one forward β, one backward β. Output at each position = concatenation of both hidden states β output dim = 2 Γ hidden_size.
| USE when | DON'T use when |
|---|---|
| Full sequence available upfront | Real-time / streaming |
| Text classification, NER, POS tagging | Autoregressive generation |
| Encoder in seq2seq | Language modeling / can't see future tokens |
Mnemonic: bidirectional = you already have the whole input; unidirectional = generating one token at a time.
9 Deep / stacked RNNs
- Stack layers: layer-1 output β layer-2 input β ... Each layer learns a different level of abstraction (syntax β semantics β discourse).
- PyTorch:
nn.LSTM(input_size, hidden_size, num_layers=3). - 2015β2017 NLP standard: 2-layer bidirectional LSTM with dropout.
β Likely exam questions
Q1. Write the cell-state update; why does the additive form matter?
C_t = f_t β C_{t-1} + i_t β C~_t. Addition gives βC_t/βC_{t-1} = f_t (element-wise), avoiding the repeated matrix multiplication by Whh that causes vanishing gradients in RNNs.
Q2. What if the forget gate outputs all 1s? All 0s?
All 1s: cell state preserved perfectly (C_t = C_{t-1} + new), never forgets. All 0s: cell wiped clean (C_t = i_t β C~_t), total amnesia β memoryless.
Q3. A GRU has z_t = 0 at some step. What is h_t and what does it mean for gradient flow?
h_t = (1-0)βh_{t-1} + 0βh~_t = h_{t-1}. State passes through unchanged β perfect memory, gradient flows as identity.
Q4. Real-time speech-to-text: use a bidirectional LSTM?
No β streaming means you don't have the full audio upfront. Bidirectional needs the complete sequence; use unidirectional.
Q5. How many sigmoid activations are in one LSTM cell? Name them.
3 β forget gate, input gate, output gate (each uses sigmoid). The candidate and the cell-to-hidden step use tanh.
Q6. Short reviews (10β20 words): vanilla RNN, GRU, or LSTM?
A GRU is likely sufficient β sequences are short, GRU gives a safety margin with minimal overhead; LSTM's extra complexity is unlikely to help.
Q7. What's the output dimension of a bidirectional layer, and why?
2 Γ hidden_size β the forward and backward hidden states at each position are concatenated.