The Math Behind the Machine/ Unit 17 · Machines with Memory Checks 0/3 Sign in
Unit 17 of 20 · by Prof. Saurabh

Machines with Memory

You read a sentence one word at a time, and you remember what came before. A network can do the same. It keeps a small running note — a list of numbers — and updates it after every word, using the same weights each time. With that one trick a machine can even learn to talk, one character at a time. Training sends the blame back through every word, and the blame gets multiplied by the same matrix again and again — so its eigenvalues (Unit 4) decide whether memory fades away or blows up. Then the fixes: gates and an express lane for memory, readers that go both ways, and a machine that translates a whole sentence through one summary — the bottleneck that sets up Unit 18.

≈ 140 min read + play 17 interactive widgets · 4 in 3D · a tiny language model that talks 37 inline checks 🧾 23 proofs, folded away — open "if you want the algebra" when you are ready ✍ 16 solved practice problems

← Unit 16 · Words as Vectors

memory goes right · blame comes back · drag to orbit
1

Why order matters

"Dog bites man" and "Man bites dog" use the same three words. How can a machine that reads them tell which story it is?

Imagine this

Two newspaper headlines. "Dog bites man." Nobody cares. "Man bites dog." Front page!

Both use the same three words. Only the order is different — and the order is the whole story. A machine that reads must notice the order.

In Unit 16 every word became a list of numbers. So how do we turn a whole sentence into numbers? Here are three ideas. The first two fail, and how they fail tells us what we need.

Idea 1 · Throw the words in a bag. Add up the word vectors, or just count the words. People call this a bag of words. It is quick. But "dog bites man" and "man bites dog" have exactly the same bag: one "dog", one "bites", one "man". The sums are the same. The order is lost.

Idea 2 · Look at the last few words. Keep a small window, say the last 3 words. This is what the neural language model of Unit 16 did: it saw only a fixed window of the words just before. A window keeps local order, but it forgets everything older. Take the announcement "The train to Delhi from platform four is late". When "late" arrives, a 3-word window holds only "four is late". What is late? The word "train" has already dropped out.

Idea 3 · Read one word at a time and keep a running note. Think of a shopkeeper with a notebook. After each customer, he updates one line. But a plain running total does not care about order either: 5 + 3 is the same as 3 + 5. So we add one twist. Before writing in the new word, we first change the old note a little — turn it, shrink it — with a matrix. Then an old word has been turned many times and a new word only once. The order leaves its mark.

A tiny example. Give each word a toy vector (hand-made, not trained): dog=(1,0)\text{dog}=(1,0), man=(0,1)\text{man}=(0,1), bites=(1,1)\text{bites}=(1,1). The note starts at (0,0)(0,0). At each word we do two things: turn the note a quarter turn with W=(0−110)W=\begin{pmatrix}0&-1\\ 1&0\end{pmatrix}, then add the word. A quarter turn sends (a,b)(a,b) to (−b,a)(-b,a).

  • dog bites man: after "dog" the note is (1,0)(1,0). Turn it: (0,1)(0,1); add "bites": (1,2)(1,2). Turn it: (−2,1)(-2,1); add "man": (−2,2)(-2,2).
  • man bites dog: after "man", (0,1)(0,1). Turn: (−1,0)(-1,0); add "bites": (0,1)(0,1). Turn: (−1,0)(-1,0); add "dog": (0,0)(0,0).

Two different notes: (−2,2)(-2,2) and (0,0)(0,0). Without the turn, both would end at the plain sum (2,2)(2,2). The turn is what makes the note remember order.

Three ways to read a sentenceA bag of words, a sliding window, and a running note. Word vectors here are toy numbers made by hand.

Try: In running note, set the turn to 0° and watch both sentences land on the same point, (2, 2) — that is the bag of words again. Turn it back up and they split apart. Now try 180°: they land together again, at (0, 0). Press ↻ another order for B to try the other orders of the same three words: at 90° none of them lands on A's note. Then open window and slide the window size down to 3: "train" falls out before "late" arrives.

