Lesson 13: Recurrent Networks and Sequences
Pictures have space; language, music and measurements over time have order. A sentence is not a bag of words — "the dog chased the cat" and "the cat chased the dog" use the same words and mean opposite things. Networks that handle sequences need memory: a way to carry what has happened so far into the next step. Recurrent neural networks, or RNNs, are the classic design for that.
An RNN processes a sequence one step at a time, and at each step it updates a hidden state — a vector of numbers that summarises everything seen so far. The same weights are used at every step, so the network learns a general rule for updating its memory, not a separate rule for each position. To predict the next word, the network reads its hidden state; to keep going, it feeds the new word back in and updates the state again. The loop is the memory.
The Problem with Long Memories
In theory an RNN can remember the whole sequence. In practice its memory fades. The hidden state is updated by the same weights at every step, and the gradient has to travel backwards through every step of the sequence — the same vanishing-gradient problem from Lesson 4, now stretched across time. After twenty or thirty steps, the signal from an early word is almost gone, so the network forgets the beginning of a long sentence.
The long short-term memory network, or LSTM, was designed to fix this. It adds a separate memory cell with gates — small mechanisms that decide what to keep, what to add, and what to forget. The cell can carry information across many steps with little change, so the gradient survives the journey. LSTMs were the standard tool for sequences for years, and they still work well for modest problems, even though the transformer, which you meet next, has largely replaced them for language.
Where Sequences Appear
Sequences are everywhere once you look: the words in a sentence, the notes in a melody, the daily readings from a sensor, the prices in a market. The RNN's lesson is general — when order matters, a model needs a memory that updates step by step — and the LSTM's lesson is equally general: memory needs protection, or it fades. The transformer keeps both lessons and changes the mechanism, as the next lesson explains.
Sequences in the World Around You
Sequences are not only language. The daily temperature readings from a weather station form a sequence, and predicting tomorrow's reading from the last week's is a sequence task. A piece of music is a sequence of notes, and a model that has learned musical patterns can continue a melody. The movements of a bank account — deposits, withdrawals, purchases — form a sequence that can reveal fraud, because a fraudulent transaction breaks the pattern of a person's ordinary behaviour. In each case the order is the information: shuffle the readings, notes or transactions and the meaning disappears.
That is why the RNN's idea — a memory that updates step by step — was so important, even though transformers have largely replaced it. The RNN was the first architecture to take order seriously, and every sequence model since has had to answer the question it raised: how does a model remember what matters and forget what does not? The LSTM's gates were one answer; attention is another. When you meet a new sequence model in the future, ask how it answers that question, and you will understand its design.
Why Order Matters More Than Speed
An RNN reads a sentence the way a person does: one word at a time, carrying the meaning forward. That sounds slow, and it is — but it also matches how meaning actually builds in language, where the start of a sentence shapes what the end can mean. The transformer's parallel reading is faster, but it has to work to recover the order that the RNN gets for free, which is why positional information matters so much. Neither design is simply better; each trades something away. The RNN trades speed for a natural sense of order; the transformer trades a natural sense of order for speed and long-range memory. Knowing what each design gives up is the key to choosing between them — and to understanding why the field moved from one to the other.
Key Terms
- Sequence
- Data where order matters, such as the words in a sentence or readings over time.
- RNN
- Recurrent neural network: a network that processes a sequence step by step, carrying a hidden state.
- Hidden state
- A vector of numbers summarising what an RNN has seen so far in a sequence.
- LSTM
- Long short-term memory: an RNN variant with a protected memory cell and gates.
- Gate
- A mechanism in an LSTM that decides what to keep, add or forget in the memory cell.
- Memory cell
- The LSTM's long-term store, designed to carry information across many steps.
Discussion Questions
- Why does a plain list of words fail to capture a sentence's meaning? Give an example of your own.
- An RNN uses the same weights at every step. Why is that an advantage, and what does it force the network to learn?
- If an LSTM can remember for longer, what new mistakes might it make that a plain RNN would not?
Practice Questions
- What is the hidden state of an RNN?
- Why do the gradients vanish in a long sequence?
- What problem does the LSTM's memory cell solve?
- Give two examples of sequence data outside language.
- Why does an RNN share the same weights at every step?
Show answers
- A vector of numbers that summarises everything the network has seen so far in the sequence. It is updated at each step and used to make predictions.
- Because the gradient must travel backwards through every step, multiplying by small slopes each time, so the signal from early steps fades.
- It carries information across many steps with little change, so the network can remember the beginning of a long sequence. The gates control what enters, stays and leaves.
- Any two of: notes in a melody, daily sensor readings, prices over time, or DNA bases. In each case the order of the items matters.
- So it learns one general rule for updating its memory, which works for sequences of any length. Separate weights per position would only work for one fixed length.