3.5.4 · D3Sequence Models

Worked examples — Long Short-Term Memory (LSTM) cells

2,310 words11 min readBack to topic

This page runs an LSTM cell through every kind of situation it can meet: gates fully open, fully closed, half-open, cell states that grow, cell states that vanish, the degenerate "brand-new cell" case, and a couple of exam twists. If you have not yet met the gate equations, read Long Short-Term Memory (LSTM) cells first — this page assumes those six lines and then stress-tests them.

Everything below uses 1-dimensional cells () so every number is something you can hold in your head. The formulas are identical in higher dimensions — you just apply them element-by-element.

Recall The six lines we will keep reusing

Here squashes any real number into , and squashes into . The symbol is element-wise multiply. Since we work in 1D, is just ordinary multiplication.


The scenario matrix

Think of an LSTM step as three knobs — forget , input , output — each of which can be near , near , or somewhere in between, plus the state of the memory (empty, small, large). Every real behaviour is some combination. Here is the full grid we will cover:

# Cell class What it models
A Pure carry (gradient highway) remember forever, ignore input
B Pure overwrite new wipe old, store fresh
C Partial blend mixed smooth interpolation
D Sealed memory store now, expose later
E Degenerate start any any any any cold boot
F Counting / accumulation additive integer count
G Saturation limit , why caps
H Gradient decay contrast vs LSTM vs vanilla RNN over many steps
I Real-world word problem learned learned learned negation in sentiment
J Exam twist trick trick trick "what forces remembering?"

The examples below are labelled with the cell(s) they hit. Between them they touch every row.


Example A — Pure carry: the gradient highway

Forecast: guess before reading — does the memory change at all? What number does the gradient carry?

  1. Apply the cell update. . Why this step? The whole point of the forget/input pair is that with the memory is copied unchanged — no matrix, no in the path.
  2. Differentiate. From , treating as fixed, . Why this step? This is the "superhighway" claim from the parent note made concrete. A gradient multiplied by at every step neither shrinks nor grows.

Verify: over steps of pure carry, the gradient is — no decay. Compare with a vanilla RNN weight : , effectively zero. This is exactly the vanishing gradient the LSTM defeats.


Example B — Pure overwrite

Forecast: roughly, how much of the old survives?

  1. Old contribution. . Why this step? A near-zero forget gate means the past is almost erased — only leaks through.
  2. New contribution. . Why this step? An open input gate lets nearly the whole candidate in; the sign is negative, showing cell states carry signed information ( output).
  3. Combine. .

Verify: the result is dominated by the fresh , with a tiny trace of the old positive value nudging it up. That matches "overwrite, don't blend." Sign flipped from to — the memory now stores the opposite fact.


Example C — Partial blend (the continuity case)

Forecast: is "forgetting 40% of the cell" or something else?

  1. Kept memory. . Why this step? scales the memory by — every dimension shrinks smoothly to , it does not randomly delete of dimensions.
  2. Added candidate. . Why this step? Only half the candidate is admitted, showing the gate as a dimmer switch, not an on/off toggle.
  3. Sum. .

Verify: both parts are strictly between full-keep and full-drop, so sits between the old and the candidate — a genuine interpolation. Because everything is smooth, the whole thing is differentiable, which is why gradient descent can train it.


Example D — Sealed memory: store now, reveal later

Forecast: the memory is strong — will the hidden state be strong too?

  1. Squash the cell. . Why this step? never reads raw; it reads so the exposed value is bounded to .
  2. Apply output gate. . Why this step? A closed output gate means "I hold this memory but choose not to show it now" — think of storing grammatical gender and only revealing it when a pronoun is needed.

Verify: memory is large, yet is tiny — the fact is retained internally but invisible externally. This is the cell/hidden-state split. Later, if opens to , jumps to without changing at all.


Example E — Degenerate cold start ()

Forecast: what happens to the forget term when there is nothing to forget?

  1. Forget term vanishes. . Why this step? This is the degenerate case: no matter what is, multiplying by gives . On the first step the forget gate is irrelevant.
  2. Only the input term survives. . Why this step? The very first memory is built purely from the input path — there is no past to carry.
  3. Hidden state. .

Verify: with an empty start the cell equals exactly , and is a squashed, gated version of it. Everything reduces cleanly — no division, no undefined quantity, so the cold boot is well-behaved.


Example F — Counting: additive accumulation

Forecast: can a bounded-activation network hold the exact number ?

  1. Recurrence. . Why this step? With those learned gate values the update collapses to plain integer addition — the cell literally becomes a counter.
  2. Unroll. . Why this step? Each additive step increments by exactly one; there is no on the memory path, so nothing caps the value.

Verify: exactly. A vanilla RNN with can never reach because — it would saturate near (shown next). The additive cell path is what makes exact counting possible.


Example G — Saturation limit: why needs

Forecast: does blow up to as well?

  1. Squash the huge cell. (to machine precision, ). Why this step? of any large positive number is essentially . This is exactly why the hidden path uses : it protects downstream layers from an unbounded conveyor belt.
  2. Output. .
  3. Limit. As , , so . Symmetrically . Why this step? This is the limiting-behaviour cell of the matrix: the memory can be arbitrarily large, yet the exposed state is always trapped in .

Verify: but — the internal counter keeps rising while the external signal is safely capped. The memory remembers "twenty"; the world just sees "very positive."


Example H — Gradient decay: LSTM vs vanilla RNN

Forecast: vs — a difference per step. How different are they after 50 steps?

  1. LSTM path. . Why this step? The cell-state gradient multiplies only by the forget gate each step — no weight matrix, no shrinking it.
  2. Vanilla RNN path. . Why this step? The RNN gradient multiplies by each step, here abstracted to , and makes decay worse.
  3. Compare. LSTM keeps ; RNN keeps — over 100× stronger signal.

Verify: . The LSTM preserves roughly two orders of magnitude more gradient, which is precisely why it learns long-range dependencies. See 3.5.03-Vanishing-and-Exploding-Gradientsin-RNNs for the RNN side.


Example I — Real-world word problem: "not bad" is positive

Forecast: the word "not" is three tokens before "bad" — will the network still know it saw "not"?

  1. Store negation. At "not", . Why this step? Input gate opens to write the fact "a negation just happened" into memory.
  2. Carry through "was". . Why this step? Forget gate stays high so the negation persists nearly intact across an irrelevant word.
  3. Still present at "bad". . Why this step? When "bad" arrives, the cell still holds of negation, so the network flips "bad" into "positive overall."

Verify: starting at , after two carries the memory is — over retained. The negation survived the gap, which is exactly what an RNN would fail to do. Contrast the GRU, which merges these gates, and attention, which skips the carry entirely.


Example J — Exam twist: what bias forces "remember by default"?

Forecast: should be positive, negative, or zero?

  1. Set up the equation. We need . Why this step? At initialization the weighted term is , so the gate value is decided entirely by the bias.
  2. Invert the sigmoid. . Why this step? The inverse sigmoid (the "logit") answers "which input produces this output?" — the same undo-question logic as arctan for tan.
  3. Interpret. A positive pushes forget gates near , so untrained LSTMs default to keeping memory, giving gradients a head start.

Verify: exactly. This is the well-known "forget-gate bias = 1 or 2" initialization trick, here solved precisely.


Where this goes next: the same gating idea, simplified, becomes the GRU; run two LSTMs in opposite directions and you get bidirectional RNNs; drop the recurrence entirely for attention and Transformers.