90°
1
Why it works

Unfold the rule over three words. The note ends as W2 word1+W word2+word3W^2\,\text{word}_1+W\,\text{word}_2+\text{word}_3. Every position wears its own stamp: the last word goes in as it is, the one before is turned once, the first is turned twice. Swap two words and they trade stamps, so the note changes.

A plain sum gives every word the same stamp — no turn at all. That is exactly why it cannot see order.

The realization

new note=W (old note)+new word\begin{gathered}\text{new note}=\\ W\,(\text{old note})+\text{new word}\end{gathered}

A bag of words forgets order. A window forgets the past. A running note keeps both, if it changes the old note before adding the new word. Every word leaves its mark, and older marks have been changed more times.

The three readers side by side.

sees word order?remembers far back?size of the summary
bag of wordsnoyes — every word counts, but order is gonefixed
window of k wordsonly inside the windowno — only the last k wordsfixed
running noteyesyes, if old marks do not fade (§7)fixed

Rule of thumb. If the question is "which words appear?" (is this review about food or about service?), a bag is often enough. If the question is "who did what to whom?", you need a reader that keeps order — a running note.

Trap

A running total is still a bag. Adding numbers does not care about order, so "keep a sum of the words" loses order just as badly as counting them. The note only remembers order because the old note is changed (turned, shrunk) before each new word is added.

Pause & predict

With a plain bag of words (just count the words), which pair of sentences looks different?

Pause & predict

Use the same rule (quarter turn, then add) and the same toy vectors. What is the note after reading "bites dog" (just these two words)?

Pause & predict

Change the turn to a half turn, 180°, which sends (a,b)(a,b) to (−a,−b)(-a,-b). Can this reader still tell "dog bites man" from "man bites dog"?

The road ahead. The unit has five acts.

  1. Why a reader needs memory (§1–§3): order, the recurrent cell, and the same cell copied along time.
  2. A cell that talks (§4–§5): the many jobs one cell can do, and a small language model that learns to write, one character at a time.
  3. Blame through time (§6–§8): training sends blame back through every word; it is multiplied by the same matrix again and again, so eigenvalues decide whether memory fades or explodes; then first aid.
  4. Gates, and reading both ways (§9–§11): the LSTM's express lane, the GRU's blend dial, and readers that read in both directions or in floors.
  5. Translating through one summary (§12–§14): an encoder–decoder that turns English into Hindi, how it picks and grades its words, and the bottleneck that Unit 18 removes.

In one sentence: A sentence is an ordered list, so a reader must keep a running note and change the old note before adding each new word — a bag or a plain total would give "dog bites man" and "man bites dog" the same note.

Free preview · Unit 17 of 20

That was section 1. The rest of the unit opens when you unlock it.

13 more sections and the practice arena — 17 widgets, 37 checks and 16 solved problems in the whole unit (this preview had 1 widget and 3 checks).

Unlock Unit 17

  1. 2

    The recurrent cell: one small machine, used again and again

  2. 3

    Unrolling: the loop is a chain of twins

  3. 4

    Many jobs, one cell

  4. 5

    An RNN language model: learning to talk, then talking

  5. 6

    Backprop through time: blame walks back along the words

  6. 7

    Why memory fades or explodes

  7. 8

    First aid for explosions: clip the gradient, start at the right volume

  8. 9

    The LSTM: a memory with an express lane

  9. 10

    The GRU: a lighter cousin with one blend dial

  10. 11

    Reading both ways, and stacking floors

  11. 12

    Encoder–decoder: translate through one summary

  12. 13

    Choosing the words, and grading them: greedy, beam search, BLEU

  13. 14

    What to carry forward

  14. 15

    Practice arena — sixteen problems, solved in full

Unlock this unit for ₹299, or all seven paid units for ₹999 — one-time payment, full refund within 7 days. See pricing.