← AI Terminology

Cosine Similarity

Cosine similarity measures the cosine of the angle between two vectors — 1 for same direction, 0 for orthogonal, −1 for opposite — ignoring magnitude when vectors are compared by orientation.

It is the default similarity for many text embeddings.
Why It Matters in AI
Embedding models often encode meaning in direction more than length. Cosine (or dot product on L2-normalised vectors) is the standard relevance score in semantic search and clustering of text embeddings.
Key Points
Aspect Description
Use RAG retrieval, duplicate detection, clustering
Range [−1, 1] for real vectors; often [0, 1] in practice for embeddings
Vs L2 Cosine ignores magnitude; L2 cares about length
Formula cos(a,b) = (a·b) / (‖a‖‖b‖)
Related Dot product, Euclidean distance, ANN metrics
Practice Normalise embeddings, then use dot product
Simple Analogy
Comparing whether two arrows point the same way, not how long they are — direction of meaning over raw strength.
Common Usage Examples
  • F.cosine_similarity(q, docs, dim=-1)
  • sklearn cosine_similarity
  • Normalise then FAISS inner product index
  • Threshold for near-duplicate docs
Summary
In short: Cosine similarity scores how aligned two vectors’ directions are — the everyday metric for embedding-based semantic search.