Embeddings

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.

cat100.00.10.10.80.90.0

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.

dogcatpuppykittenkingqueenprinceapplebreadpizza

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.

catkittenapplecat · kitten: 0.99cat · apple: 0

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.

idwordroyalmalefemalegrown-upanimalfood
01.01.00.01.00.00.0
11.00.01.01.00.00.0
21.01.00.00.20.00.0
31.00.01.00.20.00.0
40.01.00.01.00.00.0
50.00.01.01.00.00.0
60.01.00.00.20.00.0
70.00.01.00.20.00.0
80.00.10.10.81.00.0
90.00.10.10.11.00.0
100.00.10.10.80.90.0
110.00.10.10.10.90.0
120.00.00.00.00.01.0
130.00.00.00.10.00.9
140.00.00.00.20.00.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.

Word
“dog”
00.10.10.81.00
  • king?
  • queen?
  • prince?
  • princess?
  • man?
  • woman?
  • boy?
  • girl?
  • puppy?
  • cat?
  • kitten?
  • apple?
  • bread?
  • pizza?
Step 0 / 16
700 ms

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.

-2-1012-2-1012ab
20°
65°
1.6
1
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.

Analogy
royalmalefemalegrown-upanimalfood
king
1.01.001.000
− man
0-1.00-1.000
+ woman
001.01.000
= result
1.001.01.000
queen (nearest)
1.001.01.000

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.

You can return an unused purchase within 30 days of delivery. The item must be unused, but its packaging may have been opened.Return shipping is free only when the item arrived damaged. For all other returns, the customer pays the return shipping cost.Start a return by emailing support with your order number. Keep your shipping receipt until the returned package reaches our warehouse.A refund starts after our warehouse receives and checks the returned item. The money reaches your original payment method within five business days after that check.We send a confirmation email when the refund is issued. An exchange sends a replacement item instead of returning money.Gift purchases can be refunded to the original buyer only. Our support team can look up a refund using the order number.Standard delivery takes three to five business days. Express delivery takes one to two business days.Orders above 50 dollars qualify for free standard delivery. Delivery estimates begin when the package leaves our warehouse.A tracking link is emailed when your order ships. Contact support if tracking says delivered but the package is missing.The warranty covers manufacturing defects for one year after purchase. Accidental damage and ordinary wear are not covered by the warranty.Email support a photo and the order number to request a warranty assessment. A warranty assessment determines whether a repair or replacement is available.A warranty claim is different from returning an unused item. Warranty requests do not change the shop's 30-day return window.How many days do I have to return an unused purchase?I unboxed my purchase but never used it. Can I send it back?Who pays return shipping if my item arrived damaged, and who pays for other returns?What is the return deadline, and how long does a refund take after the warehouse checks the item?Do I get a refund if express delivery arrives late?Does Luma Shop offer a student discount?
Returns passageRefunds passageDelivery passageWarranty passage☆ question

“I unboxed my purchase but never used it. Can I send it back?”

Closest passages, by cosine similarity over all 384 numbers:

  1. Returns0.563You can return an unused purchase within 30 days of delivery. The item must be unused, but its packaging may have been opened.
  2. Returns0.542Start a return by emailing support with your order number. Keep your shipping receipt until the returned package reaches our warehouse.
  3. 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. 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. 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. 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. 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. 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. 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. 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?”.
time O(n · d) for a brute-force searchspace O(n · d)

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.