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.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Vanilla RNNs and the Vanishing Gradient
  5. The LSTM Architecture
  6. Gate Equations in Detail
  7. Gradient Flow Through the Cell State
  8. The GRU Simplification
  9. Bidirectional and Stacked RNNs
  10. Common Pitfalls
  11. Research Perspective
  12. Summary
  13. Exercises

Learning Objectives

  1. Derive the vanilla RNN gradient and identify the vanishing/exploding gradient conditions.
  2. Explain each LSTM gate and its role in memory control.
  3. Prove that the cell state gradient has a multiplicative path through forget gates only.
  4. Derive the GRU equations as a simplified LSTM with merged gates.
  5. Analyze when LSTMs/GRUs are preferred over transformers.

Notation

  • xtRd\mathbf{x}_t \in \mathbb{R}^d — input at time tt
  • htRn\mathbf{h}_t \in \mathbb{R}^n — hidden state
  • ctRn\mathbf{c}_t \in \mathbb{R}^n — cell state (LSTM only)
  • ft,it,ot(0,1)n\mathbf{f}_t, \mathbf{i}_t, \mathbf{o}_t \in (0,1)^n — forget, input, output gates
  • c~t\tilde{\mathbf{c}}_t — candidate cell state
  • \odot — element-wise (Hadamard) product
  • σ()\sigma(\cdot) — 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 ct\mathbf{c}_t 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

C0.61Forget f0.52Input i0.74Output o0.65Cell State0.605xₜ0.50hₜ0.35Cₜ = f·Cₜ₋₁ + i·tanh(Ĉ) · hₜ = o·tanh(Cₜ)Information highway (cell state)
f=0.52i=0.74o=0.65C=0.61
Input xₜ
0.50
Gate ≈ 0Gate ≈ 1
Insight: The forget gate discards old cell state, the input gate writes new information, and the output gate controls what is exposed as hₜ. Gates are learned sigmoids — color shows activation strength.

Vanilla RNNs and the Vanishing Gradient

Vanilla RNN:

ht=tanh(Whht1+Wxxt+b).(1)\mathbf{h}_t = \tanh(\mathbf{W}_h\mathbf{h}_{t-1} + \mathbf{W}_x\mathbf{x}_t + \mathbf{b}). \tag{1}

Gradient through time. For loss at time TT, the gradient w.r.t. ht\mathbf{h}_t (t<Tt < T):

