Topic 2 of 5 · 3 min read
Measuring similarity between embeddings
Goal: Rank a set of sentences by their similarity scores to a query and explain why the ranking came out that way.
To compare two embeddings, you measure how close their vectors are; the most common measure is cosine similarity, and a higher score means more similar. Search then becomes: embed the query, score it against every stored vector, and return the best few.
Check yourself · 1 of 3
Two embeddings have a cosine similarity near 1. What does that suggest?
Why it matters
Every embedding feature rests on one step: "which stored texts are nearest to this one?" If you misread the score, you will trust weak matches or throw away good ones. Knowing what the number means keeps you honest about results.
How it works
Picture each vector as an arrow from the same starting point. Cosine similarity looks at the angle between two arrows.
- Arrows pointing the same way: score near 1. Very similar.
- Arrows at right angles: score near 0. Unrelated.
- Arrows pointing opposite ways: score near -1. Rare in practice for text.
It ignores how long the arrows are, and only compares direction. That is why it works well for text: a long document and a short sentence can still point the same way.
Two other measures show up in tools. Dot product is the same as cosine when vectors are normalised to length 1, which many providers do for you. Euclidean distance measures the straight-line gap, where a smaller number means closer. Read the docs of your model to see which one it was trained for, and use that one.
Nearest-neighbour search is the lookup itself: find the k stored vectors with the highest scores. With a few thousand texts you can score them all. With millions, a vector database or index uses a shortcut called approximate nearest neighbour search. It is much faster and may occasionally miss a true top result.
Before and after
Before: "The search returned a score of 0.78, so it is a strong match."
After: "0.78 is the best of our five candidates, but scores depend on the model. In this model, unrelated texts often score around 0.5 to 0.7, so 0.78 is only a weak lead. We check the text itself."
A score is not a percentage of correctness. It is only useful for ranking, or for a cut-off you chose by testing your own data.
What goes wrong
- Fixed thresholds copied between models. A cut-off of 0.8 may be strict in one model and loose in another. Test on a sample of your own queries.
- Reading the score as probability. 0.9 does not mean "90% sure". It means "close in this model's space".
- Ignoring the ranking order. The top result being first matters more than its absolute score. Check the top few by eye.
Remember: the ranking is what you can rely on, and the raw number only means something when you have calibrated it on your own examples.
Examples
Ranking by score
Query: cheap flights to Madrid
| Candidate | Score |
|---|---|
| Budget airfare to Spain | 0.84 |
| Madrid hotel deals | 0.71 |
| How to cook paella | 0.38 |
The ranking makes sense: flights to Spain first, a related travel topic second, food last. The gap between 0.84 and 0.71 is smaller in meaning than it looks, so a person should still check the top two.
Optional code: cosine by hand
import numpy as np
def cosine(a, b):
a, b = np.array(a), np.array(b)
return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))
scores = [(text, cosine(query_vec, vec)) for text, vec in store]
top3 = sorted(scores, key=lambda s: s[1], reverse=True)[:3]
This scans every item, which is fine for thousands of texts. Use an index for millions.
Key points
- Cosine similarity compares the direction of two vectors and gives a score near 1 for very similar texts.
- Search embeds the query, scores it against stored vectors and returns the top few nearest neighbours.
- A similarity score is not a probability or a percentage of correctness.
- Score ranges differ between models, so a cut-off must be tested on your own data.
- Large collections use approximate nearest-neighbour indexes, which are fast and can occasionally miss a true match.
Common mistakes
- Reading a score of 0.8 as 80 percent confident.
- Reusing a similarity cut-off from one embedding model on another.
- Trusting the order beyond the top few results without looking at the texts.
Check yourself · 2 of 3
What is nearest-neighbour search in an embedding system?
Check yourself · 3 of 3
A search returns its best match with a score of 0.78. A colleague says 'that is 78% likely to be right'. What is the best reply?
Practice items, not counted towards your mastery.
Start here
- Embeddings (Google Machine Learning Crash Course) (opens in a new tab)CourseFree
A free, visual course lesson on how embeddings place similar items close together in a vector space.
- Vector embeddings guide (OpenAI API docs) (opens in a new tab)DocsFree
Shows how to request embeddings, compare them with cosine similarity and use them for search and clustering.
- Sentence Transformers documentation (opens in a new tab)DocsFree
Open-weight embedding models you can run yourself, with semantic search and similarity examples.