What are embeddings? Meaning as a list of numbers
Embeddings turn words and passages into lists of numbers, so that similar meanings end up close together. How closeness is measured, and where it goes wrong.
2 min readPart 7 of 21
On this page
Computers calculate with numbers, not with meaning. An embedding is how meaning becomes numbers: a list of them, called a vector, for a word, a sentence or an image. The numbers are chosen so that things with similar meanings get similar lists. Search for "laptop won't turn on" and find a page titled "computer doesn't start", and an embedding is probably at work.
Closeness you can calculate
Think of each vector as a point. With two numbers it's a point on a sheet of paper, and similar things are in a similar direction from the center. The usual measure is cosine similarity: 1 means the same direction, 0 means unrelated directions, −1 means opposite.
You can repeat the calculation for cat and dog:
python3 -c "from math import hypota, b = (0.9, 0.8), (0.8, 0.9)print(round((a[0]*b[0] + a[1]*b[1]) / (hypot(*a) * hypot(*b)), 3))"0.993Real ones are much longer. OpenAI's embedding models return 1,536 numbers for the small model and 3,072 for the large one by default. The vectors inside GPT-3 have 12,288. A single number usually has no meaning you could name; the meaning is in the whole pattern.
Where the numbers come from
They're learned. The principle is older than computers: you can tell what a word means from the words around it. In 2013 Google researchers published word2vec, which trained vectors on a simple task, predicting words from their neighbors, across 1.6 billion words in less than a day. Words used in similar contexts ended up with similar vectors, without anyone defining a single one.
The paper's best-known observation is about directions: take the vector for King, subtract Man, add Woman, and the closest word to the result is Queen. It's a striking result, with a caveat often left out: the usual way of testing it excludes the three input words from the answer. Vectors capture regularities in how words are used. They aren't a dictionary or a logic.
They also capture whatever else is in the text. If a job appears mostly alongside one gender in the training data, the vectors will reflect that.
Two kinds you'll meet
Inside a language model
One vector per token
Changes at every layer, with the context
Never leaves the model
From an embedding model
One vector per sentence or passage
Fixed once computed
Stored and compared: search, recommendations, RAG
In word2vec, "bank" has one vector, for the river and for the money. In a language model, "bank" starts from a fixed vector and is then changed by attention as it passes through the layers, so in different sentences it ends up different.
Embedding models use the same machinery to produce one vector for a whole passage. That's what most applications mean by embeddings.
Searching by meaning
- 1SplitCut documents into passages
- 2EmbedTurn each passage into a vector and store it
- 3Embed the questionUse the same model
- 4CompareReturn the passages whose vectors are closest
Keyword search needs the same words. Vector search needs the same meaning, so it copes with synonyms and even with a question in one language and a document in another. It's the retrieval step in many RAG systems.
Where it goes wrong:
- Exact strings: part numbers, error codes and names may be similar to many others. Keyword search is better here, and good systems use both.
- Negation: "refunds are allowed" and "refunds aren't allowed" are about the same thing, so their vectors can be close.
- Related isn't correct: the nearest passage is the most similar one, even if nothing in the collection answers the question.
- Models don't mix: vectors from different embedding models can't be compared. Change the model and everything has to be embedded again.
Vectors aren't anonymous either. They're computed from the text and carry information about it, so treat stored vectors as being as sensitive as the documents.