Long Short-Term Memory (LSTM)
Complete derivation of the LSTM architecture: the vanishing gradient problem in vanilla RNNs, gate mechanisms (forget, input, output), the cell state as a controlled memory highway, gradient flow analysis, and the GRU simplification.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Vanilla RNNs and the Vanishing Gradient
- The LSTM Architecture
- Gate Equations in Detail
- Gradient Flow Through the Cell State
- The GRU Simplification
- Bidirectional and Stacked RNNs
- Common Pitfalls
- Research Perspective
- Summary
- Exercises
Learning Objectives
- Derive the vanilla RNN gradient and identify the vanishing/exploding gradient conditions.
- Explain each LSTM gate and its role in memory control.
- Prove that the cell state gradient has a multiplicative path through forget gates only.
- Derive the GRU equations as a simplified LSTM with merged gates.
- Analyze when LSTMs/GRUs are preferred over transformers.
Notation
- — input at time
- — hidden state
- — cell state (LSTM only)
- — forget, input, output gates
- — candidate cell state
- — element-wise (Hadamard) product
- — sigmoid function
Core Intuition
Vanilla RNNs struggle with long-range dependencies because gradients either vanish or explode when backpropagated through many time steps. The LSTM solves this by introducing a cell state that flows through time with minimal transformation — analogous to a skip connection across time. Gates control what information to store, forget, and output, allowing the network to selectively maintain information over hundreds of time steps.
LSTM Cell Gates
Vanilla RNNs and the Vanishing Gradient
Vanilla RNN:
Gradient through time. For loss at time , the gradient w.r.t. ():
Vanishing: If , the product exponentially in .
Exploding: If , the product .
Since and is typically , vanishing dominates for long sequences.
The LSTM Architecture
The LSTM (Hochreiter & Schmidhuber, 1997) introduces:
- A cell state — a memory highway with additive updates.
- Three gates — sigmoid-activated controllers that modulate information flow.
The cell state update is additive (like a residual connection through time):
Gate Equations in Detail
Forget gate (what to erase from memory):
Input gate (what new information to store):
Candidate cell state (proposed new content):
Cell state update:
Output gate (what to expose from memory):
Hidden state:
Gradient Flow Through the Cell State
The key advantage: the gradient through the cell state from to :
Over steps:
Key insight: The forget gate is learned. If the network sets for some dimensions, the gradient flows nearly unattenuated through those dimensions, enabling long-range memory. Compare with the vanilla RNN where the "forget factor" is not directly controllable.
Analogy: The cell state is a "conveyor belt" — information travels forward with the forget gate controlling which items stay on the belt.
The GRU Simplification
The Gated Recurrent Unit (Cho et al., 2014) merges the forget and input gates and removes the cell state:
Update gate:
Reset gate:
Candidate:
State update:
Comparison to LSTM:
- Fewer parameters (2 gates instead of 3, no separate cell state).
- The update plays dual role: and .
- Empirically similar performance for most tasks.
Bidirectional and Stacked RNNs
Bidirectional: Run two RNNs — one forward, one backward — and concatenate:
Captures both past and future context (not applicable for autoregressive generation).
Stacked (deep): Multiple RNN layers, each taking the previous layer's hidden states as input. Depth increases representational power.
Common Pitfalls
Pitfall 1. Initializing forget gate biases to zero. This means initially — the network starts by forgetting half the cell state. Best practice: initialize to 1 or 2 so initially (remember by default).
Pitfall 2. Not using gradient clipping. Even with LSTMs, gradients can still explode through the hidden-to-hidden paths in the gates. Clip gradient norms to a maximum value.
Pitfall 3. Using LSTMs where transformers suffice. For tasks with fixed-length inputs and parallelizable training, transformers are faster and often more accurate. LSTMs remain relevant for online/streaming settings.
Research Perspective
The LSTM (1997) was one of the most impactful architectural innovations in deep learning, enabling breakthroughs in speech recognition, machine translation, and language modeling before transformers. The gating mechanism inspired many subsequent architectures. Today, state-space models (Mamba, S4) attempt to combine the LSTM's sequential efficiency with the transformer's parallelism.
Summary
- Vanilla RNNs suffer from vanishing gradients: .
- LSTM adds a cell state with additive updates: gradient flows through .
- Three gates (forget, input, output) control memory read/write/erase.
- GRU simplifies to two gates and no separate cell state.
- Forget gate bias initialization to 1 is critical for long-range dependencies.
- LSTMs are the temporal analog of residual connections.
Exercises
Exercise 1. For a vanilla RNN with and activation, compute the gradient magnitude after 50 time steps.
Exercise 2. Derive equation (11) by recursive application of the chain rule through the cell state.
Exercise 3. Show that with and , the LSTM perfectly preserves the cell state (infinite memory).
Exercise 4. Prove that the GRU update (equation 12) is a convex combination of and .
Exercise 5. Count the total number of parameters in a single LSTM layer with input dimension and hidden dimension .