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, and is the standard similarity metric for embeddings because it is magnitude-invariant.

What is Cosine Similarity?

Cosine similarity measures the cosine of the angle between two vectors, ranging from -1 to 1, and is the standard similarity metric for embeddings because it is magnitude-invariant.

Cosine similarity measures the cosine of the angle between two vectors, ranging from -1 to 1, and is the standard similarity metric for embeddings because it is magnitude-invariant.

Where is it used?

RAG systems retrieve documents by cosine similarity between query and chunk embeddings; recommendation systems and semantic search all rely on it.

How to build it

Use `F.cosine_similarity(emb_a, emb_b, dim=0)` or implement manually as `dot(a,b) / (norm(a) * norm(b))`, and rank documents by similarity to a query embedding.

Code

A practical example:

example.pypython