AI & ML — INTERMEDIATE
Embeddings for Engineers: Cosine Similarity, Training & Benchmarks
“Similar meanings end up close together” is the intuition. Here’s the actual formula that measures “close,” the training process that makes it true in the first place, and how you’d actually pick a model for a real system.
The Quick Answer
Closeness between two embeddings is measured with cosine similarity: the cosine of the angle between the two vectors, which ignores their length and only measures direction. Embedding models learn to produce these directions through contrastive learning — trained on pairs of texts that are known to be similar or dissimilar, and adjusted until similar pairs point in nearly the same direction. Which embedding model to actually use in production is an empirical question, answered by benchmarks like MTEB rather than intuition.
Cosine Similarity, the Actual Formula
Two embedding vectors, A and B, are compared using the cosine of the angle between them:
The numerator is the dot product — how much the two vectors point in the same direction, before accounting for their size. The denominator divides that out by each vector’s own magnitude, which is exactly why length doesn’t matter: a one-sentence query and a ten-page document can still score close to 1.0 if they’re about the same thing, because only the direction of each vector is being compared, never how long it is. The result ranges from -1 (opposite meaning) to 1 (identical direction), with values near 0 meaning the two texts are essentially unrelated.
Try it below — four fixed sentences, real 4-dimensional toy vectors, and the actual cosine similarity computed between whichever pair you pick.
How Embedding Models Are Actually Trained
Embedding models don’t learn “meaning” directly — they learn from contrastive training pairs. A positive pair is two texts known to be semantically related (a question and its correct answer, two paraphrases of the same sentence); a negative pair is two texts that aren’t. During training, the model’s parameters are adjusted so that positive pairs get pulled closer together in vector space and negative pairs get pushed further apart — repeated across millions of pairs until “semantically related” and “geometrically close” become nearly the same statement.
Picking a Model: MTEB
There’s no single “best” embedding model — performance depends heavily on the task and language. The Massive Text Embedding Benchmark (MTEB) is the standard reference for this: it evaluates embedding models across 8 different task types (like classification, clustering, and retrieval) spanning 58 datasets and 112 languages. Its own headline finding is worth internalizing before picking a model off a leaderboard: no single embedding method wins across every task, which means the right choice genuinely depends on what you’re building, not just which model scores highest overall.
Source: Muennighoff et al., “MTEB: Massive Text Embedding Benchmark,” arXiv:2210.07316
Why This Matters for Search
Once text is represented as a vector, finding “similar” content becomes a geometry problem: embed a search query the same way, then find which stored vectors have the highest cosine similarity to it. That single idea — compare vectors instead of matching keywords — is the foundation of semantic search, and the reason it can match “affordable plane tickets” to a document that only ever says “cheap flights.” Storing and searching millions of these vectors efficiently is its own engineering problem, covered in the Vector Database article.
Fun Fact
MTEB’s original 2022 evaluation covered 33 embedding models. By the time you read this, the public MTEB leaderboard tracks hundreds more — the benchmark itself became the de facto way new embedding models prove themselves before anyone adopts them in production.
Test Yourself
What does cosine similarity actually measure between two embedding vectors?
In contrastive learning for embeddings, what happens to a "positive pair" (semantically similar texts) during training?
Why does cosine similarity ignore vector magnitude instead of using raw distance?
What is MTEB?
What did MTEB’s original evaluation find about embedding models?
Ready for a fully worked cosine similarity example with real code, plus how production systems shrink and search millions of embeddings? Continue to the Advanced level →