See how words become vectors and why similar meanings sit close together. Explore nearest-neighbour search, cosine similarity, word arithmetic and real MiniLM embeddings on a map.
1A word becomes a list of numbers
A tokenizer gives each word an id. The embedding table turns the id into a vector: a list of numbers the model can compute with. Real models use hundreds or thousands of numbers per token.
2Similar meanings sit close together
Training arranges the vectors so words used in similar ways end up near each other. Distance in this space stands in for difference in meaning.
3Closeness is measured by angle
Cosine similarity compares the directions of two vectors: 1 for the same direction, 0 for unrelated (at right angles). Search, recommendations and RAG all rank by it.
Start here
From token id to vector
A tokenizer turns text into ids. The model then looks each id up in its embedding table: id 10 reads row 10. To keep every number readable, this table has just 15 words and six dimensions that have names. Real models learn hundreds or thousands of dimensions, and none of them come with names: what each one means is spread across many numbers.
| id | word | royal | male | female | grown-up | animal | food |
|---|---|---|---|---|---|---|---|
| 0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 | 0.0 | |
| 1 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 0.0 | |
| 2 | 1.0 | 1.0 | 0.0 | 0.2 | 0.0 | 0.0 | |
| 3 | 1.0 | 0.0 | 1.0 | 0.2 | 0.0 | 0.0 | |
| 4 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 0.0 | |
| 5 | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 0.0 | |
| 6 | 0.0 | 1.0 | 0.0 | 0.2 | 0.0 | 0.0 | |
| 7 | 0.0 | 0.0 | 1.0 | 0.2 | 0.0 | 0.0 | |
| 8 | 0.0 | 0.1 | 0.1 | 0.8 | 1.0 | 0.0 | |
| 9 | 0.0 | 0.1 | 0.1 | 0.1 | 1.0 | 0.0 | |
| 10 | 0.0 | 0.1 | 0.1 | 0.8 | 0.9 | 0.0 | |
| 11 | 0.0 | 0.1 | 0.1 | 0.1 | 0.9 | 0.0 | |
| 12 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | |
| 13 | 0.0 | 0.0 | 0.0 | 0.1 | 0.0 | 0.9 | |
| 14 | 0.0 | 0.0 | 0.0 | 0.2 | 0.0 | 0.9 |
Click any word. Its row is its embedding:
cat = [0, 0.1, 0.1, 0.8, 0.9, 0]
Read across the row: “cat” is grown-up, an animal. Words that share features share numbers, and that is what makes them close.
Where ids come from: Tokenization →Watch it search
Find the nearest words
Pick a word and step through: it is compared with every other word, one at a time, and then they are ranked. This is exactly what a semantic search does, just with more words and longer vectors. The grid on the right shows every pair at once: same-group words form bright blocks.
- king?
- queen?
- prince?
- princess?
- man?
- woman?
- boy?
- girl?
- puppy?
- cat?
- kitten?
- apple?
- bread?
- pizza?
Press Step to look up “dog” and compare it with the other 14 words.
Cosine similarity of every pair
Brighter = more similar. Hover a cell for its value. The highlighted row is the word you're searching with.
The measuring stick
Cosine similarity: closeness by angle
Two vectors are similar when they point the same way. Cosine similarity is the cosine of the angle between them: 1 when they point the same way, 0 at right angles, −1 when opposite. Turn and stretch the arrows: the angle changes the cosine, the length doesn't. The dot product, by contrast, grows with length.
- Angle between
- 45°
- Cosine similarity
- 0.707
- Dot product
- 1.131
cos(a, b) = (a · b) ÷ (|a| × |b|)
Many embedding models return vectors of length 1. Then the dot product and the cosine are the same number, which is why vector databases often just use the dot product.
The famous trick
Arithmetic on meanings
If directions carry meaning, then differences between vectors can too. “king − man” removes “male” and “grown-up” and leaves “royal”; adding “woman” puts “female” and “grown-up” back. The nearest word to the result is the answer.
Nearest word: queen, similarity 1.000. Runner-up: princess (0.889).
This toy lands exactly on the answer because its vectors were built by hand. In real word embeddings (word2vec made this famous in 2013) the result only lands near the answer, the inputs themselves have to be excluded, and the trick works for some relationships far better than others.
Not a toy
Real embeddings, on a map
These are real vectors from all-MiniLM-L6-v2, a small sentence-embedding model: 384 numbers for each passage of a made-up shop handbook (the one on the RAG page) and for six questions. To draw 384 numbers on a flat page, they are projected onto the two directions where the passages differ most. That keeps only 37% of the spread, so trust the scores on the right more than the distances on the map.
“I unboxed my purchase but never used it. Can I send it back?”
Closest passages, by cosine similarity over all 384 numbers:
- Returns0.563You can return an unused purchase within 30 days of delivery. The item must be unused, but its packaging may have been opened.
- Returns0.542Start a return by emailing support with your order number. Keep your shipping receipt until the returned package reaches our warehouse.
- Refunds0.484Gift purchases can be refunded to the original buyer only. Our support team can look up a refund using the order number.
Passages about the same topic cluster together even though nobody told the model the topics. Real scores are rarely near 1: here, about 0.5 already means a strong match.
Where the numbers come from
How embeddings are learned
Start random
Every row of the table starts as random numbers. At first, “cat” is no closer to “dog” than to “pizza”.
Learn from context
Training nudges the numbers so the model gets better at its task, such as predicting a missing or next word. Words that appear in similar contexts get pushed in similar directions: “you shall know a word by the company it keeps”, as the linguist J. R. Firth put it in 1957.
Words, sentences, anything
Inside a language model the embedding table is trained along with everything else. Sentence-embedding models like MiniLM are trained further so that sentences with similar meaning land close together, which is what makes them good for search.
No magic, just arithmetic
The maths, step by step
How similar are “king” and “man”? Then, what is king − man + woman? The vectors use the toy dimensions [royal, male, female, grown-up, animal, food].
- 1
Write down the two vectors
Each word is a row of the embedding table.king = [1, 1, 0, 1, 0, 0]
man = [0, 1, 0, 1, 0, 0]
- 2
Dot product
Multiply matching positions and add them up. It's large when both vectors are big in the same places.king · man = Σ kingᵢ × manᵢ = 1×0 + 1×1 + 0×0 + 1×1 + 0×0 + 0×0 = 2.0000
- 3
Divide by the lengths
Dividing by both lengths removes the effect of size, leaving only direction: the cosine of the angle between them.|king| = √(Σ kingᵢ²) = √3 = 1.7321
|man| = √(Σ manᵢ²) = √2 = 1.4142
cos(king, man) = (king · man) ÷ (|king| × |man|) = 2 ÷ (1.7321 × 1.4142) = 0.8165
0.82: they share “male” and “grown-up”, and differ only in “royal”.
- 4
king − man + woman
Subtract and add position by position, then find the nearest word to the result.[1, 1, 0, 1, 0, 0] − [0, 1, 0, 1, 0, 0] + [0, 0, 1, 1, 0, 0]
= [1, 0, 1, 1, 0, 0]
queen = [1, 0, 1, 1, 0, 0]
cos(result, queen) = nearest of all words except king, man, woman = 1.0000
Exactly 1 here, because these vectors were built by hand. In real embeddings the result only lands near “queen”, and the trick works for some relationships much better than others.
In practice
Where you'll meet embeddings
Semantic search & RAG
Embed the question and the documents, return the closest. RAG →
Inside every Transformer
The first thing the model does with token ids. The Transformer →
Recommendations
Users and items as vectors: recommend what's close to what you liked.
Clustering & deduplication
Group similar texts, or spot near-duplicates by a very high score. K-Means →
How it works: Embeddings
Step by step
- Royalty
- People
- Animals
- Food
Press Start or Step: the step the animation is on lights up below.
- 1
Look up the vector
Turn the word (or sentence) into its vector: a table row for a word, a model's output for a sentence.
q = E[word] - 2
Compare with every candidate
Cosine similarity: dot product divided by both lengths. 1 means the same direction, 0 unrelated.
s = q·v / (|q| |v|) - 3
Rank by similarity
Sort by score and keep the top few. Real systems use an approximate nearest-neighbour index to do this fast over millions of vectors.
top_k = sorted(s)[:k]
An embedding is a list of numbers that stands for a word, a sentence, an image or anything else. A model keeps an embedding table with one row per token; looking up a token's id gives its vector, and everything the model does starts from there.
Training arranges the vectors so that things used in similar ways end up pointing in similar directions. Closeness is usually measured with cosine similarity, which compares directions and ignores length.
Because similar meanings sit close together, embeddings turn “find things like this” into geometry: embed the query, embed the candidates, and rank by similarity. That is how semantic search, recommendations, clustering and RAG retrieval work.
Where you'll meet it
- Search by meaning rather than exact words, and the retrieval step of RAG.
- Recommendations, deduplication, clustering and grouping similar items.
- Interview favourites: “what is an embedding?”, “cosine similarity vs dot product?”, “why do embeddings capture meaning?”, “how would you build semantic search?”.
Comparing a query with n vectors of d numbers each costs n · d multiplications. Vector databases use approximate indexes (such as HNSW) to search millions of vectors without comparing every one.