liveBook · Manning

Similarity metric

As explained in Transformers in Action

A similarity score is a numerical value indicating how closely two represented items align according to a specified similarity measure.

A similarity metric produces a numerical score for how closely two represented items align. With embeddings, cosine similarity compares query and document vectors by semantic direction rather than surface wording. section 3.4.1

For Q query embeddings and D document embeddings, the resulting (Q, D) tensor records each pairwise alignment. Higher values indicate stronger similarity, approaching 1.0 for directionally aligned vectors. section 3.4.1

Listing 3.7 Similarity search between documents and query
model = SentenceTransformer('Owen/Owen3-Embedding-0.6B')   #1

queries = [  #2
    'What is an embedding in machine learning?',
    'How are embeddings generated in transformer models?'
]

documents = [
    'Embeddings are vector representations of data, such as words or 
    sentences, used to capture semantic relationships in machine learning 
    models.',
    'Transformer models generate embeddings by passing input tokens through 
    multiple layers of attention and feedforward networks, producing 
    contextualized vector outputs.'
]

query_embeddings = model.encode(queries, prompt_name='query')
document_embeddings = model.encode(documents)

similarity_scores = util.cos_sim(
query_embeddings, document_embeddings)  #3

print(similarity_scores)

Encode queries and documents with a pretrained embedding model, then compute their cosine-similarity matrix. section 3.4.1

tensor([[0.6683, 0.5525], [0.5882, 0.7510]])

Read the pairwise scores to identify which document most closely matches each query. section 3.4.1

How Transformers in Action covers it

How deep it goes
Explains cosine scores, pairwise tensor shape, and directional interpretation, but defers the metric’s mathematical derivation.
Examples
Uses transformer-generated sentence embeddings, query-document pairs, cosine-similarity matrices, and a printed score tensor.
What it assumes
Assumes familiarity with embeddings and vector representations while demonstrating their generation from raw sentences.
What you can do after
Apply cosine similarity through SentenceTransformer to rank documents against encoded queries.
How it connects
Connects embeddings with semantic search, vector search, approximate nearest neighbor search, clustering, and retrieval-augmented generation.

Also explained in

Compare how 10 books explain similarity metric →

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.