15. Long Short-Term Memory Networks (LSTM)
Overcoming long-term dependencies: Cell state highway, forget gate, input gate, output candidate generation, and bidirectional LSTM architectures.
LSTM: Complete Notes (Beginner to Advanced)
Introduction#
Long Short-Term Memory (LSTM) is a specialized type of Recurrent Neural Network (RNN) designed to learn dependencies across sequences, especially when useful information needs to be carried across many time steps.
A standard RNN maintains a hidden state:
›xₜ + hₜ₋₁ → RNN → hₜ
LSTM adds a separate cell state and uses gates to control what information should be:
textForgotten Added Exposed
The core LSTM components are:
textCell State Hidden State Forget Gate Input Gate Candidate State Output Gate
A useful high-level view is:
Architecture & Data FlowPrevious Cell State ────────────────────────────────┐ | Previous Hidden State ──┐ | ↓ ↓ Current Input ───────> LSTM Cell ─────────────> New Cell State | └──────────────────────> New Hidden State
The central idea is that the LSTM controls the flow of information using gates.
1. Long Short-Term Memory (LSTM)
Long Short-Term Memory (LSTM) is a gated recurrent neural network architecture.
It was introduced to address an important difficulty with traditional RNNs: learning useful dependencies over long sequences can be difficult because gradients may vanish or explode during Backpropagation Through Time (BPTT).
An LSTM introduces a memory pathway called the cell state and several gates that regulate information flow.
Standard RNN#
A simplified RNN update is:
Mathematical Formulationhₜ = f(Wₓh xₜ + Wₕh hₜ₋₁ + b)
The same hidden state is responsible for carrying the recurrent information.
LSTM#
An LSTM maintains two main states:
›Cell State → cₜ Hidden State → hₜ
and uses gates to control them.
The high-level flow is:
Architecture & Data FlowPrevious Cell State | v +-----------+ | | Previous hₜ₋₁ →| LSTM |→ New hₜ Current xₜ ───→ | Cell | | |→ New cₜ +-----------+
Why LSTM?#
The important motivation is:
textRNN → difficult to preserve useful information over long sequences LSTM → explicit memory path + gates → better control over information flow
LSTM does not guarantee perfect long-term memory, but its architecture makes learning long-range dependencies easier than in a basic RNN in many settings.
2. LSTM Cell
An LSTM cell receives:
textCurrent input: xₜ Previous hidden state: hₜ₋₁ Previous cell state: cₜ₋₁
and produces:
›Current hidden state: hₜ Current cell state: cₜ
The LSTM can be viewed as a sequence of controlled operations:
Architecture & Data Flowxₜ + hₜ₋₁ | +--> Forget Gate | +--> Input Gate | | | +--> Candidate State | +--> Update Cell State | +--> Output Gate | v New Hidden State
The main equations are:
Mathematical Formulationfₜ = σ(W_f [hₜ₋₁, xₜ] + b_f) iₜ = σ(W_i [hₜ₋₁, xₜ] + b_i) c̃ₜ = tanh(W_c [hₜ₋₁, xₜ] + b_c) cₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ oₜ = σ(W_o [hₜ₋₁, xₜ] + b_o) hₜ = oₜ ⊙ tanh(cₜ)
Where:
Mathematical Formulationσ = sigmoid activation tanh = hyperbolic tangent ⊙ = element-wise multiplication [ ] = concatenation
The gates usually use sigmoid because their values lie between:
›0 and 1
This makes them useful as soft information-control mechanisms.
3. Cell State
The Cell State is the long-term memory pathway of an LSTM.
It is represented as:
›cₜ
The cell state carries information through the sequence while the gates determine what should be removed and what should be added.
Cell State Flow#
textcₜ₋₁ ───────────────────────────────→ cₜ | | | | ↓ ↓ Forget Gate Input Update
The update equation is:
Mathematical Formulationcₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ
There are two main operations:
›fₜ ⊙ cₜ₋₁
controls how much previous memory is retained.
›iₜ ⊙ c̃ₜ
controls how much new candidate information is written.
Therefore:
Mathematical FormulationNew Cell State = Retained Old Memory + Selected New Information
Why the Cell State Is Important#
The cell state provides a relatively direct pathway through time.
Conceptually:
›c₁ → c₂ → c₃ → c₄ → c₅
The gates modify this memory rather than completely recomputing it from scratch at every step.
This design helps LSTMs preserve useful information for longer periods.
Forgetting Information#
If:
Mathematical Formulationfₜ ≈ 0
then:
Mathematical Formulationfₜ ⊙ cₜ₋₁ ≈ 0
so most previous cell-state information is discarded.
If:
Mathematical Formulationfₜ ≈ 1
then most of the previous cell state is retained.
4. Hidden State
The Hidden State is represented as:
›hₜ
It is the current output representation of the LSTM and is passed to the next time step.
The hidden state is calculated using:
Mathematical Formulationhₜ = oₜ ⊙ tanh(cₜ)
Therefore, the hidden state depends on:
textCurrent Cell State + Output Gate
Cell State vs Hidden State#
These two states are related but have different roles.
| State | Symbol | Main Role |
|---|---|---|
| Cell State | cₜ | Carries long-term memory |
| Hidden State | hₜ | Current exposed/output representation |
A useful intuition is:
textCell State → internal memory Hidden State → information exposed by the LSTM at the current time step
This is an intuition rather than a strict separation of "long-term" and "short-term" information.
Flow#
textPrevious hₜ₋₁ + Current xₜ ↓ LSTM Gates ↓ New cₜ ↓ Output Gate ↓ New hₜ
The new hidden state is also passed forward:
›hₜ → next LSTM step
5. Forget Gate
The Forget Gate decides how much of the previous cell state should be retained.
It is represented as:
›fₜ
The equation is:
Mathematical Formulationfₜ = σ(W_f [hₜ₋₁, xₜ] + b_f)
The sigmoid produces values between:
›0 and 1
Therefore each component of the forget gate can be interpreted as a soft retention value.
Interpretation#
Mathematical Formulationfₜ ≈ 0 → forget most of that information fₜ ≈ 1 → retain most of that information
Example#
Suppose:
Mathematical FormulationPrevious memory: cₜ₋₁ = [0.8, 0.6, 0.4]
and the forget gate produces:
Mathematical Formulationfₜ = [1.0, 0.0, 0.7]
Then:
Mathematical Formulationfₜ ⊙ cₜ₋₁ = [1.0, 0.0, 0.7] ⊙ [0.8, 0.6, 0.4]
Result:
›[0.8, 0.0, 0.28]
So the second component is effectively forgotten while the first is retained.
Mental Model#
Architecture & Data FlowPrevious Cell State | v [Forget Gate] | v How much to keep? | v Retained Memory
6. Input Gate
The Input Gate controls how much new information should be written into the cell state.
It is represented as:
›iₜ
The equation is:
Mathematical Formulationiₜ = σ(W_i [hₜ₋₁, xₜ] + b_i)
The sigmoid output lies between:
›0 and 1
Interpretation#
Mathematical Formulationiₜ ≈ 0 → write very little new information iₜ ≈ 1 → allow more new information to be written
However, the input gate alone does not determine the new content.
The actual proposed information comes from the candidate state.
This distinction is important:
textInput Gate → how much to write Candidate State → what could be written
7. Candidate State
The Candidate State is the new information that the LSTM proposes to add to the cell state.
It is commonly represented as:
›c̃ₜ
and calculated using:
Mathematical Formulationc̃ₜ = tanh(W_c [hₜ₋₁, xₜ] + b_c)
The tanh activation produces values approximately in:
›[-1, 1]
Why Candidate State?#
The LSTM needs to determine:
›What new information should be considered?
The candidate state provides that proposed information.
Then the input gate determines how much of it should actually be written.
Architecture & Data FlowCurrent Input + Previous Hidden State | v Candidate State | v Proposed New Memory ^ | Input Gate | v How much to write?
Input Gate + Candidate State#
The actual contribution to the cell state is:
›iₜ ⊙ c̃ₜ
So:
Mathematical FormulationInput Gate × Candidate State = New Information Added
8. Updating the Cell State
After calculating the forget gate, input gate, and candidate state, the LSTM updates its cell state.
The equation is:
Mathematical Formulationcₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ
This equation is the core of LSTM memory management.
Break it into two parts:
Part 1: Retain Previous Memory#
›fₜ ⊙ cₜ₋₁
The forget gate controls how much old memory remains.
Part 2: Add New Information#
›iₜ ⊙ c̃ₜ
The input gate controls how much candidate information is added.
Complete Interpretation#
Mathematical FormulationNew Memory = Old Memory × Forget Gate + Candidate × Input Gate
Or simply:
Mathematical FormulationKEEP old information + WRITE useful new information = NEW cell state
9. Output Gate
The Output Gate controls how much of the current cell state should be exposed as the hidden state.
It is represented as:
›oₜ
The equation is:
Mathematical Formulationoₜ = σ(W_o [hₜ₋₁, xₜ] + b_o)
The hidden state is then:
Mathematical Formulationhₜ = oₜ ⊙ tanh(cₜ)
Interpretation#
›Output Gate → decides how much of the current memory is exposed
The cell state itself may contain information that should remain internal.
The output gate controls what becomes the current hidden representation.
Example#
Suppose:
Mathematical Formulationtanh(cₜ) = [0.8, 0.5, -0.4]
and:
Mathematical Formulationoₜ = [1.0, 0.2, 0.0]
Then:
Mathematical Formulationhₜ = [1.0, 0.2, 0.0] ⊙ [0.8, 0.5, -0.4]
Therefore:
Mathematical Formulationhₜ = [0.8, 0.1, 0.0]
The output gate allowed some components to be exposed while suppressing others.
10. Complete LSTM Flow
All the components work together in a specific order.
Given:
textxₜ hₜ₋₁ cₜ₋₁
the LSTM performs:
Step 1: Forget Gate#
Mathematical Formulationfₜ = σ(W_f [hₜ₋₁, xₜ] + b_f)
Decides:
›What old information should be retained?
Step 2: Input Gate#
Mathematical Formulationiₜ = σ(W_i [hₜ₋₁, xₜ] + b_i)
Decides:
›How much new information should be written?
Step 3: Candidate State#
Mathematical Formulationc̃ₜ = tanh(W_c [hₜ₋₁, xₜ] + b_c)
Creates:
›Candidate new information
Step 4: Cell State Update#
Mathematical Formulationcₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ
Combines:
textRetained old memory + Selected new information
Step 5: Output Gate#
Mathematical Formulationoₜ = σ(W_o [hₜ₋₁, xₜ] + b_o)
Decides:
›How much memory should be exposed?
Step 6: Hidden State#
Mathematical Formulationhₜ = oₜ ⊙ tanh(cₜ)
Produces the current hidden representation.
Complete Diagram#
Architecture & Data Flowxₜ | | +------+------+ | | hₜ₋₁ | | | +------+------+ | v +-----------------------+ | LSTM Cell | | | | +----------------+ | | | Forget Gate | | | +----------------+ | | | | | v | cₜ₋₁ ──────────────×───────────+ | | | +----------------+ | | | Input Gate | | | +----------------+ | | | | | v | | +----------------+ | | | Candidate | | | | State | | | +----------------+ | | | | | v | | New Cell State | | | | | v | | +----------------+ | | | Output Gate | | | +----------------+ | | | | +-----------|-----------+ | v hₜ
11. LSTM Equations Together
For reference, all standard LSTM equations can be written together:
Mathematical Formulationfₜ = σ(W_f [hₜ₋₁, xₜ] + b_f) iₜ = σ(W_i [hₜ₋₁, xₜ] + b_i) c̃ₜ = tanh(W_c [hₜ₋₁, xₜ] + b_c) cₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ oₜ = σ(W_o [hₜ₋₁, xₜ] + b_o) hₜ = oₜ ⊙ tanh(cₜ)
The flow is:
Architecture & Data Flowxₜ + hₜ₋₁ | +--> Forget Gate ──────────┐ | | +--> Input Gate | | | | | +--> Candidate | | | +--------------------------+ | v Cell State cₜ | v Output Gate | v Hidden State hₜ
12. Numerical Example
Consider a simplified scalar LSTM.
Suppose:
Mathematical Formulationcₜ₋₁ = 0.8
and the gates and candidate state have already been calculated:
Mathematical Formulationfₜ = 0.7 iₜ = 0.4 c̃ₜ = 0.5
The new cell state is:
Mathematical Formulationcₜ = fₜ × cₜ₋₁ + iₜ × c̃ₜ
Substitute:
Mathematical Formulationcₜ = 0.7 × 0.8 + 0.4 × 0.5
Mathematical Formulationcₜ = 0.56 + 0.20
Mathematical Formulationcₜ = 0.76
Now suppose:
Mathematical Formulationoₜ = 0.6
The hidden state is:
Mathematical Formulationhₜ = oₜ × tanh(cₜ)
Therefore:
Mathematical Formulationhₜ = 0.6 × tanh(0.76)
Approximately:
Mathematical Formulationtanh(0.76) ≈ 0.641
So:
Mathematical Formulationhₜ ≈ 0.6 × 0.641
Mathematical Formulationhₜ ≈ 0.385
The important sequence is:
textPrevious Memory ↓ Forget Gate ↓ Retained Memory + Candidate × Input Gate ↓ New Cell State ↓ Output Gate ↓ New Hidden State
13. LSTM Parameter Structure
An LSTM has separate parameter sets for its four main transformations:
textForget Gate Input Gate Candidate State Output Gate
For an input size:
›D
and hidden size:
›H
each gate transformation receives the concatenation:
›[hₜ₋₁, xₜ]
whose size is:
›H + D
Each transformation therefore has weights of approximately:
›H × (H + D)
and a bias of:
›H
There are four such transformations.
Therefore, the total parameter count for a standard LSTM layer is:
›4 × [H(H + D) + H]
or:
›4 × H × (H + D + 1)
Example#
Suppose:
Mathematical FormulationInput size = 10 Hidden size = 20
Then:
Mathematical FormulationParameters = 4 × [20(20 + 10) + 20]
Mathematical Formulation= 4 × [600 + 20]
Mathematical Formulation= 4 × 620
Mathematical Formulation= 2480
This is for one direction and one LSTM layer under the standard formulation.
14. Simple LSTM Implementation with PyTorch
PyTorch provides an nn.LSTM module.
🐍 PythonInteractive WebAssemblyimport torch
import torch.nn as nn
lstm = nn.LSTM(
input_size=10,
hidden_size=20,
batch_first=True
)
x = torch.randn(4, 5, 10)
output, (hidden, cell) = lstm(x)
print("Output shape:", output.shape)
print("Hidden shape:", hidden.shape)
print("Cell shape:", cell.shape)
Here:
Mathematical Formulationbatch_size = 4 sequence_length = 5 input_size = 10 hidden_size = 20
Therefore:
textInput: (4, 5, 10) Output: (4, 5, 20)
For one layer and one direction:
textHidden: (1, 4, 20) Cell: (1, 4, 20)
The two returned states are:
›hidden → final hidden state cell → final cell state
15. LSTM with Multiple Layers
LSTMs can also be stacked.
🐍 PythonInteractive WebAssemblylstm = nn.LSTM(
input_size=10,
hidden_size=20,
num_layers=3,
batch_first=True
)
x = torch.randn(4, 5, 10)
output, (hidden, cell) = lstm(x)
print(output.shape)
print(hidden.shape)
print(cell.shape)
For:
›3 layers 1 direction
the shapes are:
textOutput: (4, 5, 20) Hidden: (3, 4, 20) Cell: (3, 4, 20)
The first dimension of hidden and cell represents:
›number of layers × number of directions
16. Bidirectional LSTM
Although this topic focuses on the LSTM mechanism itself, an LSTM can also be made bidirectional.
A bidirectional LSTM processes the sequence:
textForward: x₁ → x₂ → x₃ → x₄ Backward: x₄ → x₃ → x₂ → x₁
The representations from both directions are combined.
In PyTorch:
🐍 PythonInteractive WebAssemblylstm = nn.LSTM(
input_size=10,
hidden_size=20,
batch_first=True,
bidirectional=True
)
For one layer:
Mathematical FormulationOutput feature size = 2 × hidden_size = 40
So for:
Mathematical FormulationInput = (4, 5, 10)
the output is:
›(4, 5, 40)
The hidden and cell states have:
›2 × number_of_layers
as their first dimension.
17. RNN vs LSTM
A basic RNN has one primary recurrent hidden state:
›hₜ
An LSTM has:
texthₜ + cₜ
and uses gates.
| Feature | RNN | LSTM |
|---|---|---|
| Hidden State | Yes | Yes |
| Cell State | No | Yes |
| Forget Gate | No | Yes |
| Input Gate | No | Yes |
| Output Gate | No | Yes |
| Candidate State | Not as a separate gated component | Yes |
| Long-range dependency handling | More difficult in many cases | Often better |
| Architecture | Simpler | More complex |
| Parameters | Fewer | More |
The important conceptual difference is:
textRNN: Previous hidden state + current input ↓ New hidden state LSTM: Previous hidden state + current input ↓ Multiple gates ↓ New cell + hidden state
18. Gate Mental Model
A simple way to remember the gates is:
textFORGET GATE → What old information should I keep? INPUT GATE → How much new information should I write? CANDIDATE STATE → What new information could I write? CELL STATE → What memory should I carry forward? OUTPUT GATE → How much of that memory should I expose? HIDDEN STATE → What representation do I output at this time step?
A compact memory formula is:
Mathematical FormulationNew Memory = Old Memory × Forget + Candidate × Input
Then:
Mathematical FormulationHidden State = tanh(New Memory) × Output
19. Complete LSTM Time-Step Flow
At every time step:
Architecture & Data FlowInput xₜ + Previous Hidden State hₜ₋₁ + Previous Cell State cₜ₋₁ | v +----------+ | LSTM | +----------+ | +--> Forget Gate | +--> Input Gate | +--> Candidate State | v New Cell State cₜ | +--> Output Gate | v New Hidden State hₜ
Then the states move to the next time step:
›hₜ → hₜ₊₁ cₜ → cₜ₊₁
Therefore:
textx₁ → LSTM → h₁, c₁ x₂ → LSTM → h₂, c₂ x₃ → LSTM → h₃, c₃ ...
The same LSTM parameters are reused across time steps.
20. Summary
| Component | Symbol | Main Function |
|---|---|---|
| Long Short-Term Memory | LSTM | Gated recurrent architecture |
| Cell State | cₜ | Carries memory through the sequence |
| Hidden State | hₜ | Current exposed representation/output |
| Forget Gate | fₜ | Controls how much old memory is retained |
| Input Gate | iₜ | Controls how much candidate information is written |
| Candidate State | c̃ₜ | Proposes new information for the cell state |
| Output Gate | oₜ | Controls how much cell-state information becomes hidden state |
21. Quick Recap
textLSTM → A gated RNN designed to handle sequence dependencies. Cell State → Main memory pathway. Hidden State → Current output representation. Forget Gate → Controls what old memory to keep. Input Gate → Controls how much new information to write. Candidate State → Proposed new information. Output Gate → Controls what information is exposed through the hidden state.
The complete mathematical flow is:
Mathematical Formulationfₜ = σ(W_f [hₜ₋₁, xₜ] + b_f) iₜ = σ(W_i [hₜ₋₁, xₜ] + b_i) c̃ₜ = tanh(W_c [hₜ₋₁, xₜ] + b_c) cₜ = fₜ ⊙ cₜ₋₁ + iₜ ⊙ c̃ₜ oₜ = σ(W_o [hₜ₋₁, xₜ] + b_o) hₜ = oₜ ⊙ tanh(cₜ)
Final Mental Model
Architecture & Data FlowCURRENT INPUT xₜ | v +-------------------+ | | PREVIOUS hₜ₋₁ → LSTM | | | +-------------------+ | | | | | | v v v Forget Input Output Gate Gate Gate | | | | v | | Candidate | | | | +-----+ | | | v | Cell State cₜ | v tanh(cₜ) | × Output Gate | v Hidden State hₜ | v Next Time Step
The easiest way to remember an LSTM is:
textFORGET ↓ decide what old memory to remove INPUT + CANDIDATE ↓ decide what new information to add CELL STATE ↓ carry the updated memory OUTPUT ↓ decide what part of memory to expose HIDDEN STATE ↓ pass the current representation forward
15. LSTM Checkpoint
Finished studying this notebook?
Mark this guide as completed to update your course progress roadmap.