πŸ“š Study Notes / Home / Neural Nets / Exam Notes / LSTMs & GRUs
Exam Notes Β· W6S1

LSTMs, GRUs & Gating

Vanilla RNNs vanish past ~20 steps. The fix: a gated cell-state conveyor belt with additive updates so gradients flow. LSTM (3 gates), GRU (2 gates), bidirectional & deep RNNs.

1 The problem & the fix


  • Vanilla RNNs suffer vanishing gradients β€” can't learn dependencies beyond ~20 steps.
  • Cause: backprop multiplies by Whh repeatedly β†’ 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 h and cell C (vs RNN's single h).

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: bf initialized 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. Together it βŠ™ 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 )
  • ht is 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.
Must-know for exam The core LSTM equation 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 timeTimestep-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 zt does both in one shot: (1-zt) keeps from old state (like forget), zt takes from candidate (like input).
  • Reset gate rt controls 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


LSTMGRU
Gates32
Separate cell stateYes (h + C)No (one state h)
Parameters4Γ— RNN3Γ— RNN (~75% of LSTM)
Training speedSlower~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 whenDON'T use when
Full sequence available upfrontReal-time / streaming
Text classification, NER, POS taggingAutoregressive generation
Encoder in seq2seqLanguage 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.