We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Why Cosine Similarity?

You might wonder: why cosine similarity specifically? Why not some other similarity metric?

The answer is that we want to use the same metric that our embedding model was trained on.

Embedding models learn to create vectors during training. The training process determines which similarity metric usually works best.

"We fine-tune the model using a contrastive objective. Formally, we compute the cosine similarity from each possible sentence pair from the batch."

all-MiniLM-L6-v2 documentation

Because our model was trained using cosine similarity, we should also use cosine similarity for search.

Cosine Similarity Is Common

Good news – many sentence embedding models are trained with cosine similarity:

  • all-MiniLM-L6-v2: cosine similarity
  • all-mpnet-base-v2: cosine similarity
  • Other sentence-transformers models typically use cosine similarity.

When using other embedding models in the future, double-check their documentation to be sure!