Lesson 4
How does a model know "cat" and "dog" are alike?
What an embedding is, where its numbers come from, and how a computer measures whether two words are close in meaning.
1Does a token's number tell the model what a word means?
No. A token id is only a label. It tells the model which token it has, but nothing about what the token means.
- 🏷️A token id works like a roll number in class. Roll number 23 tells the teacher whose name to call. It does not say if you are tall, or good at maths.
- ␣As in lesson 3, the mark ␣ means a space that is part of the token. ␣cat is a space, then cat, as in I fed my cat.
- 🔢Words with similar meanings do not get similar ids. A kitten is a baby cat, but the id of ␣kitten is far from the id of ␣cat. The id of ␣car is much nearer.
How far is each id from the id of ␣cat?
- ␣cat9059
- ␣kitten114502
- ␣car1669
Real ids from GPT-4o's tokenizer. ␣ marks a space that is part of the token. "Away" is the bigger id minus the smaller one.
So the id can't tell the model that cat and kitten are alike. The model needs a second thing for each token, one that carries meaning. To see how a model builds it, start with how you learned what a cat is.
2How did you learn what a cat is when you were a baby?
You learned from examples, not from rules. Someone pointed at an animal and told you whether it was a cat.
- 👶Nobody gave you a list of rules. No one said "a cat has whiskers and pointy ears, and it says meow" before you knew the word.
- 👉You saw examples, each with the right answer. "That's a cat." "No, that one is a dog."
- 🔍You found the clues yourself. A clue you learn to look for is called a feature. Whiskers.
- 🎚️A feature has a value for each thing you look at. A cat: whiskers yes. A spoon: whiskers no.
- ❌Some clues you found were wrong. Every cat you saw came in the morning, and the dog came at night. So "morning means cat" seemed true.
- 🗑️A wrong clue drops away when an example does not match it. One night you see a cat. Now the time of day no longer tells a cat from a dog.
- 💪A good clue stays, because it keeps matching. Cat after cat has whiskers, so you keep looking for whiskers.
- 🤖A model learns in a similar way. It gets examples with the right answers, finds its own features, and keeps the ones that keep matching. A model is shown thousands of photos, each marked "cat" or "not a cat". Nobody tells it to look for whiskers.
3What is an embedding?
An embedding is a list of numbers that a model keeps for every token.
The model works with this list, not with the token id.
- 📋Every token has its own list. ␣cat has one list, and ␣dog has another.
- 🎚️Think of each number as the value of one feature, like whiskers yes or no in section 2. If one number meant "is an animal", ␣cat would have a high value there, like 0.90, and ␣car a low one, like 0.05. These two numbers are made up.
- ➡️Another name for a list of numbers is a vector. "The vector for cat" and "the embedding of cat" mean the same list.
From a token to its list of numbers
- ␣cat9059
from I fed my cat
Made-up names, to help you read the table. Real lists have no names.
␣cat → 0.90, 0.80, 0.05, …
In I fed my cat, the word cat is the token ␣cat. Its id is 9059.
The model keeps a table. It has one row for every token id, and each row holds a list of numbers.
The model finds row 9059, the row for ␣cat (in green).
Out comes the list for ␣cat. This list is the embedding of ␣cat. The model works with this list, not with 9059.
The ids are real, from GPT-4o's tokenizer. ␣ marks a space that is part of the token. The column names (animal, pet, wheels) and every number in the table are made up, and only 3 numbers per row are shown. Green marks the row the model looks up.
Real lists differ from this made-up table in two ways:
- 📜Real lists are much longer, and every list in a model has the same length. In Phi-3-mini, a small model from Microsoft, every token's list has 3,072 numbers.
- ❔Real numbers have no names. Nobody chooses a feature like "is an animal". Training sets every number. If you look at one number in the list for cat, say the 500th, it is only a value, like 0.31 (made up). Nobody can say which idea it stands for.
4Why do cat and dog end up with similar lists?
Because people use cat and dog in the same kinds of sentences. Training then pushes their two lists to become alike.
- 🎲At the start of training, every list is random. ␣cat could start as 0.12, 0.67, 0.40 and ␣dog as 0.81, 0.05, 0.33. Nothing alike. (Made-up numbers.)
- 🎛️The numbers in the lists are dials, the parameters from lesson 2. The 3 numbers shown for ␣cat in the table are 3 dials.
- 🔁Training turns these dials as in lesson 2: guess the next word, check the real one, nudge the dials. The model reads My cat is and guesses the next word. The real next word is sleeping.
- 🔧The list of a token gets nudged only when that token was part of the guess. A dial that did not affect the guess does not move, as in lesson 2. In My cat is, the list for ␣cat was used, so it gets nudged. The list for ␣car was not used, so it stays the same.
- 👯Words used in the same places get nudged in similar ways. I fed my cat and I fed my dog. My cat is sleeping and My dog is sleeping.
- 🚗A word used in other places gets nudged in other ways. I parked the car. The car needs petrol.
- ✅After a lot of training, the lists for cat and dog usually end up alike, and the list for car ends up different. The made-up table in section 3: ␣cat 0.90, 0.80, 0.05; ␣dog 0.85, 0.90, 0.05; ␣car 0.05, 0.10, 0.95.
5How can a computer measure how close two meanings are?
One simple way: compare the two lists number by number, and add up the differences. A small total means the meanings are close, and a big total means they are far apart.
- 1️⃣Put the two lists side by side. ␣cat is 0.90, 0.80, 0.05. ␣kitten is 0.88, 0.85, 0.02.
- 2️⃣For each position, take the bigger number minus the smaller one. The first numbers: 0.90 − 0.88 = 0.02.
- 3️⃣Add up the differences. We call this total the gap. 0.02 + 0.05 + 0.03 = 0.10.
- 4️⃣A small gap means close in meaning. A big gap means far apart. The gap from ␣cat to ␣kitten is 0.10. The gap to ␣car is 2.45.
How far is each list from the list for ␣cat?
The lists are the made-up ones from section 3, 3 numbers each. The gap is the sum of the differences, worked out on this page. Adding up the differences is one real way to measure a gap. Many tools use a different formula, but it answers the same question: how alike are two lists?
- 🏷️The ids play no part in this. The id of ␣kitten (114502) is far from the id of ␣cat (9059), but its list has the smallest gap of the three.
6Can a model learn the wrong clue?
Yes. A model keeps any clue that keeps matching its training examples, even a clue that is wrong. In section 2, "morning means cat" stayed until a cat came at night.
A real case from 2018, with chest X-rays:
- 🩻Researchers trained a model to spot pneumonia, a lung illness, in chest X-rays from 3 hospitals. At one hospital, about 34 in 100 X-rays showed pneumonia. At the other two, about 1 in 100. The model learned to tell the hospitals apart. One clue was a small metal marker that shows in the corner of the X-ray.
- 📉The hospital was a wrong clue that kept matching. If the model spotted the hospital, it could often guess pneumonia right without looking at the lungs. On X-rays from a hospital it had not trained on, the model did worse in 3 of 5 tests.
Embeddings can pick up a wrong clue from text:
- 🧑⚕️Models often learn lists where doctor is closer to man, and nurse is closer to woman. If more texts say "the doctor said he was late" and "the man said he was late", then doctor and man are used in the same places. As in section 4, their lists get nudged in similar ways. The same happens for nurse and woman.
- 📚This is not a fact about the world. It is a pattern in the text the model read, unfair parts included. A woman can be a doctor, but the lists can still put doctor closer to man.
Each token gets only one list. ␣bank has one list, but a river bank and a money bank are different things. How does a model work out which bank you mean? Lesson 5 starts there.
Where this comes from
- Token ids: run on 2026-10-31 with OpenAI's tiktoken 0.14.0, encoding o200k_base, which tiktoken maps to gpt-4o. github.com/openai/tiktoken. Each with its leading space, as in a sentence: ␣cat 9059 (in I fed my cat, which is I 40, ␣fed 28033, ␣my 922, ␣cat 9059; and in My cat is sleeping), ␣dog 6446, ␣car 1669 (in I parked the car), ␣kitten 114502 (in I have a pet kitten), ␣bank 6922 (in I sat on the river bank). Without the space they differ: cat is 8837, and kitten is two tokens, kit 11498 + ten 1305.
- Embeddings are lists of numbers, also called vectors; similar meaning sits close: How Large Language Models Work (Raff, Farris, Biderman; Manning 2025), ch.3, PDF p.57-58: "the lists of numbers used to represent tokens are referred to as embeddings ... As a shorthand, we call such arrays vectors. Each position in the vector is called a dimension." Hands-On Large Language Models (Alammar, Grootendorst; O'Reilly 2024), ch.1, PDF p.31: compressed to two dimensions, "words with similar meaning tend to be closer".
- Features don't have human names: Hands-On Large Language Models, ch.1, PDF p.30: "We may oversimplify by imagining that dimensions represent concepts (which they don't)"; "these properties are often quite obscure and seldom relate to a single entity or humanly identifiable concept."
- A table with one row per token, part of the model; starts random; size fixed: Hands-On Large Language Models, ch.2, PDF p.95: "a portion of the model is this embeddings matrix"; "these vectors are randomly initialized like the rest of the model's weights". PDF p.106: the matrix has "dimensions vocab_size x embedding_dimensions". ch.1, PDF p.30: "the size of embeddings is fixed". GPT-4o's list length is not published, so the page uses Phi-3-mini for real sizes.
- Phi-3-mini: 3,072 numbers per token:
"hidden_size": 3072in the model's config file, huggingface.co/microsoft/Phi-3-mini-4k-instruct. - Similar neighbours, similar lists; not guaranteed: Hands-On Large Language Models, ch.1, PDF p.29: "If the two words tend to have the same neighbors, their embeddings will be closer to one another". How Large Language Models Work, ch.3, PDF p.58: "we cannot guarantee that these semantic relationships will form during the training process. It just so happens that they often do." That is why the page says "usually".
- A dial that did not affect the guess does not move: lesson 2, section 3, and its sources.
- Measuring the gap: the sum of differences is a standard distance between two lists (also called the Manhattan or L1 distance). Many embedding tools use cosine similarity instead: Hands-On Large Language Models, ch.4. The gaps on this page (0.10, 0.15, 2.45) are worked out by the page script from the made-up lists, and were checked separately with a script.
- Pneumonia X-rays: 34 in 100 vs about 1 in 100, 22,050 of 22,062, metal marker, worse at a new hospital: Zech et al., "Variable generalization performance of a deep learning model to detect pneumonia in chest radiographs: A cross-sectional study", PLOS Medicine, 6 November 2018. Pneumonia in 34.2% of Mount Sinai images, 1.2% at NIH and 1.0% at Indiana; hospital system identified for "99.95% NIH (22,050/22,062)"; Fig 2: "the CNN has learned to detect a metal token that radiology technicians place on the patient in the corner of the image field of view ... When these strong features are correlated with disease prevalence, models can leverage them to indirectly predict disease"; better internal than external performance "in 3 out of 5 natural comparisons". journals.plos.org/plosmedicine/article?id=10.1371/journal.pmed.1002683
- Doctor closer to man, nurse closer to woman (the book says male and female): How Large Language Models Work, ch.3, PDF p.58: "models will often determine that doctor is more similar to male and nurse is more similar to female ... The relationships are thus not a discovered truth of the world but a reflection of the data". The same bias shows up in generated text, ch.9, PDF p.179.
- One list per token, so "bank" has one list: Hands-On Large Language Models, ch.1, PDF p.32-35 (lesson 5 starts from it).
- Made up on purpose: the column names and numbers in the table (section 3), the random starting lists (section 4), the "500th number" value 0.31, and the gaps worked out from them (section 5). Each is labelled where it appears. All web pages accessed 2026-10-31.