LLMs from Scratch, Part 1: The Next-Word Machine

by Jason Kuma Updated 0 views

A computer can't read. The chips inside a computer only work with numbers: they add numbers, multiply numbers, compare numbers and move numbers around. Not one chip knows what a word is.

Yet you can type a question to a chatbot and get back a paragraph that sounds like a person wrote it. The programs behind chatbots are called large language models, or LLMs.

Make your guess. When a chatbot writes a reply, what is the LLM inside doing?

  1. Finding an answer that someone already wrote, and copying the answer
  2. Searching the internet and pasting what the search finds
  3. Guessing the next word, adding the word, then guessing again, over and over
Show answer

C. Guessing the next word, adding the word, then guessing again, over and over

An LLM writes its reply one small piece at a time, and every piece is a guess. Some chatbots can also search the web, but the writing itself still comes out one guessed piece at a time. On this page you will build a tiny guessing machine yourself.

A guess needs something to guess from, and a computer only has numbers. So the first problem is this: how do you hand a word to a machine that only understands numbers?

Words become numbers

Everything inside a computer is stored as numbers. A photo is a grid of numbers, one for the colour of each tiny dot. Text is numbers too.

Our machine needs only a simple rule: give every word its own number. The first word gets 1, the next new word gets 2, and so on. A word that shows up again keeps the number it already has.

Drawing
Six words become six numbers. Both copies of “the” get the same number, 1.

Using the same numbers, what does 1 5 3 say?

  1. the mat sat
  2. the cat sat
  3. on the mat
Show answer

A. the mat sat

1 is “the”, 5 is “mat” and 3 is “sat”. The computer can't read the words, but the computer can check whether two numbers match. Matching numbers mean matching words, and that one skill is enough for the machine on this page.

Real LLMs give numbers to pieces of words, not just whole words, so a model can handle any word, even a new one. The pieces are called tokens. On this page, every token is a whole word.

Now a sentence is a row of numbers. But a row of numbers just sits there. What can a machine do with the row?

Guess the next word

Here is the whole job. Look at the words so far and guess the word that comes next. Add that word to the end. Then look again and guess again.

Drawing
Each guess joins the end of the text, and the longer text is the starting point for the next guess.

Real models do something much bigger inside each guess, but the game is the same: predict the next piece of text.

You play this game all day without noticing. Read “Once upon a”, and “time” jumps into your head. Can a machine that can't read play the game as well as you?

Beat the machine

Tap the word you think comes next, then see which word the machine picked and which word was real.

You against the machine

The machine read one short story about a cat and a dog, and nothing else. The machine never saw the six sentences in the game. Even so, the machine got half of them right.

Look at the blue word in each sentence: the last word before the blank. Sentences 1 and 4 both end in “a”, and the machine answered “time” both times. How can one word give the machine a guess?

A machine that counts

The machine counted. Word by word through the story, the machine noted which word came right after each word. Then the machine added up the tallies.

Drawing
In the story, 27 words come right after “the”. “Cat” comes 8 times, “dog” 7 times, “ball” twice, and ten other words once each.

To guess, the machine finds the last word of your sentence in the tally and picks the biggest count. After “the”, the biggest count is “cat”. That count is why the machine wrote “The cat sat on the cat.”

A tiny story: “The dog ran. The cat sat. The dog sat.” After “the”, which word does the counting machine guess?

  1. dog
  2. cat
  3. sat
  4. ran
Show answer

A. dog

“Dog” comes after “the” twice, and “cat” comes after “the” once. The biggest count wins, so the guess is “dog”. The machine never asks what a dog is.

Tap any word in the story and the bars count what came right after it. Then press Edit the text and type your own: a song, a recipe, a text message.

The counting table

Counting neighbours like this has a name: a bigram model. “Bi” means two, and a bigram is a pair of neighbouring words.

The whole machine is this table of counts. So far the table guesses one word at a time. Can the same table write whole sentences?

Let it write

Writing is guessing over and over. Start with “the” and look up the words that came after “the”. Pick one. The new word is now the last word, so look up the words after the new word, and pick again.

But which word should the machine pick each time? The simplest rule is to always take the biggest count.

If the machine always takes the word with the biggest count, what does the machine write?

  1. A different sentence every time
  2. The same few words over and over, forever
  3. One good sentence, and then the machine stops
Show answer

B. The same few words over and over, forever

Each word always leads to the same next word. After “the” comes “cat”, then “sat”, then “on”, and after “on” comes “the” again. The machine is back where it started, so the words repeat forever: “the cat sat on the cat sat on the cat…”

The fix is to roll dice. Each word gets a chance that matches its count. “Cat” came after “the” 8 times out of 27, so the dice pick “cat” about 8 times out of every 27 rolls:

\[\begin{aligned} \text{chance} &= \frac{\text{the word's count}}{\text{all the counts}} \\[6pt] \text{chance of “cat”} &= \frac{8}{27} \approx 30\% \end{aligned}\]

Press Next word a few times and watch bigger blocks catch the marker more often. Then switch to Always the top word and watch for a loop.

The next-word machine

With the dice, the machine writes sentences like “The dog sat on the cat ate the fish.” Every pair of neighbouring words makes sense, and the whole sentence almost does. The machine has no idea what a cat is. The machine only knows which word tends to come next.

Many chatbots also pick with a bit of chance, which is one reason the same question can get a different answer each time.

Counting pairs gets surprisingly far. So where does counting pairs break?

One word of memory

Read a sentence the way the machine reads one. Cover every word except the last, and guess.

Drawing
You read the whole sentence. The machine sees only “speak”.

The machine sees only “speak”. What would the machine need to know to guess “French”?

  1. Which word most often comes after “speak”
  2. That the sentence mentions “France”
  3. How many words the sentence has
Show answer

B. That the sentence mentions “France”

The clue is “France”, four words before the blank. In a big pile of text, “speak” is followed by many words: “up”, “softly”, “English”, “to”. The biggest count can't know which one fits this sentence.

Why not count longer runs, like the last three words? Longer runs do help, for a while. But the number of runs to count explodes. Suppose the machine knows 50,000 words:

Looks backRuns to count
1 word50,000
2 words2.5 billion
3 words125 trillion

No pile of text is big enough to show the machine every run, so most of the table would stay empty. Real LLMs can look back over thousands of words at once, and the newest ones much further. No table of counts could do that.

A table of counts can't fix that blind spot. So how could a machine use the whole sentence?

Where this series goes
  1. The Next-Word Machine. Words become numbers, and counting neighbours writes almost-sentences. (This page.)
  2. Too Many to Count. Why a table of counts can't remember more, and a machine that learns by nudging its numbers instead.
  3. Words as Points. Each word gets a list of numbers, so words with similar meanings land close together.
  4. Paying Attention. How each word looks back over the whole sentence and picks out the words that matter.
  5. The Transformer. Stacking attention, layer on layer, into the design behind modern LLMs.
  6. Reading Everything. Training on huge piles of text, one next-word guess at a time.
  7. From Autocomplete to Assistant. How a next-word machine learns to answer questions and hold a conversation.

Ask Phy

  • How do real LLMs split text into tokens? Show me how a long word gets split up.
  • Here is a short text. Build its bigram table and write me three sentences with it.
  • Why does picking with a bit of chance make a chatbot's answers vary?