Distance✓ Mathematical
◆ The PatternMeasuring angular similarity between vectors — the backbone of retrieval
Cosine similarity measures the angle between two vectors, ignoring magnitude. Two vectors pointing the same direction have similarity 1, opposite = −1, perpendicular = 0. It's the standard metric for embeddings and RAG retrieval.
cos(θ) = (a · b) / (||a|| · ||b||)
a · b = dot product | ||a|| = L2 norm | Range: [−1, 1]
Cosine Distance = 1 − cos(θ)
Converts similarity to distance | Range: [0, 2] | 0 = identical direction
// Two vectors — adjust angle to see similarity change
Angle between vectors30°
Cosine Sim—
# PyTorch cosine similarity sim = F.cosine_similarity(a, b, dim=-1) # batch-wise sim = torch.dot(a, b) / (a.norm() * b.norm()) # single pair # For nearest-neighbor retrieval: sims = query @ embeddings.T # all similarities at once top_k = sims.topk(10) # top-10 most similar
In practice: Embeddings are often L2-normalized, making cosine similarity equivalent to a simple dot product. This is why dot-product search (FAISS, HNSW) is so fast.
Pattern bridge: The angle between embeddings measures semantic similarity — the same geometry as Pearson correlation on centered data. Vector search in RAG relies on this.