Semantic Similarity and Distance Metrics
In this article
- Explain cosine similarity, dot product and Euclidean distance as pictures of two arrows, and predict which chunk each one ranks first.
- Say what scaling every vector to length 1 changes, and why the three then agree.
- Read pgvector's `<->`, `<#>` and `<=>` the right way round and pick the one that fits your embedding model.
- Reject a similarity threshold copied from another model, metric or corpus.
- Terms defined here: cosine similarity, dot product, Euclidean distance, vector normalization.
Overview
A customer asks how to get their money back, and the search behind a support assistant returns the router setup page first. Nothing is broken. Each page is stored with a list of numbers, a vector, that stands for its meaning, and the code returned the page whose vector was "closest" to the question's, exactly as asked. The surprise is in what "closest" meant.
Take Brightline, a made-up home-internet provider: its support assistant splits its help pages into short pieces called chunks and returns the 3 whose vectors are closest to the question.
There are three common ways to measure how close two vectors are, and they can disagree. After this you can predict which chunk each measure ranks first, read pgvector's three operators the right way round, and refuse a similarity threshold copied from another model.
Sign in to keep reading
The rest of this article opens with an account. An account also keeps your progress and quiz results on every device, and lets you follow news for up to five topics.
Still to read in this article
- Three ways to say "close"
- Why length wins the dot product
- Scaling every arrow to length 1
- Which way is closer
- A score is not a probability
- Common mistakes
- Sources
- Check yourself, 13 questions