← Learn / Notebooks

Models & toolsPractitionerStudy guide

Text Embeddings: Cosine Similarity and Retrieval

Explore vector direction, calculate cosine scores and see why a geometric match does not prove answer relevance.

Before you start: Coordinate pairs and simple multiplication. Trigonometry is explained by the picture.

AI-assisted study notebook · Published 14 Nov 2026. Source-support checks and synthetic examples are recorded; automated review is not independent expert fact verification.

In [1]: Architecture

1. Architecture and scope

Text embedding models map text to vectors. A retrieval system can compare a query vector with document vectors and rank candidates by a chosen similarity function. That representation is learned by a real model; the four vectors below are hand-authored geometric examples, not embeddings of the displayed labels. [1]

Cosine similarity divides the dot product by both vector lengths. It measures direction for nonzero vectors. Multiplying one vector by a positive scalar leaves its cosine with another vector unchanged. On unit-length vectors, the denominator is one, so cosine equals the dot product. [2]

Scope. Hand-authored two-dimensional vectors. No embedding model is downloaded or evaluated.

Workflow and scope notes
  1. Prepare text. Decide which text units the actual system will encode. Chunking and model choice are outside the geometric experiment.
  2. Encode vectors. A real encoder supplies numeric representations for query and passages. Here the coordinates are declared by hand so every score can be inspected.
  3. Score candidates. Compare nonzero vectors using the stated cosine formula. Other similarity functions have different assumptions and behavior.
  4. Inspect retrieval. Sort scores and evaluate against the question’s actual evidence needs. A high score is a geometric result, not an independent truth or relevance judgment.
In [2]: Experiment

2. Vector direction and cosine ranking

Documents D1–D4 point at 0°, 45°, 90° and 135°. Rotate the query between 0° and 180°. Each arrow has length 1. The second chart shows all four scores on the full −1 to +1 scale.

Vector direction and cosine ranking

Original synthetic experiment · Change an input to update the figure.

cos(q,d) = (q · d) / (‖q‖ ‖d‖), for nonzero vectors. Exact ties use document ID order in this toy. Labels have no semantic meaning and the score is not a probability.

Query geometry and similarity scoresRanking: D2 → D1 → D3 → D4 · Scores 0.8660, 0.9659, 0.5000, -0.2588. Scores use a −1 to +1 axis. All vectors have unit length.xyD1D2D3D4Query 30°COSINE SCORE−10+1D10.866D20.966D30.500D4-0.259
Ranking: D2 → D1 → D3 → D4 · Scores 0.8660, 0.9659, 0.5000, -0.2588
Calculation record
{
  "inputs": {
    "angle": "30"
  },
  "scores": [
    0.8660254037844387,
    0.9659258262890683,
    0.5000000000000001,
    -0.25881904510252085
  ],
  "order": [
    "D2",
    "D1",
    "D3",
    "D4"
  ]
}
CaseResultInterpretation
0° between vectorsCosine = 1Same direction, including positive rescaling.
90° between vectorsCosine = 0Orthogonal directions.
180° between vectorsCosine = −1Opposite directions; negative values are possible.
In [3]: Interpretation

3. Interpretation and limitations

At a query angle of 45°, D2 has score 1, while D1 and D3 tie near 0.7071. The tie is broken by ID here. A query at 180° gives D1 score −1. The figure therefore preserves negative scores rather than clipping them into an apparently probabilistic range.

The mathematical denominator vanishes for a zero vector. This notebook’s Python helper raises an error instead of choosing a library-specific fallback. A production system should record the actual encoder and similarity implementation, including normalization and its zero-vector behavior. [2]

Editorial retrieval check: inspect which passages a ranking actually returns and label their relevance to real questions. A model’s similarity convention, an index configuration and evidence quality are separate parts of that evaluation. This exercise establishes only the declared geometry. [1] [3]

Practice exercise

