Sequences and the problem of memory
Fill the gap: does the model still see the clue?
Why the headline mattered
Count-based n-gram models only see the last few words. An RNN language model can, in principle, use the whole history, and in 2010 it beat the best n-gram systems by a wide margin. Click a milestone:
Walk a sentence through an unrolled RNN
Worked example: one step of the worked example, shape by shape
The same three matrix products happen at every step. Section 05 lets you change T, d, h and k.
The ideas that made it work: hidden state, weight sharing, backpropagation through time
Idea 1A hidden state carries memory
A one-unit RNN gets a single input pulse at t = 1 and nothing afterwards: h_t = tanh(w · h_(t−1) + x_t). How long does the pulse survive?
Idea 2Weight sharing across time
Give every step its own weights and the model grows with the sentence. Share them and it does not: 1 set of weights for all T steps.
Idea 3Backpropagation through time
Unroll the loop into a deep feed-forward chain, run ordinary backpropagation, and add up the gradient for the shared W_hh from every step.
The RNN rule: one cell, the same weights, every time step
One step by hand (one hidden unit)
Shared recurrent cell vs fixed-window feed-forward network
Design rules of the vanilla RNN. One cell; the same W_xh, W_hh, W_hy at every step; tanh keeps the state in (−1, 1); the output y_t = W_hy h_t + b_y is read at every step (or only at the last one); h_0 starts at zero.
Shapes by hand and by eye: (T, d) → (T, h) → (T, k)
Worked example by hand: d = 2, h = 2, two steps
Redo it on paper before you press Next step. Notice that h_2 depends on x_1 through h_1: the first input is still "in" the state after it is gone.
The calculator
Where the parameters live: W_xh, W_hh and W_hy
The arithmetic for that block appears here.
Parameters
Compute (multiply-adds)
Worked example: counting the 830 parameters
Any framework that builds this RNN with a separate output layer reports the same total, 830, whatever T you feed it.
The family: Elman, Jordan, bidirectional, stacked, LSTM and GRU
| cell | year | fed back | recurrent params | × Elman core | total params | mult-adds / step | sees future? | long-range memory |
|---|
Pick a row above.
Why 4× and 3×. An LSTM computes four gate-sized blocks (input, forget and output gates plus the candidate) and a GRU three (update and reset gates plus the candidate), each with its own W_x, W_h and bias. A vanilla RNN has one. So for the same d and h they carry 4× and 3× the recurrent parameters: 2,480 and 1,860 against 620.
Which one?
Short sequences, tiny devices: vanilla or GRU. Long dependencies: LSTM or GRU. Whole sequence available in advance (tagging, classification): bidirectional. Complex patterns: stack 2–3 layers. Real-time streaming: never bidirectional.
The cost: sequential steps, vanishing and exploding gradients
Budget · time for one sequence, one epoch, one training run
Gradients · the same factor, once per step: vanishing or exploding
Reuse and transfer: embeddings, encoder–decoder and fine-tuning
Which recipe?
The encoder–decoder bottleneck
A sequence-to-sequence model reads the source with one RNN (encoder) and hands only its final hidden state to a second RNN (decoder). Drag the source length.
History: from Jordan and Elman to LSTM and the Transformer
Vanilla RNN vs Transformer · tap a card to flip
Where RNNs are used today
On-device speech recognition
Recurrent transducer models turn audio frames into text as you speak.
Forecasting and sensors
LSTM and GRU models predict demand, energy load or machine wear.
Robotics and reinforcement learning
Recurrent policies remember what the agent can no longer see.
Handwriting, music, keyboards
Character-level RNNs generate pen strokes, melodies and next-word suggestions.
LSTM and GRU
The gated cells of section 07: the default "RNN" in practice.
Transformers, and recurrence returns
Attention replaced the loop; linear recurrences are bringing it back.