LTht=LThTk=t+1Tdiag(tanh(zk))Wh.(2)\frac{\partial\mathcal{L}_T}{\partial\mathbf{h}_t} = \frac{\partial\mathcal{L}_T}{\partial\mathbf{h}_T}\prod_{k=t+1}^T \text{diag}(\tanh'(\mathbf{z}_k))\mathbf{W}_h. \tag{2}

Vanishing: If diag(tanh)Wh<1\lVert\text{diag}(\tanh')\mathbf{W}_h\rVert < 1, the product 0\to 0 exponentially in (Tt)(T-t).

Exploding: If diag(tanh)Wh>1\lVert\text{diag}(\tanh')\mathbf{W}_h\rVert > 1, the product \to \infty.

Since tanh(z)(0,1]\tanh'(z) \in (0, 1] and is typically <1< 1, vanishing dominates for long sequences.


The LSTM Architecture

The LSTM (Hochreiter & Schmidhuber, 1997) introduces:

  1. A cell state ct\mathbf{c}_t — a memory highway with additive updates.
  2. Three gates — sigmoid-activated controllers that modulate information flow.

The cell state update is additive (like a residual connection through time):

ct=ftct1+itc~t.(3)\mathbf{c}_t = \mathbf{f}_t \odot \mathbf{c}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{c}}_t. \tag{3}

Gate Equations in Detail

Forget gate (what to erase from memory):

ft=σ(Wf[ht1,xt]+bf).(4)\mathbf{f}_t = \sigma(\mathbf{W}_f[\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_f). \tag{4}

Input gate (what new information to store):

it=σ(Wi[ht1,xt]+bi).(5)\mathbf{i}_t = \sigma(\mathbf{W}_i[\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_i). \tag{5}

Candidate cell state (proposed new content):

c~t=tanh(Wc[ht1,xt]+bc).(6)\tilde{\mathbf{c}}_t = \tanh(\mathbf{W}_c[\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_c). \tag{6}

Cell state update:

ct=ftct1+itc~t.(7)\mathbf{c}_t = \mathbf{f}_t \odot \mathbf{c}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{c}}_t. \tag{7}

Output gate (what to expose from memory):

ot=σ(Wo[ht1,xt]+bo).(8)\mathbf{o}_t = \sigma(\mathbf{W}_o[\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_o). \tag{8}

Hidden state:

ht=ottanh(ct).(9)\mathbf{h}_t = \mathbf{o}_t \odot \tanh(\mathbf{c}_t). \tag{9}

Gradient Flow Through the Cell State

The key advantage: the gradient through the cell state from tt to t1t-1:

ctct1=diag(ft).(10)\frac{\partial\mathbf{c}_t}{\partial\mathbf{c}_{t-1}} = \text{diag}(\mathbf{f}_t). \tag{10}

Over TtT-t steps:

cTct=k=t+1Tdiag(fk).(11)\frac{\partial\mathbf{c}_T}{\partial\mathbf{c}_t} = \prod_{k=t+1}^T \text{diag}(\mathbf{f}_k). \tag{11}

Key insight: The forget gate fk(0,1)n\mathbf{f}_k \in (0,1)^n is learned. If the network sets fk1f_k \approx 1 for some dimensions, the gradient flows nearly unattenuated through those dimensions, enabling long-range memory. Compare with the vanilla RNN where the "forget factor" tanh(z)Wh\tanh'(z)\mathbf{W}_h 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: zt=σ(Wz[ht1,xt])\mathbf{z}_t = \sigma(\mathbf{W}_z[\mathbf{h}_{t-1}, \mathbf{x}_t])

Reset gate: rt=σ(Wr[ht1,xt])\mathbf{r}_t = \sigma(\mathbf{W}_r[\mathbf{h}_{t-1}, \mathbf{x}_t])

Candidate: h~t=tanh(W[rtht1,xt])\tilde{\mathbf{h}}_t = \tanh(\mathbf{W}[\mathbf{r}_t \odot \mathbf{h}_{t-1}, \mathbf{x}_t])

State update:

ht=(1zt)ht1+zth~t.(12)\mathbf{h}_t = (1 - \mathbf{z}_t)\odot\mathbf{h}_{t-1} + \mathbf{z}_t\odot\tilde{\mathbf{h}}_t. \tag{12}

Comparison to LSTM:

  • Fewer parameters (2 gates instead of 3, no separate cell state).
  • The update zt\mathbf{z}_t plays dual role: ft=1zt\mathbf{f}_t = 1 - \mathbf{z}_t and it=zt\mathbf{i}_t = \mathbf{z}_t.
  • Empirically similar performance for most tasks.

Bidirectional and Stacked RNNs

Bidirectional: Run two RNNs — one forward, one backward — and concatenate:

ht=[ht;ht]R2n.(13)\mathbf{h}_t = [\overrightarrow{\mathbf{h}_t}; \overleftarrow{\mathbf{h}_t}] \in \mathbb{R}^{2n}. \tag{13}

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 ft0.5f_t \approx 0.5 initially — the network starts by forgetting half the cell state. Best practice: initialize bf\mathbf{b}_f to 1 or 2 so ft1f_t \approx 1 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: ktanh(zk)Wh0\prod_k \tanh'(z_k)\mathbf{W}_h \to 0.
  • LSTM adds a cell state with additive updates: gradient flows through kfk\prod_k \mathbf{f}_k.
  • 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 Wh=0.9I\mathbf{W}_h = 0.9\mathbf{I} and tanh\tanh 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 ft=1\mathbf{f}_t = \mathbf{1} and it=0\mathbf{i}_t = \mathbf{0}, the LSTM perfectly preserves the cell state (infinite memory).

Exercise 4. Prove that the GRU update (equation 12) is a convex combination of ht1\mathbf{h}_{t-1} and h~t\tilde{\mathbf{h}}_t.

Exercise 5. Count the total number of parameters in a single LSTM layer with input dimension dd and hidden dimension nn.