C.W.K.
Stream
Lesson 03 of 06 · published

Cosine, Euclidean, Dot Product

~22 min · math, metrics

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Three ways to measure closeness

Once your text is a vector, "how similar?" becomes "how do we measure distance?" There are three answers you will see in production, and choosing wrong silently degrades retrieval quality.

Cosine similarity (default for text)

Measures the angle between two vectors. Identical direction = 1, opposite = -1, perpendicular = 0. It ignores magnitude, which is exactly what you want when document length should not bias similarity.

Euclidean distance (L2)

Straight-line distance in high-dimensional space. Smaller = closer. Useful when magnitude carries meaning (frequency-weighted vectors, image embeddings without normalization).

Dot product (inner product)

Combines angle and magnitude in one number. For unit-length vectors it equals cosine, but it is faster on GPU because there is no normalization. Recommendation systems often pick dot product on purpose so popular items get a boost.

Most modern text embedding models output unit-length vectors

For OpenAI, Voyage, Cohere, BGE, and most sentence-transformers models, the vector is already L2-normalized. That makes cosine, dot product, and a monotonic function of Euclidean distance equivalent — pick whatever your vector store offers most efficiently. Read the model card before assuming.

Code

Three metrics in NumPy·python
import numpy as np

def cosine(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

def euclidean(a, b):
    return np.linalg.norm(a - b)

def dot(a, b):
    return np.dot(a, b)

a = np.array([1.0, 0.0, 0.0])
b = np.array([0.9, 0.4, 0.0])
print('cosine    ', cosine(a, b))     # ~0.913
print('euclidean ', euclidean(a, b))  # 0.412
print('dot       ', dot(a, b))        # 0.9
Confirm your model emits unit vectors·python
norms = np.linalg.norm(vecs, axis=1)
assert np.allclose(norms, 1.0, atol=1e-3), 'embeddings are not L2-normalized'

External links

Exercise

Take any 5 sentence pairs (3 related, 2 unrelated). Compute cosine, Euclidean, and dot product on both raw and L2-normalized vectors. Confirm that for unit vectors the three metrics agree on ranking. Show the numbers in a table.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.