1. Memory needs selective updates
In the previous lesson, our scalar RNN mixed the old state with every new input. With a recurrent coefficient of and no later input signals, the original signal quickly faded.
That example does not describe every possible RNN. It does reveal a useful design question: could the network learn which information to keep and which new information to write?
Imagine a story establishes that its main character fears darkness. Several scenes later, the character enters a basement. The earlier fact may matter, while the intervening breakfast details may not.
A useful memory mechanism would regulate retention, writing and access. Long short-term memory, or LSTM, provides learned gates for these roles.
2. Two state paths: a carry route and a working view
An LSTM carries two vectors from one time step to the next:
- Cell state : an internal state with an additive update, providing a relatively direct path through time.
- Hidden state : a gated, transformed view of the cell state, used by later recurrent steps and often by an output layer.
The cell state is sometimes compared to a highway and the hidden state to a workbench. The analogies explain their different roles; they do not mean one stores information forever while the other only stores the current word.
Three gates regulate this standard LSTM cell:
- Forget gate: how much of each previous cell-state coordinate to carry forward.
- Input gate: how much proposed new information to write.
- Output gate: how much of the transformed cell state to expose as the hidden state.
Both states remain fixed-size vectors as the sequence grows. The model still compresses history rather than preserving a complete transcript.
3. A gate scales information between zero and one
A gate is a vector of values produced by a sigmoid function. A value near one passes most of a coordinate's signal; a value near zero suppresses it. Intermediate values scale it continuously.
For finite inputs, the mathematical sigmoid lies strictly between zero and one. Exact zero and one are useful idealised endpoints; our “disable later writes” experiment explicitly sets a gate to zero as a controlled intervention.
Let the current input be and the previous hidden state be . Concatenate them:
The forget and input gates are:
The proposed new content uses tanh:
The core update is:
Read it as new cell state = retained old state + admitted new content. The symbol means elementwise multiplication, not a dot product. Every coordinate can have its own gate values.
Finally, the output gate produces the hidden state:
These equations describe a common LSTM without peephole connections. If the hidden size is and input size is , each gate matrix has shape and each gate bias has entries.
Sigmoid is useful here because it supplies bounded scaling values. Its derivatives can still become small when it saturates; using it in gates does not magically remove every gradient problem.
| Teaching event | Forget gate | Input gate | Candidate | Output gate |
|---|---|---|---|---|
| Fear of darkness | 0.90 | 0.90 | 0.95 | 0.80 |
| Later event | 0.99 | 0.04 | 0.20 | 0.80 |
| Later event, writes disabled | 0.99 | 0 | 0.20 | 0.80 |
Unlike a gate, the candidate can be negative. New writes can therefore add to or cancel existing cell-state values. The cell state itself is not constrained to the interval from minus one to one; tanh is applied when computing the hidden state.
4. Calculate a gated memory step by step
Use one scalar cell coordinate as a teaching signal for “fear of darkness”. Start with .
For the first event, choose , and :
For each subsequent event, choose , and . Then:
With output gate , compute the working hidden state separately:
Follow a scalar cell state through nine teaching events
- fear of darkness
- the character
- ate
- breakfast
- left
- the room
- went downstairs
- entered
- the basement
Events read: 0 / 9 · Initial states are zero
Events include phrases rather than real tokenizer output. Gates and signals are hand-picked. Bars compare fractions within each toy state, not accuracy or equivalent learned features. Reset rewinds the events and keeps the selected writing mode.
Use Read next event to follow all nine events. The events include short phrases rather than actual tokenizer output. Gates and input signals are fixed teaching values, and the contrasting RNN uses the same scalar update as the previous lesson.
A high cell value is not the same as preserving the original signal
After eight later events, the default cell value is about , or 99.5% of . But every later event added . Those repeated writes partly replaced the cell value lost through the forget gate.
With the gate sequence fixed, the contribution from the initial write is instead:
So the original contribution retains about 92.3%, even though the total cell remains close to its initial value. The two percentages answer different questions.
Enable Disable later writes. The later input gate becomes zero, so no replacement content is added. The total cell then equals the carried original contribution. This isolates what the forget gate alone retains.
The RNN comparison retains about 1.5% of its first-event state after eight zero-signal updates. This illustrates the chosen recurrences, not a benchmark proving that an LSTM always outperforms an RNN. Cell and hidden states are different quantities; the bars compare fractions within each example.
5. A better path through time—and remaining limits
The additive cell update offers a direct carry path. Holding gate values and candidate content fixed, its local derivative is:
Along one coordinate of that direct path, the multiplier across a gap is a product of forget-gate values. If each is :
The first product contracts much less than the second. These are path multipliers, not universal measures of memory quality. The full gradient also has other paths through hidden states and the learned gates; it is not simply the forget-gate product.
Gating can help with long-range learning, but there is no universal promise that every LSTM reliably remembers exactly 50, 100 or any other fixed number of steps. Results depend on the task, data, architecture and training. Saturation, interference and gradient problems can still occur.
Recurrent steps remain sequential
To compute the next hidden and cell states, a standard LSTM needs the previous ones. Its recurrent state updates therefore form a sequential chain.
That does not mean GPUs cannot help: operations within a step and across independent sequences in a batch can be parallelised. The dependency between consecutive recurrent states remains a constraint on parallelising the sequence dimension.
LSTMs have been useful in speech, translation and other sequence tasks. Attention mechanisms introduce a different way to access relevant representations, and Transformer architectures allow substantial parallel computation across positions during training.
For causal language models, attention must still mask future positions. Autoregressive generation remains sequential across newly generated tokens. Moving to Transformers is not a promise of a particular reduction from months to days for every model or workload.
6. Chapter III is complete
LSTM regulates recurrent information flow with a cell state, a hidden state and learned gates.
- Two state paths: the cell carries internal information; the hidden state exposes a gated, transformed view.
- Three gates: forget, input and output control retention, writing and access.
- One core update: retained old content plus admitted new content forms the new cell state.
- A controlled comparison: our example separates total state value from the contribution of the first write.
- A remaining constraint: consecutive recurrent updates still depend on each other.
This chapter traced five useful ideas: N-grams → word vectors → feedforward language models → RNNs → LSTMs. Each introduces a new way to represent or use language context, with its own limitations.
The next chapter begins with attention: how can a model choose which other representations matter for a particular prediction?