3.5.4 · D2Sequence Models

Visual walkthrough — Long Short-Term Memory (LSTM) cells

2,874 words13 min readBack to topic

This is the visual companion to Long Short-Term Memory (LSTM) cells. It exists because that note showed the cure for vanishing gradients; here we see the cure.


Step 0 — The problem we are escaping

Before we build anything, look at what goes wrong without an LSTM.

A plain recurrent network keeps one memory vector, the hidden state (a list of numbers describing "what I remember at step "). At each new step it is completely rewritten:

Reading it term by term: is a fixed grid of learned numbers applied to the old memory ; is a second grid applied to the current input ; is a learned offset that shifts the total; and (a function that gently bends every number into the range to ) squashes the result. The point to notice is the first term: the old memory is passed through the same grid every step. Multiplying by the same grid over and over is like multiplying a number by again and again: . The early memory fades geometrically.

Figure — Long Short-Term Memory (LSTM) cells
Figure s01 — A curve of against the number of steps we look back. The red dot marks the strong early memory; by twelve steps the surviving signal is essentially zero. This is the geometric fading a plain RNN cannot avoid.

Everything below is us earning that sentence.


Step 1 — The conveyor belt: the cell state

WHAT. We introduce a second memory vector called the cell state , a list of numbers (say 128 of them) that runs straight along the top of the cell. Read the subscript as "at time step ".

WHY. We want a channel where information can travel from step to step untouched unless we deliberately touch it. Think of a horizontal conveyor belt: a box placed on it at step 1 rides to step 100 without anyone lifting it off.

PICTURE. The red line is the belt. Notice it goes straight across — no grid , no sitting on the main road.

Figure — Long Short-Term Memory (LSTM) cells
Figure s02 — The cell state drawn as a red conveyor belt running straight from on the left to on the right. Boxes of memory sit on it and ride across untouched: no weight grid and no live on this main road, which is exactly why memory can survive many steps.

We will build the machinery that reads and writes the belt out of small, understandable parts. The first part is a valve.


Step 2 — A valve: the sigmoid gate

WHAT. A gate is a vector of numbers each between and . We get those numbers from the sigmoid function, written :

Here is any real number and . Let's read the formula: when is very negative, is huge, so the fraction is tiny → output near . When is very positive, is near , so the fraction is near . At we get exactly .

WHY sigmoid and not something else? We need a dial that lands smoothly anywhere in meaning "block this", meaning "let it all through", and everything between meaning "let a fraction through". A hard on/off switch (step function) has no slope, so gradient descent could never nudge it. Sigmoid is the smooth switch.

PICTURE. The S-curve. The red dot at shows the midpoint; the flat tails are the "fully closed" and "fully open" ends.

Figure — Long Short-Term Memory (LSTM) cells
Figure s03 — The sigmoid curve rising smoothly from (far left, "valve closed") to (far right, "valve open"). The red dot at marks the exact midpoint value . Every gate in an LSTM is one of these smooth valves.

The dial takes its instructions from the current situation. The "situation" is the previous worker's report glued to the new input .


Step 3 — The forget gate: how much of the old belt to keep

WHAT. First we glue two vectors together. means concatenation: stack the old hidden state (length ) on top of the input (length ) to get one longer list of length . Then:

Term by term: is a grid of learned weights (shape ) that turns the situation into a raw score; is a learned offset (length ) that shifts that score up or down; crushes it into a dial between and . The result has one dial per belt lane.

