Skip to Content

Two metrics to compare embedding vectors. Picking the wrong one silently degrades retrieval quality.

Data Flow

          v1                  v2
          ^ (theta)           ^
         /|                /
        / |               /
     direction       straight-line
     (cosine)        distance (L2)

cos(v1, v2) = (v1 . v2) / (||v1|| * ||v2||)
L2(v1, v2)   = sqrt(sum_i (v1_i - v2_i)^2)

When to use which

Code

import numpy as np
 
def cosine(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-12)
 
def l2(a, b):
    return np.linalg.norm(a - b)

Pitfalls

Analogy

Cosine is the angle between two arrows; L2 is how far you would walk to align them.

Interview tip: Pick cosine + normalise for Sentence-Transformers in pgvector.

Advertisement