Embeddings
An embedding turns a word, sentence or image into a list of numbers, so that similar things get similar numbers. Explore a map of words and measure meaning as distance.
9 minFree
Background: the idea in plain words
A computer can't compare "dosa" and "idli" as words. To a program they're just different strings (pieces of text). It has no idea that both are things you eat.
An embedding fixes that: it turns each item into a vector (a list of numbers) so that items with similar meanings get similar numbers. Each vector is a point in an embedding space (think of a map), and the distance between two points tells you how related they are.
Nobody types these numbers in by hand. A model learns them during training. Words that are used in similar contexts end up close together.
A tiny example: on the map below, "idli" sits close to "dosa" and far from "Mumbai". So a program can now work out that idli and dosa are related, just by comparing numbers.
You've seen embeddings already: inside an LLM, every token becomes an embedding before the transformer runs (see What is an LLM?). The same idea powers semantic search (search by meaning, not by exact words), recommendations and the "retrieval" step that grounds an agent in your documents.
The usual way to compare two embeddings is cosine similarity: how closely the two vectors point in the same direction. Picture two arrows starting from the same spot. Close to 1 means very similar. Close to 0 means unrelated.
Press Play to walk through the map. Afterwards, tap any word to see its nearest neighbours.
What to notice
- Meaning becomes geometry. Foods cluster together, cities cluster together, weather words cluster together. Nobody drew those groups: they fall out of the numbers.
- There's structure inside a cluster too. Chennai sits nearer Madurai than Mumbai, and idli nearer dosa than vada pav, because the vectors also carry a sense of region.
- Directions can mean something. Subtracting Chennai and adding Mumbai moves "idli" towards Mumbai's food. Real embedding models show patterns like this, though they're never this neat.
- Real dimensions have no names. The toy example labels its 8 dimensions so you can read them. Real word embeddings typically have 256, 512 or 1024 dimensions, learned during training, and single dimensions rarely mean anything a person could name.
- Similarity is a choice of maths. Google's guide describes three common measures: Euclidean distance, cosine and dot product. Cosine ignores the length of the vectors; dot product doesn't. When vectors are normalised to length 1, all three rank neighbours the same way.
Keyword search
Matches the exact letters. A search for "monsoon" misses a document that only says "rain".
Embedding search
Matches meaning. "monsoon" and "rain" have nearby embeddings, so the document is found.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Embedding | A list of numbers that stands for a word, sentence or image, so that similar things get similar numbers |
| Vector | A list of numbers. You can picture it as a point, or as an arrow |
| Embedding space | The "map" where every embedding is a point |
| Dimension | One of the numbers in a vector. Real embeddings have hundreds of them |
| Cosine similarity | A score for how closely two vectors point the same way. Close to 1 means very similar |
| Semantic search | Search that matches meaning instead of exact words |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.