Lesson 07 of 10 /Builds on 1

What are embeddings? How AI turns words into numbers that carry meaning

Before a model can do anything with a word, it swaps the word for a long list of numbers. Words with similar meaning get similar numbers. That one idea is how AI measures meaning, and it is what AI search and RAG are built on.

By the end you can
  • Say what an embedding is: a list of numbers for a piece of text, where close numbers mean close meaning
  • Explain why king minus man plus woman lands near queen, and name five ways the trick breaks
  • Read a cosine similarity score and say what 1, 0.8 and 0.3 mean
  • Describe how AI search and RAG use embeddings to find the right text for a question
In this lesson
  1. 1What is an embedding?
  2. LabExplore a real map of meaning
  3. 3Why does king minus man plus woman land near queen?
  4. 4Where the trick breaks
  5. 5What is cosine similarity?
  6. QuizHow embeddings power AI search and RAG
Part 1

What is an embedding?

An embedding is a list of numbers that stands for a piece of text. The rule that makes it useful is simple: text with similar meaning gets similar numbers. Dog and puppy end up with lists that look alike. Dog and democracy do not.

Every language model starts here. In our first lesson we saw that text is cut into tokens. The next step swaps each token for its embedding. GPT-2, the model behind all our labs, has a table with one row for each of its 50,257 tokens, and each row holds 768 numbers. Everything the model does after that, it does with numbers, never with letters.

Nobody wrote those numbers by hand. They start random. During training, the model guesses the next word billions of times, and every miss nudges the numbers a little. Words that show up in the same kinds of sentences get nudged the same way, so they drift together. "Pizza" and "pasta" both come after "we had" and before "for dinner", so they end up close. That is where an embedding gets its meaning: from the company a word keeps.

Lab

Explore a real map of meaning

These are GPT-2's real numbers for about 100 everyday words. Tap a word to see its 768 numbers as a strip, and the eight words whose numbers sit closest out of 30,390. Then compare two words, and try the word sums.

Lab 01Words as numbersReady

Real data from GPT-2's input embeddings, not a simulation. The map squashes 768 numbers into 2 so it fits on a screen, so distances on the map are rough. Every score in the lists uses all 768 numbers.

Part 3

Why does king minus man plus woman land near queen?

In 2013 researchers led by Tomas Mikolov showed a trick that made embeddings famous. Take the numbers for king, subtract the numbers for man, add the numbers for woman. The result sits closest to queen. Mikolov's 2013 paper at Google added a test set of 8,869 questions about meaning, like countries and capitals, and 10,675 about grammar, like plurals and past tense.

Here is why it works. Think of each word as a spot on a map with 768 directions instead of two. Going from man to king is one step in a certain direction. Going from woman to queen points almost the same way: in GPT-2 the two steps line up with a score of 0.65 out of 1 (we explain this score below), while two unrelated steps, dog to pizza and man to king, score 0.10. So "start at woman and take the king step" lands near queen. Grammar works the same way. The plural step is a direction too, which is why cats minus cat plus dog gives dogs in the lab.

Nobody taught the model what royalty or plurals are. Those directions fell out of guessing the next word on a lot of text.

Two panels of arrows. Left: man to woman, uncle to aunt and king to queen all point the same way. Right: king to kings and queen to queens point the same way.
The 2013 picture of word steps: the man-to-woman step matches king-to-queen (left), and the plural step matches too (right). /Mikolov, Yih and Zweig, NAACL 2013, Figure 2
Part 4

Where the trick breaks

The trick is real, but the famous version hides a few things. You can see each one in the lab.

The answer is usually the word you started with. In GPT-2, king minus man plus woman sits closer to king (0.74) than to queen (0.69). The sum only moves part of the way. The 2013 word2vec code left the three input words out of the race, and so does the popular gensim library. A 2020 study by Nissim, van Noord and van der Goot measured what happens when you let them back in: on the standard test, accuracy fell from 74% to 21%.

Some sums slip or fail. Paris minus France plus Italy gives Milan before Rome: the sum lands in "big Italian city" and does not know which one is the capital. Warm minus hot plus cold should give cool. GPT-2 gives colder, warmer and warmth, and cool only comes 28th. Order matters too: better minus good plus bad gives worse, but good minus better plus bad asks the wrong question and gives Good, Bad and terrible.

One word gets one list of numbers. "Bank" has the same numbers in "river bank" and "bank account". In GPT-2, river and money sit the same distance from bank, 0.40 and 0.39. Inside a full model, later layers sort this out by mixing in the words around it, which is what attention does; the starting numbers alone cannot.

Opposites look alike. Hot and cold score 0.57, about the same as dog and cat. They appear in the same kinds of sentences, so their numbers match, even though their meanings are reversed.

The numbers carry our biases. Doctor minus man plus woman gives physician and doctors first, then nurse. In 2016 Bolukbasi and colleagues found the same kind of pattern in word embeddings trained on Google News. The 2020 study above warns that word sums overstate this, partly because of the left-out input words. The bias is real all the same: the numbers copy what is in the text they learned from.

Bar chart of the words closest to king minus man plus woman in GPT-2: king 0.74, queen 0.69, princess 0.59, Queen 0.58, kings 0.57, woman 0.52.
Let the input words into the race and king (0.74) beats queen (0.69). Measured on GPT-2, 30,390 words searched. /Sansser, from GPT-2 input embeddings
Part 5

What is cosine similarity?

To say how close two embeddings are, we use cosine similarity. Picture each list of numbers as an arrow pointing out from the centre. Cosine similarity asks one thing: how closely do the two arrows point the same way?

