Long Short-Term Memory
Give the network a separate memory line that gates control instead of overwrite, so relevant information from many steps back can survive untouched until it's needed.
Why Does This Exist?
A vanilla RNN recomputes its hidden state fully at each step through a tanh. The gradient for a clue fifty steps back must survive fifty multiplies. In practice it shrinks toward zero. A subject twenty words back cannot steer its verb.
The 1997 fix did not tune that update. It added a second state whose job is to avoid the squash, edited by choice rather than overwritten. This page covers that memory line and its three gates. The plain starting point lives in vanilla RNN. The slimmer cousin lives in gated recurrent unit. Stacking such cells for translation lives in sequence to sequence.
Think of It Like This
A whiteboard with eraser, marker, and window
A project whiteboard holds notes for weeks. It is never wiped clean each meeting. Three small acts happen. Someone erases lines no longer needed, which is forget. Someone adds notes for what just came up, which is input. Someone glances and decides what to say aloud now, which is output, usually a filtered slice rather than the whole board.
The board is the cell state. It persists except where an act touches it. That differs from a vanilla RNN, where the whole board is rewritten every step whether anything changed or not.
Where it stops: meetings allow talk and erasure in any order with judgment. Gates are fixed multiplies between zero and one learned from data, with no notion of truth, and the window never changes what the board holds.
How It Actually Works
Three gates edit one memory line. Each gate is a sigmoid over current input plus prior hidden , giving per dimension numbers in that mean how much.
Forget decides what stays:
Near erases that dimension. Near keeps it. It scales old memory only and adds nothing.
Input decides what to add, with candidate :
Cell update combines both with elementwise multiply :
If and , then . Memory rides through untouched.
Output decides what others see:
Here feeds the next layer. It is a filtered view, not the memory itself.
Worked example with one step
Start cell . Forget keeps , so . Input opens on candidate , so . New cell is .
Squash with tanh. . Output opens , so . Memory moved to with a paper trail, and the next layer sees only . That move is the diagram headline.
Why gradients survive
Path from to multiplies by learned , not by a matrix through tanh. Learn for a dimension worth keeping and the gradient through that step is near . Chain one hundred such steps and it still survives, because no saturating nonlinearity sat on the highway. Forget is both memory control and gradient control, which is the long in the name.
Code
One step in plain Python matching the hand numbers.
import math
c_prev: float = 1.0f: float = 0.9i: float = 0.6c_tilde: float = 0.8o: float = 0.7
c: float = f * c_prev + i * c_tildeh: float = o * math.tanh(c)
print(f"{c:.2f} {math.tanh(c):.2f} {h:.2f}") # -> 1.38 0.88 0.62The printout matches , , . Set to and to and equals exactly. Untouched memory by construction.
Watch Out For
Forget bias starts at erase by default
Near zero init gives . Cell halves each step before learning anything.
Symptom: long range tasks learn slowly or stall while short ones look fine. Fix: init forget bias near so training starts at remember by default, and check your framework default before assuming neutral.
Feeding cell where hidden belongs
Cell is long memory. Hidden is the filtered output for this step. Same shape, different job.
Symptom: hand wired model trains worse with no crash, because output filtering never happens. Fix: pass onward and across time, keep on its highway, and assert shapes plus gate order in a tiny one step test like the code above.
The Quick Version
- LSTM adds a cell state beside the hidden state, edited by three sigmoid gates.
- Our step keeps 0.9 of 1.0 and adds 0.48 for a new cell of 1.38.
- Tanh of 1.38 is about 0.88 and output gate 0.7 gives hidden about 0.62.
- Forget near 1 preserves memory and gradient together across many steps.
- Cell and hidden are different objects with different jobs and are not interchangeable.