5.6.13 · D2Machine Learning (Aerospace Applications)

Visual walkthrough — LSTM — gates, cell state

2,320 words11 min readBack to topic

This page is the Hinglish version parent's visual companion. If a word feels unfamiliar, it links back to Recurrent Neural Networks (RNN), Vanishing & Exploding Gradients, Sigmoid and Tanh Activations, or Backpropagation Through Time (BPTT).


Step 1 — What "memory across time" even means

WHAT. Picture a row of boxes, one per instant in time: . Each box holds a number that is the network's memory right then. An arrow carries memory from one box to the next.

WHY start here. Before we can ask "why do things get forgotten," we need a picture of memory being passed along a chain. That chain is the heart of every recurrent network.

PICTURE. Look at the figure: the boxes are the timesteps, the orange arrows are the "pass memory forward" links. The value inside each box is written — just "the memory at time ." Nothing more yet: for memory, subscript for which instant.

Figure — LSTM — gates, cell state

Step 2 — The plain-RNN way: overwrite every step

WHAT. A vanilla RNN builds the new memory by squashing a mix of the old memory and the new input. In symbols, with the incoming data at time :

Let us earn each piece:

  • — a single fixed number the net multiplies the old memory by (a "gain").
  • — the memory one tick ago.
  • — the fresh input scaled by another number .
  • — the squashing curve from Sigmoid and Tanh Activations. It takes any real number and gently bends it into the band .

WHY and not just leave the sum alone? Without a squash, the running sum could blow up to huge numbers. keeps every memory value polite — inside — so the network stays numerically stable. That is the only reason it is there.

PICTURE. The figure shows the curve. Notice two things we will need: (1) it flattens out at the edges, and (2) its slope (how steep it is) is largest in the middle and nearly at the edges. Slope will be the villain of Step 3.

Figure — LSTM — gates, cell state

Step 3 — Why the plain RNN forgets: a product that shrinks to zero

WHAT. To learn, the network asks: "if I nudge the old memory , how much does today's memory change?" That sensitivity is written — read it as "the change in per unit change in ." The symbol is just "a tiny change in," borrowed from Backpropagation Through Time (BPTT).

Because memory passes through boxes to get from to , the chain rule multiplies the per-step sensitivities together:

Here means "multiply all these together," and is the slope of the curve we flagged in Step 2 — a number that is at most and usually well below it.

WHY this is fatal. Multiply numbers each smaller than and the result races to . If , then after steps you have . The signal from ticks ago is essentially gone — the vanishing gradient of Vanishing & Exploding Gradients.

PICTURE. The figure plots against . Watch the plum curve nose-dive: by the memory link is invisible. This is why a plain RNN cannot remember a spike from steps ago.

Figure — LSTM — gates, cell state
Recall

Why does a plain RNN forget long ago events? ::: Its time-to-time sensitivity is a product of many factors each (slopes of times the weight), which shrinks toward exponentially.


Step 4 — The fix: a second, ADDITIVE memory line

WHAT. Instead of overwriting memory through a squash, the LSTM keeps a separate line — the cell state — and updates it by adding, not rewriting:

New symbols, each earned:

  • — the cell state, a memory value carried straight along a "conveyor belt."
  • — the forget gate, a dial between and : how much of the old memory to keep.
  • — the input gate and candidate; together they are "how much new stuff to add." We build them properly in Step 6. For now treat their product as a single quantity: "the write."
  • — elementwise multiply (multiply matching slots). For scalars it is ordinary multiplication.

WHY additive? Because the dangerous operation in Step 3 was multiplying through a squash. Addition has no squash and no weight in the memory path. The old memory can simply ride forward and have a little added to it — like dropping a note on a moving conveyor belt instead of rewriting a sticky note.

PICTURE. The figure draws the conveyor belt: enters on the left, the forget-dial scales it, the "write" is added on top, and leaves on the right — straight line, no bend.

Figure — LSTM — gates, cell state

Step 5 — The derivation payoff: the sensitivity is just

WHAT. Redo the Step-3 question, but on the cell line. Ask: "how much does change if I nudge ?" Differentiate , treating the write as an input:

