Recurrent Networks: Memory in Motion
CNNs see everything at once — sentences, audio and stock prices unfold. Watch a network read word by word while carrying a running summary of everything it has read, and meet the LSTM that learns what to remember and what to forget.
The Network That Reads in a Loop
A dense layer would need fixed-length input and zero memory between words. An RNN instead processes one token at a time, keeping a hidden state — a compressed summary of the story so far:
The secret is weight sharing across time: the same process every word. Unrolled, the single cell becomes a chain — watch the sentence “The cat sat down” flow through it in the animation, one glowing timestep at a time.
After the last word, holds the sentence's meaning — ready for sentiment, translation, or the next-word prediction behind every language model.
same weights at every step · the arrow between cells is the memory
Backpropagation Through Time — and the Vanishing Gradient
by h₁ the signal is nearly gone — early words barely learn
To train the shared weights, we unroll the loop into the chain above and backpropagate through every timestep at once — BPTT. But each backward hop multiplies the gradient by and the tanh derivative (≤ 1):
- Vanishing: factors < 1 compound — the signal decays to nothing and early timesteps never learn long-range patterns.
- Exploding: factors > 1 compound — the signal blows up to NaN. The cheap fix is gradient clipping: cap the norm before the update.
Vanishing can't be fixed by clipping — it needs an architecture that lets information bypass the multiplications. Enter the LSTM.
LSTM: Learn to Remember, Learn to Forget
The Long Short-Term Memory cell adds a cell state — a conveyor belt running along the whole sequence — guarded by three learned gates (each a tiny sigmoid layer outputting values in , i.e. “how much to let through”):
The addition in is the magic: the gradient can flow additively along the cell state for hundreds of steps without being repeatedly multiplied — the vanishing problem simply doesn't apply to the belt.
forget → input → output · three tiny sigmoid decisions per timestep
GRU, Bi-RNNs & Where Sequences Live Today
One matrix of weights, no gates. Cheap, fast — but forgets within ~10 steps. Fine for tiny sequences.
LSTM's leaner sibling: merges forget+input into one update gate. Nearly as good, 25% fewer parameters.
Two RNNs read the sequence in both directions and concatenate — 'bark' means different things after 'dog' or after 'tree'.
Encoder RNN compresses the input into a vector; decoder RNN generates the output — the blueprint of machine translation.
- · Time-series forecasting (energy, demand, sensors)
- · Streaming / online inference on devices
- · Small-data regimes where transformers overfit
- · Speech frontend features
RNNs must process token before token — no parallelism across time, and long-range memory still fades. The Transformer (2017) threw away recurrence for self-attention: everything in parallel, every token directly connected to every other. That is the architecture behind GPT — but its attention scores are exactly the weighted sums, sigmoids and softmaxes you have met across Topics 02–09.
Backprop Through Time: One Weight, Many Gradients
Section 02 named the algorithm; here is the mechanism. BPTT is nothing more exotic than unrolling the recurrence into a chain — — treating each timestep as an ordinary node, and running the plain backpropagation from Topic 07 on that unrolled graph.
The crucial part is that the copies are not separate parameters. Every cell in the chain is the same , so its gradient is the sum of the contributions from every timestep:
Each term is itself a product of local Jacobians travelling back from to the end. Multiply long enough and the magnitude of that product decides everything: slightly below 1 and it collapses toward zero (vanishing), slightly above 1 and it blows past the representable range (exploding).
- Exploding → gradient clipping: rescale the gradient whenever its norm exceeds a threshold, before the update.
- Vanishing → gating: an additive cell-state path (LSTM, Topic 03) that carries the signal across many steps without repeated multiplication.
vanilla bars shrink toward t=1 · gated bars stay level · signal sweeps right to left