A score of 1 means the same direction. A score of 0 means a right angle: no link at all. A negative score means they point apart. In GPT-2's word table, near twins like dog and dogs score about 0.8, related words like king and queen score 0.66, and unrelated words sit near 0.3 (king and pizza: 0.27). Each model has its own range, so a score only means something next to other scores from the same model.

Cosine similarity looks only at direction. It ignores how long the arrows are. OpenAI's embeddings all have a length of 1, so for them cosine similarity and plain distance rank things the same way. Their guide recommends cosine similarity.

Bar chart of cosine similarity in GPT-2: dog and dogs 0.80, king and queen 0.66, hot and cold 0.57, dog and cat 0.55, bank and river 0.40, bank and money 0.39, king and pizza 0.27.
Near twins score highest, opposites like hot and cold score high too, and unrelated words sit near 0.3. /Sansser, from GPT-2 input embeddings
Check yourself

How embeddings power AI search and RAG

So far we have looked at single words. The embeddings behind AI search do the same for whole sentences and pages. A separate embedding model reads a passage and returns one list of numbers for all of it. OpenAI's text-embedding-3-small returns 1,536 numbers, and text-embedding-3-large returns 3,072. Models built for search are trained so that text on the same subject lands close together.

Search with embeddings works in three steps. First, every document is cut into passages and each passage gets its embedding, once, ahead of time. Second, when you ask a question, your question gets its embedding. Third, the system ranks the passages by cosine similarity to your question and takes the top few. Because it compares meaning and not spelling, "I forgot my login" can find a page titled "Reset your password" even though they share no words.

RAG, short for retrieval-augmented generation, adds one step. The top passages are pasted into the model's context window next to your question, and the model writes its answer from them. The idea was named in a 2020 paper by Lewis and colleagues, which searched a vector index of Wikipedia passages. Most "chat with your files" features work like this. When one gives a bad answer, the first thing to check is the search step: if the right passage never made it into the window, the model never saw it.

Diagram from the RAG paper: a query encoder turns the question into numbers, a search over a document index finds the top passages, and a generator writes the answer from them.
The original RAG design: turn the question into numbers, find the closest passages in an index, then write the answer from them. /Lewis et al., NeurIPS 2020, Figure 1

Q1What is an embedding?

Q2In GPT-2, which word is closest to king minus man plus woman if nothing is left out?

Q3Hot and cold score 0.57 in GPT-2. Why so high for opposites?

Q4In RAG, what are embeddings used for?

Remember

What to take with you

  1. An embedding is a list of numbers for a piece of text. Similar meaning gives similar numbers. GPT-2 uses 768 numbers for each of its 50,257 tokens.
  2. The numbers are learned, not written. Words that keep the same company in training text drift together.
  3. King minus man plus woman lands near queen because the man-to-king step and the woman-to-queen step point the same way. Most demos leave out that king itself is closer.
  4. Word sums slip. Opposites look alike. One word gets one set of numbers. And the numbers copy the biases in their training text.
  5. Cosine similarity measures how closely two embeddings point the same way. AI search and RAG rank passages by it and paste the best ones into the context window.
Questions

Questions people are asking

/01What are embeddings in AI?

Embeddings are lists of numbers that stand for words, sentences or whole documents. They are learned so that text with similar meaning gets similar numbers. That lets a computer measure meaning by comparing numbers.

/02Does king minus man plus woman equal queen?

Roughly. The result sits closest to queen once the three input words are left out of the search. If they are allowed, king can win: in GPT-2, king scores 0.74 and queen 0.69. Some sums also fail, like warm minus hot plus cold, which gives colder before cool.

/03What is cosine similarity?

A score for how closely two lists of numbers point the same way. 1 means the same direction, 0 means no link, and negative means opposite directions. It ignores length, which is why it is the usual way to compare embeddings.

/04How are embeddings used in RAG?

First, documents are cut into passages, and each passage gets an embedding ahead of time. Then your question gets an embedding too. The system finds the passages closest to it and pastes them into the model's context window, and the model answers from them.

/05What is the difference between word embeddings and sentence embeddings?

Word embeddings give each word or token one fixed list of numbers, so 'bank' looks the same in every sentence. Sentence embeddings come from a model that reads the whole text, so they capture what the passage means as a whole. Search and RAG use sentence embeddings.

/06How many numbers are in an embedding?

It depends on the model. GPT-2 uses 768 numbers per token. OpenAI's text-embedding-3-small uses 1,536 and text-embedding-3-large uses 3,072.

Sources 7

  1. Mikolov, Chen, Corrado and Dean, Efficient Estimation of Word Representations in Vector Space, 2013 arxiv.org/abs/1301.3781
  2. Mikolov, Yih and Zweig, Linguistic Regularities in Continuous Space Word Representations, NAACL 2013 aclanthology.org/N13-1090
  3. Nissim, van Noord and van der Goot, Fair is Better than Sensational: Man is to Doctor as Woman is to Doctor, 2019, Computational Linguistics 2020 arxiv.org/abs/1905.09866
  4. Bolukbasi et al., Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings, 2016 arxiv.org/abs/1607.06520
  5. Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, 2020 arxiv.org/abs/2005.11401
  6. OpenAI, Vector embeddings guide developers.openai.com/api/docs/guides/embeddings
  7. OpenAI, GPT-2 model files on Hugging Face (the source of our lab's numbers) huggingface.co/openai-community/gpt2
sansser weekly

Get the next lesson in your inbox.

New lessons and how-tos, the tools we tested and what we think, in one short email.

Free, every Friday. Unsubscribe in one click.