Grokking LLM Essentials
Vote

0% completed

​

Embeddings: How Numbers Hold Meaning

Part 1: A Token ID Is Only a Label

Part 2: A List of Numbers for Every Token

Part 3: A Map of Meaning

Part 4: Measuring How Similar Two Lists Are

Key Takeaways

Here is a short puzzle. Which two of these three words belong together?

soup, stew, car

Most people choose "soup" and "stew" immediately, because both are hot dishes served in a bowl. Now try the same puzzle in the form a model first receives it. With one widely used tokenizer, the three words inside a sentence become these token IDs:

29684, 145907, 1669

No pair is obvious now. By value, 29684 is nearer to 1669 than to 145907, and that would pair "soup" with "car". This lesson explains how a model gets from IDs like these to numbers that hold meaning. That change is step 2 of the five steps in The Life of a Prompt.

Part 1: A Token ID Is Only a Label

The Tokens lesson showed that a tokenizer gives every token in its vocabulary a token ID. The ID is the token's position in the vocabulary list, and a position carries no meaning.

A token ID works like a student ID number. The number identifies one student, and it gives the school no information about that student. In the same way, 29684 identifies one token, and it gives the model no information about soup.

The tokens near "soup" in the vocabulary show this. In the same tokenizer, token 29683 is "Anth", a piece of a longer word, and token 29686 is "Love". Neither one is related to soup. Arithmetic on token IDs would give results with no meaning, so the model needs a different kind of number.

Part 2: A List of Numbers for Every Token

The first step inside the model is to replace each token ID with a list of numbers. The model holds a large table with one row for every token in the vocabulary. Each row is a list of numbers, and all the rows have the same length. The ID 29684 selects row 29684, and the model works with that row from then on.

The list of numbers that stands for a token is called an embedding. A list of numbers is also called a vector, and both terms are common in writing about LLMs. A real embedding is a long list that holds hundreds or thousands of numbers.

Nobody writes these numbers by hand. They are part of the model's parameters, so the model learned them during training. Training tends to move the lists of two words toward each other when the words are used in the same way.

"Soup" and "stew" appear in very similar sentences, because both are cooked, tasted, and served. The model usually predicts better when their lists are similar, so training makes the two lists similar.

One detail often surprises beginners. A single number in a real embedding usually has no simple meaning, such as "this is a food". The meaning is held by the whole list.

Part 3: A Map of Meaning

A list of numbers can be drawn as a point. With two numbers per word, the first number sets how far to the right the point is, and the second number sets how high it is. The diagram below draws five words this way.

Five words drawn as points from two numbers each, with words of similar meaning close together
Five words drawn as points from two numbers each, with words of similar meaning close together

Look at the two pairs in the upper part of the map. "Soup" is next to "stew", and "salt" is next to "pepper", while "car" is far from all four food words.

This map is a simplification. The two numbers per word were chosen by hand for the picture, and a real embedding has too many numbers to draw. The idea is the same at any length: words with similar meanings get similar lists.

Part 4: Measuring How Similar Two Lists Are

A program cannot look at a map, so it needs a number. A score that measures how alike two embeddings are is called a similarity. A common method treats each list as a direction. On the map, that direction is the dashed line from the bottom left corner to the word's point.

This method is called cosine similarity, and it compares the two directions. A score near 1 means the two lists point in nearly the same direction. A lower score means the directions differ more. For the lists in this lesson, the score is always between 0 and 1.

The playground below computes this score for the five words. It uses the same two numbers per word as the map, so every score can be checked against the picture. These lists are a small stand-in written by hand, because a real embedding model cannot run here. Run it and check which word scores highest against "soup".

Python3
Python3

. . . .

The program prints this result:

Similarity to "soup" stew 0.99 salt 0.91 pepper 0.84 car 0.32 Most similar to "soup": stew

"Stew" scores 0.99, the two seasonings score lower, and "car" scores only 0.32. The order matches the map: the dashed line for "stew" is the closest to the line for "soup", and the line for "car" is the farthest from it. Now change WORD to "salt" and run the program again. "Pepper" moves to the top of the list with 0.99.

Key Takeaways

  • A token ID is only a label. Its value carries no information about the meaning of the token.
  • An embedding is a list of numbers that stands for a token. A real embedding holds hundreds or thousands of numbers, and the model learned them during training.
  • Similar meanings get similar lists. The list for "soup" is close to the list for "stew" and far from the list for "car".
  • Similarity is a score for two lists. Cosine similarity is near 1 when the two lists point in nearly the same direction.

An embedding has one limit. The table gives the word "it" the same list in every sentence, although "it" refers to a different thing each time. The next lesson, Attention: How Words Use Context, shows how the model uses the other words in a sentence to solve this problem.

Reading Progress

0%


Vote for new content

On This Page

Part 1: A Token ID Is Only a Label

Part 2: A List of Numbers for Every Token

Part 3: A Map of Meaning

Part 4: Measuring How Similar Two Lists Are

Key Takeaways