CAS logo CAS NLP LABInteractive Tutorial Series ← Series

⚡  LSTM (1997) — Memory by Design, Not Decay

Long Short-Term Memory

A fully interactive deep dive into the architecture that cured the RNN's amnesia. Lecture 02 ended on a wound — vanilla RNNs forget. Hochreiter & Schmidhuber's answer: memory that is written, kept, and erased on purpose, controlled by tiny learned switches called gates.

Gated Cell State Gradient Highway Live Sentence Traces Hand-Computed BPTT LSTM vs GRU
1997Hochreiter · Schmidhuberstill unbeaten at 20 (Greff 2017)
3Gatesforget · input · output
2Statesc_t private · h_t public
100+Steps of Memoryvs ~10 for a vanilla RNN
DEFINITION

A Long Short-Term Memory (LSTM) cell carries two states through time: a private cell state c_t — a conveyor of long-term memory updated only by elementwise multiply and add — and a public hidden state h_t. Three sigmoid gates decide what to forget, what to write, and what to reveal.

THE PROBLEM, IN ONE SENTENCE

“The keys to the cabinet are on the table.” The plural subject sits five words from its verb, behind a singular distractor. A vanilla RNN's memory fades every step; an LSTM protects the plural bit with a forget gate held near 1 — then spends it exactly at the verb.

On the right, watch a cell read that sentence. Three lamps per word: forget, input, output. Bright = gate open.

WHY RNNs FORGET ↓

LIVE · THREE GATES PER WORD

c_t = f_t⊙c_{t−1} + i_t⊙c̃_t

Notice how the forget lamp stays bright on the subject words — that is memory being chosen, not decaying.

To deserve the solution, first re-live the failure: gradients in a vanilla RNN are multiplied by the same weight at every step backwards — and repeated multiplication is a terrible memory policy.

02 THE VANISHING GRADIENT, FELT

Repeated multiplication is not a memory policy

Backprop through time multiplies the gradient by W·tanh′ once per step. Drag the sliders: the left bars are a vanilla RNN's surviving signal from word #1; the right bars are an LSTM whose forget gate simply stays open.

VANILLA RNN · effective multiplier |W·tanh′|0.75
LSTM · forget gate f (learned, per channel)0.970

Bars show how much of word #1's signal is still alive after k timesteps. 24 steps shown.

VANILLA RNN · signal of word #1 after k steps

LSTM CELL · same signal, forget gate held at f

The fix is audacious: give memory its own private lane that no weight matrix is allowed to touch — only tiny learned switches may open or close it.

03 THE CORE IDEA

A conveyor of memory, guarded by gates

The cell state c_t runs straight across time like a conveyor belt. Three gates — each its own tiny sigmoid layer looking at [h_{t−1}, x_t] — are the only hands allowed to touch it. Hover the diagram.

⊙ multiply = gateElementwise multiplication by a number in (0, 1) dims or keeps each channel independently. Memory is per-dimension, not all-or-nothing.
+ add = writeNew content enters only by addition. Addition never distorts what was already there — unlike a vanilla RNN's full-matrix remix.
No matrix on the laneNothing but f_t and i_t ever touches c_{t−1}. That is why the gradient (∂c_t/∂c_{t−1} = f_t) survives hundreds of steps — §09.
Private vs publicc_t is private memory; h_t is the public summary. The output gate decides what the rest of the network may see.

04 STEP BY STEP · THE CLASSIC WALKTHROUGH

Follow the signal, gate by gate

The five moves every LSTM cell makes, in the exact order of the classic walkthrough — cell state → forget gate → input gate → cell update → output gate. A glowing dot rides the live data path at each step; the dialog underneath unpacks the underlying math, symbol by symbol.

STEP 1 / 5

Step order and diagram conventions follow “Introduction to Long Short-Term Memory (LSTM)” (Analytics Vidhya) and colah's blog; the equations are the standard Hochreiter & Schmidhuber (1997) formulation — the same six lines dissected in §07.

05 GATE INTUITION LAB

You be the gate

Each scenario puts you in charge of one gate's pre-activation z. Slide it, watch σ(z) open or close the gate, and get judged against what a trained LSTM would do.

Gates in isolation are easy. The magic is four decisions per word, sustained over a whole sentence. Time to watch a full read.

06 LIVE CELL · READING A SENTENCE

Watch memory being managed

Three classic probes: a Winograd pronoun, a slow-burn review, and a subject–verb agreement trap. Step word by word — left, the three gates; right, the six cell channels after each update.

CELL ACTIVE

TOKEN STREAM · click any word to jump

f_t · FORGET — keep old memory?0.00

high → previous cell content survives

i_t · INPUT — write new content?0.00

high → this word enters the cell

o_t · OUTPUT — reveal memory?0.00

high → h_t exposes the cell right now

CELL STATE c_t · six channels (teal = positive, rose = negative)

READY

You've now seen the behavior. Here is the exact machinery that produces it — six lines that launched a thousand models.

07 THE GOVERNING EQUATIONS

