LLM Learning Hub

workspace/llm-course/home

Cosine Similarity

Cosine similarity measures the cosine of the angle between two vectors, ranging from -1 to 1; it ignores magnitude and focuses on direction, making it ideal for semantic text similarity.

What is Cosine Similarity?

Cosine similarity measures the cosine of the angle between two vectors, ranging from -1 to 1; it ignores magnitude and focuses on direction, making it ideal for semantic text similarity.

Cosine similarity measures the cosine of the angle between two vectors, ranging from -1 to 1; it ignores magnitude and focuses on direction, making it ideal for semantic text similarity.

Where is it used?

OpenAI embeddings, `BAAI/bge`, and most text embedding models are evaluated with cosine similarity; vector stores like Qdrant support `Distance.COSINE` as a first-class metric.

How to build it

Use `F.cosine_similarity(a.unsqueeze(0), b.unsqueeze(0))` in PyTorch or `sklearn.metrics.pairwise.cosine_similarity([a],[b])`; normalise vectors and use dot-product for the same result faster.