WHY. Some memories should be dropped when a new fact contradicts them (e.g. the sentence's subject switches from singular to plural). The forget gate decides, per lane, how much of the old belt survives.

PICTURE. The belt reaching a row of red valves; valves stay wide open (memory flows on), valves pinch shut (memory erased).

Figure — Long Short-Term Memory (LSTM) cells
Figure s04 — The memory belt (black) meeting a row of red forget valves. Each valve's shade shows its opening : fully open at (memory flows on), nearly shut at , closed at (that lane erased). The kept memory is the element-wise product .

Element-wise, the belt after forgetting is , where means "multiply matching entries" ().


Step 4 — The input gate: what new thing to write, and how much

WHAT. Two pieces work together. First a candidate — a proposal of new content:

We use (range to ) here, not sigmoid, because content can be negative — memory should be able to say "the sentiment is negative" as a , not just "present/absent". Then a dial deciding how much of that proposal to actually write:

WHY two separate pieces? We split what (, signed content) from how much (, a dial). This lets the network form a strong candidate yet write only a whisper of it, or vice-versa.

PICTURE. Below the belt, (black arrow, a signed proposal) passes through the red valve before being allowed onto the belt.

Figure — Long Short-Term Memory (LSTM) cells
Figure s05 — A signed candidate rises as a black arrow from below and must pass the red input valve before joining the belt. The amount that actually gets written is the element-wise product .

The amount actually written is .


Step 5 — The additive update: the whole point

WHAT. Combine "what we kept" and "what we wrote":

WHY this is the hero equation. Look at how depends on : it is a plus, not a chain of matrix multiplies. Ask "if I nudge , how much does move?" — that sensitivity is:

Nothing else. During backpropagation the gradient travelling from step to step is simply multiplied by . If the network keeps , the gradient passes through unchanged — across 1 step or across 500. That is the gradient highway that plain RNNs lack (there the gradient is multiplied by every step and decays geometrically, exactly the fading from Step 0).

PICTURE. Two streams merging with a + node onto the belt, and the tiny red label on the arrow going backward in time — the only thing the gradient meets.

Figure — Long Short-Term Memory (LSTM) cells
Figure s06 — Kept memory (red belt from the left) meets new memory (rising arrow) at a + node, producing . The backward arrow underneath carries the label "gradient backward only": travelling from to the learning signal is scaled by nothing but .


Step 6 — The output gate: what to reveal

WHAT. The belt may hold big numbers; the outside world wants a tidy, bounded report. So we squash and filter:

Term by term: is a learned grid (shape ); is a learned offset (length ) that shifts the raw score before the sigmoid, exactly like in the earlier gates; turns the result into the reveal-dial . Then compresses the belt into for stability, and chooses per lane which parts of that compressed memory to expose as .

WHY separate memory from output? The network might store a fact ("the subject is female") for many steps but only reveal it at the moment a pronoun is generated. The belt keeps it; the output gate decides when to speak it.

PICTURE. The belt tapped from above: box then a red valve producing the worker , which then loops down to become next step's report.

Figure — Long Short-Term Memory (LSTM) cells
Figure s07 — The belt is tapped from above: it first passes through a box (squashing to ), then through the red output valve , giving . This is the tidy summary handed outside and looped back as the next step's report.


Step 7 — Edge and degenerate cases

Never leave the reader guessing what happens at the extremes. Each dial is in , so the corners are:

Situation Setting What the belt does
Pure memory — belt frozen, perfect recall (the counting task)
Full reset — old memory wiped, replaced by candidate (behaves like a plain RNN cell)
Wipe to zero — forget everything AND write nothing; the belt is blanked, a fresh start
Ignore step belt kept but nothing revealed: , silent memory
First step belt starts empty;

Counting, seen on the belt. With and each time a "[" arrives, the update is literally

so the belt reads — an exact counter. A plain RNN cannot do this: caps its memory at , so it can never store the number .

Figure — Long Short-Term Memory (LSTM) cells
Figure s08 — The cell state value plotted step by step as the tokens "[ [ [ [ ] ]" arrive. With the forget gate held at and a written on each "[", the red belt climbs then falls as brackets close — the LSTM is literally counting, something a -bounded RNN cannot do.


The one-picture summary

Here is the entire cell: belt across the top, three red valves () reaching up to it, the + merge that makes memory additive, and leaving to loop back. Trace the red belt with your finger — it never passes through a weight grid, and that is the whole secret.

Figure — Long Short-Term Memory (LSTM) cells
Figure s09 — The whole LSTM cell on one belt: enters at the left, is dimmed by the red forget valve , merges at the + node with the input contribution to become , and is tapped through and the red output valve to produce , which loops back as the next step's report. The unbroken red belt — never routed through a weight grid — is the whole secret that lets memory and gradient survive across many time steps.

Recall Feynman retelling — say it like a story

Picture a conveyor belt carrying boxes of memory. At every tick of the clock three little valves lean over the belt. The forget valve decides how much of each old box to keep — it dims them, it never throws dice. The input valve lets a freshly written box (which can be positive or negative, because a fact can be "yes" or "no") drop onto the belt, and the two are simply added together — kept memory plus new memory. Because it's addition, a box placed at the start can ride all the way to the end untouched, which also means the learning signal (how the final error depends on this memory) riding backward only gets multiplied by the forget dial each step — a number the network keeps near when it wants to remember — so nothing fades away. Finally the output valve peeks at the belt (after gently squashing it with ) and hands a tidy summary to the outside. Freeze the forget valve open and stop writing, and the belt becomes a perfect counter. That belt, and the plus sign that feeds it, is the entire reason LSTMs remember.

Recall Quick self-test

Why is used for the candidate but for the gates? ::: Candidate content must be signed ( to ), so ; gates are fractions of "how much", so ( to ). What is and why does it matter? ::: It equals ; the gradient is only scaled by this per step, giving a highway instead of geometric decay. With , what is ? ::: — the belt is frozen, perfect memory. With , what is ? ::: — the belt is completely blanked, a fresh start mid-sequence. Why can't a vanilla RNN count to 4? ::: Its memory passes through each step, bounding it to , so it can't hold the value .


Where next: the GRU merges these gates into fewer valves; bidirectional RNNs run two belts in opposite directions; and the belt idea is eventually replaced by direct look-back in attention and Transformers.