LSTM & GRU

GATE Relevance: Medium

Prerequisites

  • RNNs — Hidden state, BPTT, vanishing gradient

🎯 Learning Objectives

  • Write all four LSTM gate equations (forget, input, candidate, output)
  • Explain how the cell state solves the vanishing gradient problem
  • Write the GRU equations (reset & update gates)
  • Compute parameter counts for LSTM and GRU layers
  • Compare LSTM vs GRU for GATE questions

1. LSTM Overview

The Long Short-Term Memory (LSTM) network was introduced by Hochreiter & Schmidhuber (1997). The key innovation is a cell state Ct that acts as a “conveyor belt” running through the entire sequence, regulated by three gates.

2. LSTM Gate Equations

Complete LSTM Equations

// Forget gate — what to erase from cell state
ft = σ(Wf · [ht−1, xt] + bf)

// Input gate — what new information to store
it = σ(Wi · [ht−1, xt] + bi)

// Candidate cell state
C̃t = tanh(WC · [ht−1, xt] + bC)

// Cell state update
Ct = ft ⊙ Ct−1 + it ⊙ C̃t

// Output gate — what to output from cell state
ot = σ(Wo · [ht−1, xt] + bo)

// Hidden state
ht = ot ⊙ tanh(Ct)

Where σ = sigmoid, ⊙ = element-wise multiply
[h, x] = concatenation of ht−1 and xt

Why This Solves Vanishing Gradients

The cell state update Ct = ft ⊙ Ct−1 + it ⊙ C̃t is additive, not multiplicative. When ft ≈ 1 and it ≈ 0, Ct ≈ Ct−1 — gradients flow through the cell state without repeated multiplication by W.

3. Gate Intuition

GateOutput RangeRoleAnalogy
Forget ft[0, 1]Erase old memoryClear irrelevant context
Input it[0, 1]Write new memoryStore new information
Output ot[0, 1]Read from memoryExpose relevant info
Candidate C̃t[−1, 1]New info to writeContent of the write

4. LSTM Parameter Counting

LSTM Parameter Formula

Each gate has a weight matrix of size n × (n + d) plus a bias of size n.
There are 4 such gates (f, i, C̃, o).

Total LSTM params = 4 × [n(n + d) + n] = 4n(n + d + 1)

Where:
  n = hidden size
  d = input size

Example: LSTM Layer

Input dimension d = 100, hidden size n = 256.

Params = 4 × 256 × (256 + 100 + 1) = 4 × 256 × 357 = 365,568

Compare to vanilla RNN with same dimensions: 256 × (256 + 100 + 1) = 91,392. LSTM uses ∼4× the parameters.

5. Gated Recurrent Unit (GRU)

The GRU (Cho et al., 2014) simplifies the LSTM by merging the cell state and hidden state, and using only two gates.

GRU Equations

// Update gate — how much of past state to keep
zt = σ(Wz · [ht−1, xt] + bz)

// Reset gate — how much of past state to forget
rt = σ(Wr · [ht−1, xt] + br)

// Candidate hidden state
h̃t = tanh(Wh · [rt ⊙ ht−1, xt] + bh)

// Hidden state update
ht = (1 − zt) ⊙ ht−1 + zt ⊙ h̃t

GRU vs LSTM Key Differences

  • GRU has no separate cell state — hidden state serves both roles.
  • GRU uses 2 gates (update z, reset r) vs LSTM’s 3 gates (forget, input, output).
  • The update gate z controls both forgetting and inputting in a coupled manner: “what I forget, I replace.”

6. GRU Parameter Counting

GRU Parameter Formula

3 sets of weights (z, r, h̃), each n × (n + d) + n:

Total GRU params = 3 × [n(n + d) + n] = 3n(n + d + 1)

Example: GRU Layer

d = 100, n = 256 (same as LSTM example):

Params = 3 × 256 × 357 = 274,176

GRU uses 75% of LSTM parameters (3/4 ratio).

7. LSTM vs GRU Comparison

FeatureLSTMGRU
Gates3 (forget, input, output)2 (update, reset)
Statesht and Ct (separate)ht only
Parameters4n(n + d + 1)3n(n + d + 1)
Training SpeedSlowerFaster
Long SequencesGenerally betterComparable
Year Introduced19972014

When to Use Which?

  • LSTM: When you have lots of data and need to model very long dependencies.
  • GRU: When you want faster training with fewer parameters and data is moderate.
  • In practice, they often perform similarly; GRU is preferred for speed.

Summary

  • LSTM uses cell state + 3 gates. Cell update is additive, preventing vanishing gradients.
  • LSTM params: 4n(n + d + 1).
  • GRU uses 2 gates, no separate cell state. Params: 3n(n + d + 1).
  • Both solve vanilla RNN’s vanishing gradient problem. GRU is faster; LSTM can be slightly more expressive.
  • For GATE: know the gate equations, parameter formulas, and LSTM vs GRU differences.