liveBook · Manning

Cosine similarity

As explained in Transformers in Action

Cosine similarity is a metric that measures the directional similarity between two vectors using the cosine of the angle between them.

Cosine similarity measures how similarly two vectors point by comparing the angle between them. For embeddings, it turns semantic or contextual relatedness into a similarity score for tokens, sentences, or documents. section 3.4.1

Embedding systems use cosine similarity to:

  • Retrieve documents whose embeddings are nearest to a query embedding. section 3.4.1
  • Compare model responses with queries to estimate semantic consistency or flag potential hallucinations. section 3.4.1

How Transformers in Action covers it

How deep it goes
Defines cosine similarity as an embedding comparison measure and demonstrates computation, but does not derive its mathematical formula.
Examples
Uses contextual word meanings, sentence embeddings, query-document pairs, and a printed similarity matrix.
What it assumes
Assumes familiarity with vectors and embeddings while demonstrating sentence encoding from model loading onward.
What you can do after
Apply it via SentenceTransformer and util.cos_sim to compare encoded queries with documents.
How it connects
Connects cosine similarity to embeddings, transformers, vector search, semantic retrieval, and approximate nearest neighbor search.

Also explained in

Compare how 14 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.