AI Notes
Master Long Short-Term Memory networks. Learn LSTM architecture, gates, bidirectional LSTM, stacked LSTMs, applications. AI interview guide 2024.
The Vanishing Gradient Problem
Standard Recurrent Neural Networks (RNNs) struggle with long sequences. During backpropagation through time (BPTT), gradients are multiplied at each timestep. If the weight matrix has eigenvalues less than 1, gradients shrink exponentially—after 20-30 timesteps, they effectively vanish to zero. The network cannot learn dependencies that span more than a few tokens.
Consider predicting the final word in: "I grew up in France... [50 words later] ...I speak fluent ___." A standard RNN would lose the "France" signal long before reaching the blank. LSTM was specifically designed to solve this problem through a gating mechanism that allows information to flow unchanged across many timesteps.
LSTM Cell Architecture
The LSTM cell replaces the simple RNN's single tanh activation with a carefully designed system of four interacting components: a cell state (the memory), and three gates that control information flow.
The Cell State: The Conveyor Belt
The cell state acts like a conveyor belt—information can flow along it unchanged. Gates add or remove information via pointwise multiplication and addition.
Gate Equations (Complete LSTM Step)
| Given | x_t (current input), h_{t-1} (previous hidden state), C_{t-1} (previous cell state) |
| 1. FORGET GATE | What to erase from memory |
| Output | vector of values in [0,1]. 0 = completely forget, 1 = fully keep. |
| 2. INPUT GATE | What new information to store |
| 4. OUTPUT GATE | What to output from memory |
Worked Example: Tracking Subject-Verb Agreement
Task: Process "The cats that the dog chased are tired"
| Timestep 1 | "The" |
| Cell | mostly unchanged |
| Timestep 2 | "cats" |
| Cell now stores | plural subject detected |
| Timesteps 3-7 | "that the dog chased" |
| Cell maintains | plural subject |
| Timestep 8: "are" | model correctly predicts plural verb |
This demonstrates LSTM's key ability: maintaining relevant information (subject number) through many irrelevant intervening tokens.
Why Each Gate Matters
Forget Gate: Controlled Forgetting
Without the forget gate, old information accumulates indefinitely, causing interference.
Input Gate: Selective Writing
The input gate prevents every token from modifying the cell state—only important information passes through.
Output Gate: Context-Dependent Reading
| Scenario | Same cell stores both "past tense" and "subject=Mary" |
| When predicting a verb | output gate reads "past tense" dimension |
| When predicting a pronoun | output gate reads "subject" dimension |
LSTM vs. Standard RNN: Gradient Flow
Why Gradients Survive in LSTM
Standard RNN gradient path
∂h_t/∂h_{t-k} = Π(W · diag(tanh'(·))) → exponential decay
LSTM gradient path through cell state
∂C_t/∂C_{t-1} = f_t (just the forget gate value!)
∂C_t/∂C_{t-k} = Π f_i (product of forget gates)
If forget gates ≈ 1, gradient flows unchanged!
The additive cell state update (C_t = f_t⊙C_{t-1} + ...) creates a gradient "highway" that avoids the multiplicative decay of standard RNNs.
LSTM Variants
Peephole Connections
Gates can peek at the cell state directly:
f_t = σ(W_f · [C_{t-1}, h_{t-1}, x_t] + b_f)
i_t = σ(W_i · [C_{t-1}, h_{t-1}, x_t] + b_i)
o_t = σ(W_o · [C_t, h_{t-1}, x_t] + b_o)
GRU (Gated Recurrent Unit)
Simplified LSTM with two gates instead of three:
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 ← output
Fewer parameters, comparable performance on many tasks
No separate cell state — hidden state serves both roles
Bidirectional LSTM
Process sequence in both directions and concatenate:
| Forward: h_t | = LSTM_forward(x_1, x_2, ..., x_T) |
| Backward | h_t← = LSTM_backward(x_T, x_{T-1}, ..., x_1) |
| Combined: h_t = [h_t | ; h_t←] |
| Used in | NER, POS tagging, machine translation encoders |
Training LSTMs
Key Hyperparameters
| Hidden size | 128-1024 (larger = more capacity, slower) |
| Layers | 1-4 (deep LSTMs with residual connections) |
| Dropout | 0.2-0.5 (apply between layers, NOT within recurrence) |
| Gradient clipping | max_norm = 5.0 (prevents exploding gradients) |
| Learning rate | 0.001 with Adam optimizer |
Gradient Clipping
Applications
| Domain | Task | Input → Output |
|---|---|---|
| NLP | Language modeling | word sequence → next word |
| NLP | Machine translation | source sentence → target sentence |
| Speech | Recognition | audio features → transcript |
| Music | Generation | note sequence → next notes |
| Finance | Prediction | price history → future price |
| Anomaly | Detection | sensor readings → normal/abnormal |
LSTM vs. Transformers
| Aspect | LSTM | Transformer |
|---|---|---|
| Parallelization | Sequential (slow) | Fully parallel (fast) |
| Long-range | Good (100s of tokens) | Excellent (1000s) |
| Training speed | Slow (sequential) | Fast (parallelizable) |
| Memory | O(1) per step | O(n²) attention matrix |
| Still used? | Edge devices, streaming | Dominant in NLP/CV |
LSTMs remain relevant for streaming applications (real-time processing), edge deployment (smaller models), and tasks where sequential processing is natural.
Interview Questions
Q: How does LSTM solve the vanishing gradient problem? A: The additive cell state update creates a gradient highway. Gradients flow through the forget gate multiplicatively—when forget gates are near 1, gradients pass through unchanged regardless of sequence length.
Q: What's the difference between LSTM and GRU? A: GRU merges the cell state and hidden state into one, uses two gates (update, reset) instead of three (forget, input, output). GRU has fewer parameters and trains faster; LSTM is slightly more expressive for complex dependencies.
Q: Can LSTMs handle variable-length sequences? A: Yes, naturally. They process one token at a time, so sequence length is unlimited (theoretically). In practice, padding and masking handle batches of different-length sequences.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for LSTM Networks - Advanced Sequence Modeling.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Artificial Intelligence topic.
Search Terms
artificial-intelligence, artificial intelligence, artificial, intelligence, deep, learning, lstm, networks
Related Artificial Intelligence Topics