Frontend Labs
⋯

Chapter 17 — Networks that read sequences (RNNs) and why they struggled

In one sentence

Recurrent networks read text one word at a time while carrying a memory, which worked — but they were slow to train and forgetful, and the Transformer replaced them.

The problem

A regular neural network takes a fixed-size input. Sentences vary in length and order matters ("dog bites man" ≠ "man bites dog"). You need something that processes a sequence.

The idea: a loop with memory

Read one word, update your memory, repeat.

              h0        h1        h2        h3
              |         |         |         |
   [memory] --+-> [RNN] +-> [RNN] +-> [RNN] +-> ...
                   ^         ^         ^
                   |         |         |
                 "the"     "cat"     "sat"

At each step:  new_memory = f(old_memory, current_word)

The memory (h, the "hidden state") is a vector that summarises everything seen so far. Same weights reused at every step.

Why plain RNNs failed, and the LSTM fix

Backpropagating through 50 steps means multiplying 50 Jacobians — the vanishing- gradient problem (Chapter 15) at its worst. Plain RNNs effectively forgot anything more than ~10 words back.

The LSTM (Long Short-Term Memory, 1997) added a cell state — a memory conveyor belt with additive updates and learned gates controlling what to erase, write, and read:

   +-------------------- cell state (the conveyor belt) -------------------->
        ^ erase?          ^ write?                    ^ read?
        |                 |                           |
    forget gate       input gate                  output gate

Because the cell state is updated by addition rather than repeated multiplication, gradients survive far longer. This is the same trick as a residual connection. LSTMs could remember hundreds of steps and dominated NLP from ~2014 to 2017.

The 2014 breakthrough that led to Transformers: attention

For translation, the design was sequence-to-sequence: one RNN reads the source sentence into a single vector, another RNN generates the translation from it.

The bottleneck: the entire source sentence has to be squeezed into one fixed vector. Long sentences got mangled.

Attention (Bahdanau et al., 2014): instead of one vector, let the decoder look back at all the encoder's hidden states at each output step, and learn which ones to focus on.

Translating "the cat sat" -> "le chat s'est assis"

When generating "chat", attention weights might be:
    the: 0.1     cat: 0.8     sat: 0.1
                  ^^^^ focus here

This worked so well that in 2017 researchers asked the obvious question: "if attention is doing the heavy lifting, do we need the RNN at all?"

The answer was no. That paper was called "Attention Is All You Need", and it is Part 6.

Why Transformers replaced RNNs

Problem with RNNs Transformer's answer
Sequential — step 50 must wait for step 49, so you can't use a GPU's parallelism during training Processes all positions simultaneously — training becomes a few huge matrix multiplies
Forgetting — information degrades over many steps Every position connects directly to every other position; distance doesn't matter
Fixed-size memory bottleneck Keeps all positions' information available
Hard to make very deep Scales smoothly to 100+ layers

Speed was the decisive factor: a Transformer trains in hours what an LSTM needed weeks for, which meant researchers could try bigger models and more data — and that's what produced LLMs.

Not dead: RNN-like models are having a renaissance (Mamba, RWKV, state-space models) because they run inference in constant memory, unlike Transformers. Some current models are hybrids. But the LLM era was built on Transformers.

Practice (15 min, conceptual)

No code this time — instead, do this thinking exercise and write it in notes.md:

  1. You want to summarise a 10,000-word document. Explain in 3 sentences why an LSTM would struggle.
  2. Attention lets a model "look back at everything". What's the cost of that? (Hint: if there are 10,000 words and each looks at all 10,000... how many comparisons?) You've just derived the central limitation of Part 6.

Check yourself

  1. What is a hidden state?
  2. What problem does the LSTM's cell state solve, and how?
  3. What was the seq2seq bottleneck, and how did attention fix it?
  4. Give two reasons Transformers replaced RNNs.

Further reading

  • Article (classic, essential): "Understanding LSTM Networks" — Christopher Olah, colah.github.io. The single best explanation of LSTMs ever written, with beautiful diagrams. ~15 minutes.
  • Article: "The Unreasonable Effectiveness of Recurrent Neural Networks" — Karpathy (2015). Fun, with character-level RNNs generating Shakespeare and Linux source code.
  • Article: "Visualizing A Neural Machine Translation Model (Mechanics of Seq2seq Models with Attention)" — Jay Alammar. The perfect bridge into Part 6.
  • Video: StatQuest, "Long Short-Term Memory (LSTM), Clearly Explained".

End of Part 4 — Milestone check

  • I can explain why XOR needs a hidden layer
  • I can explain what an activation function is for
  • I can explain backprop with the restaurant/blame analogy
  • I know what vanishing gradients are and two fixes
  • I can explain why depth helps
  • I can explain why RNNs lost to Transformers

Suggested break point. You've covered 60 years of AI. Take a day. Then Part 5, which is short and fun.


Generated from real-life-ai-example-v1.md · 8 Oct 2026 16:36