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

STEP 1Reads word 1, creates memory h1
→
STEP 2Reads word 2 plus h1, updates to h2
→
STEP 3Reads word 3 plus h2, updates to h3
→
…Same weights reused at every step
→
OUTPUTPrediction from the final memory

Four shapes of sequence tasks

ShapeInput → outputExample
One to oneOne input, one answer (not really a sequence)Is this photo a cat or a dog?
Many to oneA sequence in, one answer outA customer review → positive or negative
One to manyOne input, a sequence outA photo → a caption; a first note → a melody
Many to manyA sequence in, a sequence outEnglish sentence → Hindi sentence; sales history → next 3 months

What a good sequence model must handle

NeedWhyExample
Different lengthsSentences and histories are short or long"Great food" vs a 200-word review
Long-range linksThe clue may be far back"I grew up in Kerala … so I speak fluent ___" needs "Kerala" from many words earlier
Word orderSame words, different meaning"Not bad, actually good" vs "Not good, actually bad"
Shared learningA pattern learned at step 1 should work at step 50Recognising "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:

ProblemWhat happensCommon fixes
Exploding gradientsNumbers bigger than 1 multiplied many times blow up; training goes wildGradient clipping: cap the size of each update
Vanishing gradientsNumbers smaller than 1 multiplied many times fade to zero; early words are forgottenGated 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:

GateThe question it answers
Forget gateWhat from the old memory should I throw away?
Input gateWhat new information is worth storing?
Output gateWhich 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.

RNNLSTMGRU
Long-term memoryWeakStrongGood
GatesNone32
SpeedFastestSlowestIn between
Good choice forShort sequences, learning the ideaLong sequences where accuracy matters mostLSTM-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 min

Explain an LSTM in your own words

  1. Ask a chatbot: Explain how an LSTM would forecast my monthly electricity bill using the forget, input and output gates, in 6 short lines.
  2. Then ask: Why do chatbots use transformers instead of LSTMs?
  3. 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.

Test what you learned

Take a quick quiz, or look up any word in the glossary.