Lesson 2 of 8 · Meaning as Vectors
Vector Similarity
Measure how similar embeddings are: dot product, cosine similarity and Euclidean distance, what each measures, why normalising helps and which to use.
- 5 min read
- Intermediate
- Reviewed
- Three ways to compare vectors: dot product, cosine similarity and distance
- Why cosine similarity looks at direction and ignores length
- Why normalised embeddings make all three measures agree
- Which measure to use with embeddings in practice
Close, but how close?
Saying "similar meanings are close together" needs a number. Three measures are used everywhere.
Direction versus length
Here are the three measures computed in Python:
import math
def dot(a, b):
total = 0
for x, y in zip(a, b):
total += x * y
return total
def length(a):
return math.sqrt(dot(a, a))
def cosine_similarity(a, b):
return dot(a, b) / (length(a) * length(b))
def euclidean_distance(a, b):
total = 0
for x, y in zip(a, b):
total += (x - y) ** 2
return math.sqrt(total)
query = [0.9, 0.1, 0.3]
documents = {
"same direction, short": [0.45, 0.05, 0.15],
"similar meaning": [0.8, 0.3, 0.4],
"different meaning": [0.1, 0.9, -0.2],
}
for name in documents:
v = documents[name]
print(f"{name:22} cosine {cosine_similarity(query, v):5.2f} "
f"distance {euclidean_distance(query, v):4.2f} dot {dot(query, v):5.2f}")- Cosine similarity says the "short" vector is a perfect match (1.00), because it points exactly the same way.
- Euclidean distance says "similar meaning" is closer (0.24 versus 0.48), because the short vector's tip is far away.
- The dot product mixes both: direction and length.
For meaning, direction is what matters. A longer vector might just come from a longer text, which doesn't make it more relevant. That is why cosine similarity is the standard measure for text embeddings.
Normalised vectors make life simple
Many embedding models return vectors that are already normalised. When every vector has length 1:
- the dot product equals the cosine similarity (the lengths you divide by are both 1), and
- ranking by Euclidean distance gives the same order as ranking by cosine similarity.
So with normalised embeddings, you can use the dot product, which is the fastest to compute, and get cosine similarity for free. Check your model's documentation, or normalise vectors yourself once when you store them.
Common mistakes
Compute the cosine similarity of two small vectors.
import math a = [1, 2, 2] b = [2, 1, 2] # Cosine similarity = dot(a, b) / (length(a) * length(b)) # where length(v) = square root of the sum of the squares. # Print the result with 2 decimal places.
Two embeddings point in exactly the same direction, but one is twice as long. What is their cosine similarity?
Summary
- The dot product combines direction and length; cosine similarity measures direction only; Euclidean distance measures how far apart the tips are.
- Cosine similarity is the standard for text embeddings, because direction carries the meaning.
- With normalised (unit-length) vectors, dot product equals cosine, and distance gives the same ranking.
- Choose similarity thresholds from your own model's scores, and never compare vectors from different models.
Next: how embeddings are created.
Finished reading? Tick it off to track your progress.