Two metrics to compare embedding vectors. Picking the wrong one silently degrades retrieval quality.
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)
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)
cosine = 0 does not always mean unrelated — calibrate against a held-out set.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.