A real LLM learns from billions of sentences. Ours learns from one, so that every number in every step fits on the screen. The goal: after training, if we give it the word the, it should write the rest of the sentence by itself.
Home › Learn › Small model from scratch › 14 steps
Building a language model from one sentence
The whole training text is the cat sat on the mat and the cat was happy.. Fourteen steps, one at a time; use the buttons or the arrow keys. The numbers come from a real run of one_sentence_llm.py. Overview and code.
Step 1 of 14
The corpus: one sentence
Step 2 of 14
Letters become numbers (bytes)
A computer stores every character as a number from 0 to 255, called a byte. t is 116, h is 104, e is 101, and the space (shown as ␣) is 32. This table is the same on every computer. It's not something we learn.
So the sentence is now 45 numbers. We could stop here and give the model these 45 numbers. The next steps make the list shorter, so the model has fewer, more meaningful pieces to deal with.
Step 3 of 14
Cut into words
Before any gluing, we cut the sentence into words. The space stays attached to the front of the following word, so ␣cat means "the word cat, after a space". The full stop is its own piece.
Why cut first? In the next step we glue pieces together, and we only ever glue inside a word. That stops us making junk pieces like "t␣s" from the end of one word and the start of the next.
Step 4 of 14
Count pairs, glue the winner (rule 1)
Look inside every word and count each pair of neighbouring characters. a + t appears 4 times: in cat, sat, mat, cat. No other pair appears more often, so it wins.
The winner is glued into a new piece, "at", and given the next free number: 256 (0–255 are already taken by the bytes). This is written down as rule 1. Everywhere "a" is followed by "t", the two are now one piece.
Why 256? The code book is a numbered list. Before any rules, it already has 256 entries, numbered 0 to 255: one for each possible byte (97 is "a", 116 is "t"). The new piece "at" needs a number of its own that isn't already used. The first unused number is 256. The next rule's piece will get 257, then 258, and so on.
So ␣cat, which was 4 numbers [32, 99, 97, 116], becomes 3 numbers [32, 99, 256]. The two numbers for "a" and "t" were replaced by the single number 256.
Step 5 of 14
Repeat: 12 rules
After rule 1 we count pairs again. Now "at" counts as one piece, so new pairs appear, like ␣c + at. Each round glues one new winner. We stopped after 12 rules.
Notice rules 5, 8 and 12: they glue a piece that was itself glued earlier (␣c + at → ␣cat). That's how whole words get built from pairs. From rule 7 on, every remaining pair appears only once, so it's a tie. Ties go to the pair that comes first in the sentence. We stopped at 12 rules before reaching "and", "was" and "happy", so those stay as single letters.
The list of 12 rules, in order, is the tokenizer. Our code book now has 256 + 12 = 268 pieces.
Step 6 of 14
The sentence as brick numbers
Apply the 12 rules, in order, to the sentence. It becomes 23 pieces. Each piece is replaced by its number in the code book: the → 258, ␣cat → 260, ␣sat → 263.
From here on, the model never sees letters again. Everything below works only with these 23 numbers. Turning numbers back into text (decoding) just looks each number up in the code book.
Step 7 of 14
Turn the sentence into lessons
There is no separate "answer key". The sentence is its own answer key. At each position, the model sees all the pieces so far (grey) and must guess the next one (dark).
23 pieces give 22 lessons. In a real run, a chunk of 256 pieces gives 255 lessons at once, and there are millions of chunks.
Step 8 of 14
Each brick becomes 32 numbers (embedding)
A single number like 260 says nothing about what "cat" means. So the model keeps a table with one row of 32 numbers for each of the 268 bricks. Brick 260 looks up row 260.
It also has a row for each position (1st piece, 2nd piece, …), because "cat" at position 1 and "cat" at position 11 should be treated differently. The two rows are added together.
These numbers start random and small. Training will adjust them. Nobody writes them by hand.
Step 9 of 14
Look back at earlier pieces (attention)
Here's the hard part of our sentence: "␣cat" appears twice. The first time, the next piece is ␣sat. The second time, the next piece is ␣ (the start of " was"). Looking at "cat" alone can't tell them apart.
So each piece looks back at all the pieces before it and mixes in information from them. The bars show how much the second ␣cat looks at each earlier piece, after training. Real values, averaged over the model's 2 "heads". It also knows its own position (11, not 1). Together that's enough to tell the two cats apart.
After looking back, a small feed-forward layer works on each piece's 32 numbers on its own. Attention plus feed-forward is one block. Our model has 1 block; the planned larger model will have 6.
Step 10 of 14
Scores become chances
At the end, the model produces 268 scores, one for every brick in the code book. A standard formula (softmax) turns scores into chances that add up to 100%.
Before any training, the numbers are random, so the chances are almost even: about 0.4% each. The right answer after "the cat" is ␣sat, and the model gives it only 0.4%. It has no idea yet.
Step 11 of 14
Measure the mistake (loss)
To learn, the model needs one number that says how wrong it was. That's the loss: the negative log of the chance it gave to the right answer.
Before training it gave ␣sat 0.4% → loss 5.53. After training it gives 99.2% → loss 0.01. The loss we watch during training is this number averaged over all 22 lessons.
Check: a completely random model gives every brick about 1/268, so its loss is ln(268) = 5.59. Our model's first loss was 5.60, which is correct. If it hadn't been close, something would be broken.
Step 12 of 14
Nudge every number, repeat 300 times (training)
Training is a loop: run the 22 lessons, compute the loss, then nudge all 22,080 numbers a tiny bit in the direction that makes the loss smaller. PyTorch works out the direction for each number automatically (backpropagation), and the optimizer (AdamW) makes the nudge.
We repeated this 300 times; it took about 2 seconds. Under the chart is what the model writes from "the" at four moments: random bytes at step 0, nearly right at step 25, exactly right by step 50.
Step 13 of 14
Write: pick, append, repeat (generation)
Writing is the same model used in a loop. Give it "the". It produces chances for the next brick, we pick the highest (␣cat), add it to the text, and run the model again on "the cat". Repeat until the sentence is complete.
Each row is one round: what the model has so far, the brick it picked, and how sure it was. One round = one brick. This loop is the engine from the full plan.
Step 14 of 14
Test it: memorised, not learned
With a prompt from its sentence, it's perfect. With "the dog", which it never saw, it writes nonsense made of its one sentence's pieces. It has memorised one sentence, not learned English.
| This toy | Next model (planned, not built) | |
|---|---|---|
| Text | 1 sentence | ~2 million short stories |
| Glue rules | 12 | 3,835 |
| Numbers in the model | 22,080 | ~12 million |
| Blocks | 1 | 6 |
| Training | 300 steps, 2 seconds | one night |
| Held-out text to check real learning | none | 2% |
A larger model goes through the same steps. Mostly the sizes change.