That is the whole answer. No weight matrix. No . Just the dial.

WHY this changes everything. Chain it across steps exactly as before:

Now the multiplied factors are the forget gates themselves. If the network chooses (keep everything), the product stays near — the gradient flows back undiminished across thousands of steps. This is the constant error carousel.

PICTURE. The figure compares two products side by side: the plum RNN curve (dies) versus the teal LSTM curve (flat at ). Same horizontal axis, radically different fate.

Figure — LSTM — gates, cell state

Step 6 — Where the dials come from (building the gates)

WHAT. A gate is not a hand-set number — the network computes it from what it currently knows. Concatenate the previous readout and the new input into one stacked vector , then run an affine map through a squash:

  • — the sigmoid from Sigmoid and Tanh Activations: , squeezing any number into .
  • — a weight matrix and bias; "" is just "scale and shift the inputs."
  • — the output gate (used in Step 7).

The candidate uses instead:

WHY sigmoid for gates, tanh for the candidate? A gate is a valve opening: = shut, = wide open — so it must live in , and sigmoid is the curve that maps everything there. A candidate is a value to store, which might be positive or negative, so it uses 's signed band .

PICTURE. The figure overlays sigmoid (teal, penned into — a valve) and tanh (orange, into — a signed value), with the and guide lines marked.

Figure — LSTM — gates, cell state

Step 7 — Reading the memory out (the output gate)

WHAT. The cell state can grow large (it is a running additive sum). We do not feed it out raw. Instead:

  • — the hidden state, the vector that actually leaves the cell and becomes the prediction (and part of next step's ).
  • — squashes the possibly-huge cell value back into so downstream layers stay stable.
  • — the output gate deciding how much of that to reveal now.

WHY two different memories? is the protected archive (updated additively, never squashed on the way through). is the filtered readout — what you choose to say out loud this instant. Only leaves the cell.

PICTURE. The figure shows the cell belt on top and the readout branch dropping down: times .

Figure — LSTM — gates, cell state

Step 8 — Edge cases: check every dial setting

We must make sure no reader hits a corner we skipped. Each row of the figure is one extreme.

Behaviour Meaning
Copy forever. Perfect memory (Worked Example 2).
Full reset. Behaves memoryless — the "safe default" trap.
Wipe. Memory erased, nothing written.
Accumulate. Can grow unbounded → why needs .

WHY show all four. The gates are learned data-dependent switches; training can land on any of these. The dangerous one is : it silently turns your LSTM back into a forgetful RNN. That is why LSTMs use a positive forget-gate bias ( to ), so early on — remember by default.

PICTURE. The figure draws the four conveyor belts side by side, one per row of the table, so you see the memory copied, reset, wiped, or piled up.

Figure — LSTM — gates, cell state

The one-picture summary

Everything above collapses into a single diagram: the cell belt runs straight across (additive, sensitivity ), three sigmoid dials tap into it (forget scales the old, input+candidate add the new, output reads it out), and the hidden state drops out the bottom. The straightness of the belt is the reason gradients survive.

Figure — LSTM — gates, cell state

scale by f

sigmoid

sigmoid

tanh

sigmoid

tanh then scale by o

c prev

kept memory

z = h prev and x

forget f

input i

candidate

output o

write = i times candidate

c new = keep plus write

h new

Recall Feynman: the whole walkthrough in plain words

A normal memory robot rewrites its one note every second by squashing it — and squashing quietly shaves off a little of the learning signal each time, so after a few dozen seconds the signal is gone. The LSTM's trick is a second note on a conveyor belt. It never rewrites this note by squashing; it just scales the old note by a "keep dial" and adds a little new stuff. Because the belt is straight (scale-and-add, no squash, no hidden weight), asking "how much did the old note affect today's note?" gives back just the keep-dials multiplied together — and if the robot keeps those dials open, the answer stays big even after thousands of seconds. Three small dials run the show: forget (how much old to keep), input (how much new to add), output (how much to read aloud). The read-aloud value gets one gentle squash so it never explodes. That straight belt is the entire reason LSTMs remember what plain RNNs forget.


Connections