AI & machine learning / Recurrent neural networks and LSTMs

Recurrent neural networks and LSTMs

How a network with a loop remembers a sequence, why that memory fades, the gates an LSTM adds to protect it, and what attention later did better.

Intermediate7 steps~7 min
Input — one item of the sequence
Cell — the same network, reused every step
Hidden state — the memory passed to the next step
Cell state — the LSTM’s protected long-term lane
// THE SHORT VERSION

In 30 seconds

A recurrent neural network reads a sequence one item at a time. After each item it updates a hidden state, a running summary that it feeds back into itself, so earlier inputs can shape later answers.

The catch: when it learns, the correction sent back through the steps shrinks at each one, so links across long gaps are hardly learned.

An LSTM adds a protected memory with gates that decide what to keep, write and read. Transformers later dropped the loop and let every word look at every other directly.

Recurrent neural networks and LSTMs, step by step

STEPS01 / 07

The toy RNN keeps one number; real ones keep vectors and use matrices. LSTM gates ease the vanishing gradient rather than remove it, and gradients can also explode, which is handled by clipping them.

STEP 01 / 07 A network with a loop
THE MATH
new state = f(old state, new input)
  1. Step 1: A network with a loop. Words, sounds and prices arrive in order, and what came earlier changes what the next item means. A recurrent neural network (RNN) reads one item per step and feeds a summary of everything so far, its hidden state, back into itself for the next step.
  2. Step 2: Hidden state is the memory. A toy RNN with a single number of memory. The old state is scaled by a weight w, the input by a weight u, and tanh squashes the sum to between −1 and 1. Starting empty and reading the input 1, the state becomes 0.76. Real networks keep hundreds of such numbers, but the update is the same.
  3. Step 3: Unroll it through time. To see what the loop does, draw the cell once per time step. It is the same cell with the same weights each time; only the state passing from left to right changes. Feed in a 1 and then zeros: the state still carries a trace of that 1 three steps later, though each step roughly halves it.
  4. Step 4: The learning signal fades. Training measures the error at the end and sends a correction, the gradient, backwards through every step. Each step back multiplies it by the weight w times the slope of tanh, here at most 0.5. Ten steps back only a thousandth is left, so early inputs hardly get learned. This is the vanishing gradient problem.
  5. Step 5: LSTM: gates around a memory. A long short-term memory (LSTM) cell adds a second state, the cell state c, that runs straight through. Three gates, each a small layer that outputs numbers between 0 and 1, control it: the forget gate scales what is kept, the input gate what is written, and the output gate what is shown as the hidden state h.
  6. Step 6: Memory that lasts. Along the cell state, a step back in time multiplies the learning signal by the forget gate’s value, and the network can learn to hold that near 1 for things worth keeping. With f = 0.99, ten steps back still leaves 90% of the signal, where the plain RNN kept 0.1%. That is how LSTMs learn links across long gaps.
  7. Step 7: Why transformers took over. Even an LSTM must read strictly in order, so it cannot process a whole sequence at once, and distant words are linked only through every step in between. The transformer (2017) dropped the loop: attention lets each word look directly at every other. It trained faster on far more text and took over most language work.
VALUES SO FAR
w····weight
h1····state
h4····state
0.5^10····RNN
gates····LSTM
0.99^10····LSTM
RNN····path
attn.····path

Spotted a mistake on this page?