Six lines. Every symbol touchable.

Hover or tap any highlighted symbol — the inspector below explains its job, shape and personality.

f_t=σ(W_f·[h_{t−1}, x_t]+b_f) · i_t=σ(W_i·[h_{t−1}, x_t]+b_i)
c̃_t=tanh(W_c·[h_{t−1}, x_t]+b_c)
c_t=f_tc_{t−1}+i_tc̃_t
o_t=σ(W_o·[h_{t−1}, x_t]+b_o) · h_t=o_ttanh(c_t)

READING ORDER

① all four pre-activations come from the same concatenated input — one big matmul in practice, sliced four ways.

② only then does the cell update happen: forget, then write.

③ h_t is derived last, from the new c_t.

④ parameter count: 4 × d_h × (d_h + d_x + 1) — exactly 4× a vanilla RNN layer.

08 ONE TIMESTEP, BY THE NUMBERS

Hand-computed. No magic left.

Hidden size 2, input size 1, actual weight matrices. Step through all six computations and watch [h, c] = [0.515, −0.229], [0.732, −0.603] emerge — the code lab in §11 reproduces it exactly.

Forward pass understood. But the reason the LSTM conquered the 2010s lives in the backward pass.

09 WHY IT TRAINS

The gradient highway

In a vanilla RNN, ∂h_t/∂h_{t−1} contains a weight matrix and a tanh′ — magnitudes below 1, compounding to zero. On the cell path, ∂c_t/∂c_{t−1} = f_t. Nothing else. Drag the forget gate and compare the two chains.

LSTM forget gate f_t on this path0.95

Caveat, honestly: gradients into the gates themselves still pass through h_t and can vanish — LSTMs mitigate, not abolish. But the memory path is safe, and that is what carries long-range signal. Transformers later took the same idea further: residual connections are the gradient highway generalized.

10 THE FAMILY

LSTM vs GRU — same idea, leaner body

Cho et al. (2014) asked: do we need three gates and two states? The GRU says no — and often matches the LSTM with ~25% fewer parameters. Open the full GRU deep dive →

Pick LSTM when…sequences are very long, memory must be selective and precise, or you need the separate private/public state split (e.g. attention decoders).
Pick GRU when…data or compute is limited, sequences are moderate, or you want faster iteration — fewer parameters, no cell state to carry.
Empirically…no consistent winner (Greff et al., 2017, ablated 8 variants: the vanilla LSTM with forget bias 1 is remarkably hard to beat). Try both; keep the simpler one that works.
AspectVanilla RNNGRU · 2014LSTM · 1997
State vectors1 · h only1 · h merged2 · private c_t + public h_t
Gatesnone2 · update z, reset r3 · forget, input, output
Memory updateh = tanh(W·[h,x]) — full remix(1−z)⊙h + z⊙h̃ — convex blendf⊙c + i⊙c̃ — gated, additive
Gradient path× W·tanh′ per step — vanishes× (1−z) on the keep branch× f only — the highway
Erase / write couplinginseparablecoupled — writing erases (1−z vs z)independent — f and i decide separately
Parameters (per layer)d·(d+h+1)3·d·(d+h+1)4·d·(d+h+1)
Forget-bias trickn/apartial (via z)b_f = 1 — the classic init
Best attoy demos, teachingsmall data, tight computevery long sequences, precise memory

One-line summary: the LSTM wins not by remembering harder but by deciding — three learned gates turn memory from passive decay into an active, per-channel policy, and the additive cell lane keeps the gradient alive long enough to learn that policy.

11 CODE LAB

From framework call to from scratch

The PyTorch tab is what you'll write at work. The NumPy tab is what's actually happening — one matmul, four slices, and the famous line. It reproduces §08's hand numbers to the third decimal.

LSTM · TWO WAYS

12 SUMMARY

What to carry out of this room

01 · The wound

Vanilla RNNs multiply gradients by W·tanh′ every step back — memory decays exponentially. Long-range dependencies die in training, not in forward pass.

02 · The fix

A private cell state updated only by f_t ⊙ c_{t−1} + i_t ⊙ c̃_t — multiply and add, never a matrix. Memory persists because nothing forces it to decay.

03 · The gates

Forget (delete), input (write), output (reveal): three sigmoid layers, all reading [h_{t−1}, x_t], each learning its own policy. σ ∈ (0,1) makes them soft switches.

04 · The highway

∂c_t/∂c_{t−1} = f_t. Keep the forget gate near 1 and gradients flow hundreds of steps — the same trick residual networks and transformers would later generalize.

05 · The cost

4× the parameters and compute of a vanilla RNN, still strictly sequential — no parallelization across time. That last weakness is exactly what attention was invented to break.

06 · The family

GRU merges the two states and two of the gates: ~25% fewer parameters, usually within noise of the LSTM. Default recipe: LSTM with forget-bias 1, or GRU when small.

13 MASTERY CHECK

Eight questions. No peeking at the conveyor.

YOUR SCORE

0 / 8