At 90°, predict the order of D1–D4, including the tie. Then explain why multiplying D3 by 7 would not change its cosine with the query.

In [4]: Python

4. Worked Python example

The core calculation below uses only the Python standard library. The full download adds the verification assertions and input fixtures. It runs locally on synthetic data; it does not load a model or contact a service.

lab.py · Core example · Python 3.13
from math import cos, sin, radians, sqrt, isclose


def vector(angle):
    # Hand-authored unit vectors; these are not text model outputs.
    return (cos(radians(angle)), sin(radians(angle)))


def cosine(a, b):
    denominator = sqrt(sum(x*x for x in a) * sum(y*y for y in b))
    if denominator == 0:
        raise ValueError("Zero vector is outside this toy's domain")
    return sum(x*y for x, y in zip(a, b)) / denominator


def result(angle):
    scores = [cosine(vector(angle), vector(d)) for d in (0, 45, 90, 135)]
    order = sorted(range(4), key=lambda i: (-round(scores[i], 12), i))
    return {"scores": scores, "order": ["D" + str(i+1) for i in order]}

Download full Python example and checks ↓

Run: python -I lab.py. Recorded run: Python 3.13; 7 fixture states plus scenario assertions passed. The JSON output is a reproducibility record, not a real-system benchmark.

In [5]: Knowledge check

5. Questions and explanations

Predict before opening the explanation. Answers refer to the stated scope and synthetic inputs.

Q1 · DirectionWhat is the cosine of two nonzero perpendicular vectors?
What is the cosine of two nonzero perpendicular vectors?
Answer and explanation

Answer: 0. Their dot product is zero, so their normalized dot product is zero. [2]

Q2 · ScalingMultiply one vector by a positive number. What happens to cosine?
Multiply one vector by a positive number. What happens to cosine?
Answer and explanation

Answer: It stays the same. The scale appears in the numerator and length denominator and cancels. [2]

Q3 · EvidenceDoes a score of 0.9 prove a passage supports an answer?
Does a score of 0.9 prove a passage supports an answer?
Answer and explanation

Answer: No. A geometric match does not inspect the passage’s factual support.

Q4 · BoundaryWhat policy does this example use for a zero vector?
What policy does this example use for a zero vector?
Answer and explanation

Answer: Reject it. The helper rejects a denominator of zero; that explicit policy is part of this toy.

In [6]: Sources

References and source links

  1. Intel / Hugging Face authors. (2024). CPU Optimized Embeddings with 🤗 Optimum Intel and fastRAG.https://huggingface.co/blog/intel-fast-embedding
  2. scikit-learn maintainers. (v1.9.1). cosine_similarity — scikit-learn 1.9.1 documentation.https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html
  3. Sentence Transformers maintainers. (Documentation accessed 2026). Semantic Textual Similarity — Sentence Transformers documentation.https://www.sbert.net/docs/sentence_transformer/usage/semantic_textual_similarity.html
Source checks and provenance

Primary publisher pages and relevant passages checked on 13 Nov 2026. Source support is not independent expert fact verification. Main documentation can change; retrieved-body hashes, dates, version labels and saved revision IDs are in the audit download.

[1] CPU Optimized Embeddings with 🤗 Optimum Intel and fastRAG

Evidence: Information Retrieval with Embedding Models. Saved public original used for the embedding/retrieval context, not its hardware measurements.

Public ResearchNode original e22d4f6821d6b88bbaee5294a65f8a31
Revision add9531d9d9c7a46dfd0f2c4d2bad106. Captured 2026-11-06.

[2] cosine_similarity — scikit-learn 1.9.1 documentation

Evidence: cosine_similarity API definition. Mathematical definition. This toy rejects zero vectors explicitly.

[3] Semantic Textual Similarity — Sentence Transformers documentation

Evidence: Similarity Calculation. Library documents multiple similarity choices. A cosine score is not a calibrated relevance probability.

Claim-to-evidence ledger ↓ · Review report ↓