RNNs, LSTMs and GRUs: AI that reads in order
Some data only makes sense in order: words in a sentence, daily sales, a heartbeat, a voice recording. A recurrent neural network (RNN) reads such data one step at a time and carries a small memory, the hidden state, from one step to the next. LSTMs and GRUs are improved RNNs with gates that decide what to remember and what to forget. Transformers have since taken over most language work, but RNNs are still used for time series and small devices.
Everyday analogy
Watching a TV serial episode by episode. You don't remember every scene, but you carry a running summary in your head ("the brother is hiding something") and update it after each episode. That running summary is the hidden state.
Example
Forecasting next month's claim volumes from the last 36 months: an LSTM reads the history month by month, remembers seasonal peaks such as winter flu season and summer travel, and predicts the next value.
What counts as sequential data?
- Text: "not good" means something different from "good"
- Speech and audio: sounds arrive one after another
- Time series: sales, prices, sensor readings, claim volumes
- Video: frames in order
How an RNN works
Four shapes of sequence tasks
| Shape | Input → output | Example |
|---|---|---|
| One to one | One input, one answer (not really a sequence) | Is this photo a cat or a dog? |
| Many to one | A sequence in, one answer out | A customer review → positive or negative |
| One to many | One input, a sequence out | A photo → a caption; a first note → a melody |
| Many to many | A sequence in, a sequence out | English sentence → Hindi sentence; sales history → next 3 months |
What a good sequence model must handle
| Need | Why | Example |
|---|---|---|
| Different lengths | Sentences and histories are short or long | "Great food" vs a 200-word review |
| Long-range links | The clue may be far back | "I grew up in Kerala … so I speak fluent ___" needs "Kerala" from many words earlier |
| Word order | Same words, different meaning | "Not bad, actually good" vs "Not good, actually bad" |
| Shared learning | A pattern learned at step 1 should work at step 50 | Recognising "not" wherever it appears |
Turning words into numbers
A network can't read words, so text is converted first: build a vocabulary of known words, give each word an index number, then turn each index into an embedding, a list of numbers where similar words get similar lists. See Embeddings and tokenization.
Training an RNN: backpropagation through time
To train an RNN, it is unrolled: drawn as one copy per time step, all sharing the same weights. The error is then sent backwards through every step, which is called backpropagation through time. Because the same weights are multiplied again and again, the learning signal can grow or shrink very fast:
| Problem | What happens | Common fixes |
|---|---|---|
| Exploding gradients | Numbers bigger than 1 multiplied many times blow up; training goes wild | Gradient clipping: cap the size of each update |
| Vanishing gradients | Numbers smaller than 1 multiplied many times fade to zero; early words are forgotten | Gated cells (LSTM, GRU), ReLU-style activations, careful starting weights |
The problem: a short memory
Plain RNNs forget quickly. In "The policy, which the customer first bought in 2019 and renewed several times after moving house twice, was cancelled", the network needs to remember "policy" across many words. During training the learning signal shrinks (or sometimes explodes) as it travels back through all those steps: the same problem described in vanishing gradients.
LSTM: a memory with gates
A long short-term memory (LSTM) network adds a separate long-term memory, the cell state, which runs through the sequence like a conveyor belt. Three gates control it:
| Gate | The question it answers |
|---|---|
| Forget gate | What from the old memory should I throw away? |
| Input gate | What new information is worth storing? |
| Output gate | Which part of my memory should I use right now? |
GRU: a lighter LSTM
A gated recurrent unit (GRU) merges the memory into one and uses two gates: an update gate (how much old memory to keep versus replace) and a reset gate (how much of the past to ignore when reading the new input). It is simpler and faster, and often almost as accurate.
| RNN | LSTM | GRU | |
|---|---|---|---|
| Long-term memory | Weak | Strong | Good |
| Gates | None | 3 | 2 |
| Speed | Fastest | Slowest | In between |
| Good choice for | Short sequences, learning the idea | Long sequences where accuracy matters most | LSTM-like results with less computing |
RNNs versus transformers
RNN family
- Squeezes the whole history into one small memory (a bottleneck)
- Reads one step at a time, in order
- Hard to train in parallel, so slow on huge data
- Struggles with very long text
Transformers
- Look at all the words at once using attention
- Train in parallel on enormous data sets
- Power today's LLMs (see Transformer architecture)
Where RNNs still make sense: small devices with little memory, live (streaming) speech, sensor monitoring and many business forecasting tasks.
Try it yourself
About 10 minExplain an LSTM in your own words
- Ask a chatbot: Explain how an LSTM would forecast my monthly electricity bill using the forget, input and output gates, in 6 short lines.
- Then ask: Why do chatbots use transformers instead of LSTMs?
- Write a one-line summary of each answer for yourself.
What you should see: The gates act like a filter on memory, and the main reason for the switch is that transformers read everything at once and train in parallel.
Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot) where needed. Never paste private or confidential information.