Skip to content
AI360XpertBeta
Core ML

Visual explainer

Long Short-Term Memory

Trace retained memory, signed writes and the hidden readout through an LSTM, then see how repeated forgetting can still erase the signal.

Repeated small derivatives shrink a backward signal.
Repeated small derivatives shrink a backward signal.

An LSTM keeps a separate cell state and learns gates that retain, write and expose information. This can provide a less attenuated path through time than a plain recurrent hidden state, but it does not guarantee perfect memory.

Start with an illustrative backward path whose local derivative is 0.25 at every hop. After k hops the signal is 0.25k0.25^k: 1, 0.25, 0.0625 and 0.000000953674 for k = 0, 1, 2 and 10. The last bar is too short to resolve; the open ring locates its endpoint without claiming a larger value. This is one controlled example, not a universal RNN decay rate. All four bars share 400 drawing units per unit of signal.

Separate The Cell State

A separate cell state can carry memory without a tanh at every step.
A separate cell state can carry memory without a tanh at every step.

The straight path passes through multiplication and addition, with no tanh on that direct route. In the ideal identity case, retaining all the old cell and writing zero leaves 1 unchanged. Exact forget gate 1 is a limiting case for a sigmoid, not its finite-input default.

For one coordinate, the standard update is:

ct=ftct−1+itgt,ht=ottanh⁡(ct).c_t=f_tc_{t-1}+i_tg_t,\qquad h_t=o_t\tanh(c_t).

Here c is cell memory, h is hidden state, f is the forget gate, i the input gate, g a candidate write and o the output gate. Gates are sigmoid outputs between zero and one; the candidate uses tanh and may be negative. PyTorch's LSTM equations specify the vector form. This page isolates a standard cell without projection or peephole connections.

Retain Then Write

A gated write adds to the retained cell memory.
A gated write adds to the retained cell memory.

The left bar is old cell 1. The right bar stacks two contributions: retain 0.9×1=0.900.9\times1=0.90, then add input-gated candidate 0.6×0.8=0.480.6\times0.8=0.48. New cell memory is 1.38. The stack uses 160 drawing units per cell unit, with a visible seam separating retained memory from the mint write. These chosen gate outputs are a synthetic fixture, not outputs from a trained language model. Unlike the tanh candidate, the accumulated cell is not restricted to [−1, 1].

Expose A Hidden Readout

The output gate exposes only part of the updated cell.
The output gate exposes only part of the updated cell.

All three bars share 160 units per value. Squash cell 1.38 to tanh⁡(1.38)=0.880951\tanh(1.38)=0.880951, then multiply by output gate 0.7 to produce hidden state 0.616666. The new cell remains 1.38; reading it does not replace it with 0.617. Both cell and hidden state are carried to the next recurrent step. The hidden state can also feed an output head; it is not itself a probability.

Compare Retention Rates

A forget gate near one preserves more of the direct memory path.
A forget gate near one preserves more of the direct memory path.

Hold each forget gate fixed for twenty updates and turn off all writes. Starting from cell 1, the remaining cells are 0.920=0.1215770.9^{20}=0.121577 and 0.9920=0.8179070.99^{20}=0.817907. The same scale, 240 units per cell unit, makes the difference visible; the upper dashed line marks the original value 1. This shows retained magnitude under controlled gates, not measured accuracy or proof that a word is remembered.

Holding gates fixed, the direct cell-path derivative is the product of the forget gates. The full derivative can include additional paths through hidden states and the gate computations. An additive update makes a favorable route possible; the learned gates determine how much it keeps.

Interpret Forget Bias

Forget bias changes the gate only together with its other inputs.
Forget bias changes the gate only together with its other inputs.

A forget gate is f=σ(a+b)f=\sigma(a+b), where a collects input and previous-hidden-state contributions and b is the effective bias. Set a to zero for this controlled comparison. Biases 0, 1 and 2 give gates 0.500, 0.731059 and 0.880797. Bars share a 0–1 scale, 200 units high. Bias 1 therefore does not mean a 0.9 gate. If a = −2 and b = 1, the gate instead becomes σ(−1)=0.268941\sigma(-1)=0.268941. Initialization can encourage retention, but the current input and hidden state still matter.

Respect Time Dependencies

Recurrent states impose an order on time steps.
Recurrent states impose an order on time steps.

Follow both rails: step two requires the cell and hidden states from step one, then step three requires those from step two. Independent sequences in a batch and matrix operations within a step can run in parallel. The within-sequence dependency remains. This is a dependency drawing, not a GPU-utilization or latency benchmark. Extra gates also add operations and parameters relative to a plain cell of comparable dimensions.

Write Can Subtract

A negative candidate can subtract from cell memory.
A negative candidate can subtract from cell memory.

Change only candidate g from +0.8 to −0.8 in the earlier fixture. Retained memory stays 0.90, but the write becomes 0.6×(−0.8)=−0.480.6\times(-0.8)=-0.48, leaving cell 0.42. The downward displacement is 86.4 drawing units on the shared 180-units-per-value scale. This is a fresh coordinate detail with an explicit scale change from the earlier stack. The input gate is still positive; the candidate supplies the negative sign.

Gate Each Coordinate

Forgetting acts separately on each cell coordinate.
Forgetting acts separately on each cell coordinate.

Now expand to two coordinates, A and B. Both start at 1, shown by dashed outlines. Choose forget gates (0.9, 0.1) and zero writes; their new cell values are (0.9, 0.1). The mint lengths use 300 drawing units per cell unit and intentionally overlay the original outlines. Real gates are vectors with one value per cell coordinate, not a single switch for the whole memory.

Where It Breaks

Repeated forgetting can still erase the direct cell signal.
Repeated forgetting can still erase the direct cell signal.

Keep forget gate 0.5 fixed and write nothing. Starting at cell 1 gives 0.5 after one step, 0.0625 after four and 0.510=0.00097656250.5^{10}=0.0009765625 after ten. The final bar is only 0.390625 drawing units long on the same 400-units-per-value scale; the ring marks its location. Amber and the approximation label identify the almost-lost signal.

With these gates held fixed, the direct cell-path derivative also shrinks by 0.5 each step. Learned gates, overwriting writes and the hidden readout can all affect what reaches a prediction. LSTM improves the available memory route; it does not remove vanishing gradients or guarantee retention at a particular sequence length.

The Quick Version

  • Repeated derivatives can attenuate a backward signal.
  • Separate cell memory avoids a mandatory tanh on its direct route.
  • Retained memory and gated writes add together.
  • Hidden state exposes a gated, squashed cell readout.
  • Retention depends on the learned forget gates.
  • Forget bias interacts with inputs and prior hidden state.
  • Each sequence still has recurrent time dependencies.
  • Negative candidates can subtract memory.
  • Gates act separately on cell coordinates.
  • Repeated forgetting can still erase useful information.