M01 · LLM foundations and evaluation basics01-0422 min read

Lesson 9 of 106 · Module 2 of 14 · Week 1

Threads:The measurement threadThe weights threadThe core-concepts thread

Vectors, Dot Products, and Cosine Similarity Explained

A dot product multiplies two vectors element-wise and sums the result, giving a single number that grows with both alignment and magnitude; cosine similarity is that same dot product divided by both vectors' lengths, which strips magnitude out and leaves pure directional agreement on a −1 to 1 scale. Cosine similarity is the default metric for comparing text embeddings because it makes a short document and a long document about the same topic score as similar.

01

What a dot product and cosine similarity are

Dot product. For vectors a and b of the same length:

text
a · b = a₁b₁ + a₂b₂ + … + aₙbₙ

One number out. Positive means the vectors broadly point the same way, near zero means they are roughly perpendicular (unrelated), negative means they oppose.

Cosine similarity. The dot product normalised by both magnitudes:

text
cos(a, b) = (a · b) / (‖a‖ · ‖b‖)        where ‖a‖ = √(a₁² + a₂² + … + aₙ²)

Bounded in [−1, 1] regardless of dimension: 1 = identical direction, 0 = orthogonal, −1 = opposite.

When to use which — the decision rule:

Use cosine similarity when magnitude is noise. Use the dot product when magnitude carries signal you want to reward.

For text embeddings, magnitude is usually noise — it tracks things like document length rather than meaning — so cosine similarity is the default and the metric almost every embedding model documents. The dot product is preferred where a longer or more "confident" vector should legitimately score higher, and inside attention, where the score is a dot product deliberately left unnormalised (only scaled by a constant).

The three quantities you need named, and nothing more

QuantityFormulaWhat it tells youRange
Magnitude / L2 norm ‖a‖√(Σaᵢ²)How long the arrow is0 to ∞
Dot product a · bΣ aᵢbᵢAlignment scaled by both lengths−∞ to ∞
Cosine similarity cos(a,b)(a·b)/(‖a‖‖b‖)Alignment only−1 to 1

Note the relationship between rows: the dot product is the numerator of cosine similarity, and cosine similarity is a dot product with the length information deliberately divided away. They are not two unrelated metrics; one is the other with a normalisation step. Almost every confusion on this topic dissolves once you hold that.

L2 normalisation is the operation of dividing a vector by its own magnitude, producing a unit vector that points the same way with length exactly 1. It is the single most consequential preprocessing step in retrieval pipelines, for reasons section 2 makes precise.

02

How dot products and cosine similarity measure semantic similarity

L1 — Direction is meaning

An embedding places a piece of text at a point in high-dimensional space so that similar meanings land in similar directions (01-03 gave you the shape of such a vector; the embedding lessons in Module 2 explain how the placement is learned). "Similar meaning" therefore becomes a geometric question: do these two arrows point the same way? Cosine similarity answers exactly that question and nothing else.

The mental image that survives contact with the exam: think of every text as an arrow from the origin. Two arrows pointing the same way are about the same thing. The lengths of the arrows are an accident of how much text there was. Cosine ignores the lengths; the dot product does not.

L2 — Why normalising matters, with the identity that follows from it

Consider a one-sentence note and a fifty-page report, both entirely about GPU memory. Their embeddings may point in nearly the same direction but have very different lengths. The dot product would score the report higher against any query, purely because it is longer. Cosine similarity divides that out and scores them as near-equally relevant.

Now the identity worth memorising:

If every vector is L2-normalised to unit length, then ‖a‖ = ‖b‖ = 1, so cosine similarity and the dot product are numerically identical — and ranking by Euclidean distance gives the same order too, because for unit vectors ‖a − b‖² = 2 − 2·cos(a, b).

Three metrics, one ranking. This is why production embedding pipelines normalise vectors once at index time and then let the vector database use its cheapest inner-product path: you get cosine semantics at dot-product cost. It also explains why a vector database offering "cosine," "inner product" and "L2" as index metrics is not offering three different notions of relevance once your vectors are normalised — it is offering three implementations of the same ranking. On unnormalised vectors they genuinely differ, and picking the wrong one silently biases retrieval toward long documents.

Worth verifying the algebra once so it is yours rather than borrowed. For unit vectors:

text
‖a − b‖² = (a − b)·(a − b)
         = a·a − 2(a·b) + b·b
         = ‖a‖² − 2(a·b) + ‖b‖²
         = 1 − 2·cos(a,b) + 1
         = 2 − 2·cos(a,b)

Since cosine similarity appears with a negative coefficient, larger cosine means smaller squared distance, monotonically. Rank ascending by distance or descending by cosine and you get the identical ordering. That is the whole proof, and it takes four lines.

L3 — Where the dot product shows up inside the model

Attention computes a dot product between each query vector and every key vector to decide how much one token should attend to another, then divides by √d (the scaled in scaled dot-product attention) to keep the values in a range where softmax behaves. Note the difference from cosine: the divisor is a constant derived from the dimension, not the vectors' own magnitudes. Attention deliberately keeps magnitude information.

Why divide by √d at all? Because the dot product of two d-dimensional vectors is a sum of d terms, so its typical size grows with d. Feed very large scores into softmax and the distribution saturates — one entry approaches 1, the rest approach 0, and gradients through it approach nothing. Dividing by √d keeps the scale roughly constant as the dimension grows. That is the entire content of the word "scaled," and it is the level of detail the exam asks for.

You are expected to recognise that attention scores are dot products. You are not expected to derive attention — candidate reports name detailed attention math as depth that did not appear on the exam. This lesson therefore stops here on the mechanism side and spends the rest of its length on the parts that are tested: metric choice, ranges, failure modes, and the retrieval consequences.

03

Cosine similarity vs dot product vs Euclidean distance vs the metrics they get confused with

Cosine similarityDot productEuclidean (L2) distanceManhattan (L1)Pearson correlationJaccard
Formula(a·b)/(‖a‖‖b‖)a·b√Σ(aᵢ−bᵢ)²Σ|aᵢ−bᵢ|cosine of mean-centred vectors|A∩B|/|A∪B|
Operates onTwo dense vectorsTwo dense vectorsTwo dense vectorsTwo dense vectorsTwo dense vectorsTwo sets
Range−1 to 1Unbounded0 to ∞0 to ∞−1 to 10 to 1
Higher meansMore similarMore similarLess similarLess similarMore correlatedMore overlap
Magnitude-sensitive?NoYesYesYesNoN/A
Default for text embeddingsYesOn normalised vectorsRarelyRarelyNoNo — sparse/keyword sets
CostDot product + 2 normsCheapestComparable to cosineComparableCosine + centringSet operations

Three traps in that table.

The direction reversal is the first: cosine and dot product are similarities where bigger is better, Euclidean and Manhattan are distances where smaller is better. Reading a "score" without knowing which family it belongs to inverts your ranking, and a distractor that says "the document with the highest Euclidean distance is most relevant" is testing exactly that.

The second is "cosine distance," which some libraries define as 1 − cosine similarity to make it behave like a distance. That puts it in the range 0 to 2, and reverses the direction. Check the sign convention of whatever tool you are using rather than assuming.

The third is Jaccard, which is not a vector metric at all. It compares sets — the classic use being overlap of keyword or shingle sets — which puts it on the sparse/lexical side of retrieval rather than the dense/semantic side. If an item contrasts dense with sparse retrieval, cosine belongs to the dense column and set-overlap measures belong to the sparse one.

And a fourth thing that is not in the table because it is a different category entirely: cosine similarity is not a loss function, not an accuracy metric, and not a way to compare two models. It compares two vectors. 01-05 handles losses; the metrics that compare models are a different family again.

04

Worked example: two ways to rank three documents

A query and three candidate documents, in a deliberately tiny 3-dimensional embedding space:

text
query    q = [1, 1, 0]
doc A    a = [2, 2, 0]      # same direction as q, twice as long
doc B    b = [1, 0, 1]      # partial overlap
doc C    c = [0, 0, 5]      # unrelated direction, but a long vector

Dot products:

text
q · a = 1(2) + 1(2) + 0(0) = 4
q · b = 1(1) + 1(0) + 0(1) = 1
q · c = 1(0) + 1(0) + 0(5) = 0

Magnitudes: ‖q‖ = √2 ≈ 1.414, ‖a‖ = √8 ≈ 2.828, ‖b‖ = √2 ≈ 1.414, ‖c‖ = 5.

Cosine similarities:

text
cos(q,a) = 4 / (1.414 × 2.828) = 4 / 4.0   = 1.00
cos(q,b) = 1 / (1.414 × 1.414) = 1 / 2.0   = 0.50
cos(q,c) = 0 / (1.414 × 5.0)   = 0 / 7.07  = 0.00

Read the result. Doc A scores a perfect 1.00 on cosine despite being twice as long as the query, because it points in exactly the same direction — which is the behaviour you want from a retriever. Doc B is partially relevant at 0.50. Doc C is orthogonal at 0.00, correctly judged unrelated even though it is the longest vector in the set.

Now the failure mode. Give doc C the vector [1, 1, 6] instead — barely on topic, but very long:

text
q · c_new = 1(1) + 1(1) + 0(6) = 2          → beats doc B's dot product of 1
‖c_new‖   = √(1 + 1 + 36) = √38 ≈ 6.164
cos(q,c_new) = 2 / (1.414 × 6.164) = 2 / 8.72 ≈ 0.23   → stays below doc B's 0.50

So a raw dot-product ranking promotes the barely relevant long document above the genuinely relevant short one, and cosine keeps the order correct. That is the concrete cost of choosing the wrong metric on unnormalised vectors: long, marginally relevant chunks crowd out short, precisely relevant ones — a real and diagnosable RAG failure.

The same example after L2 normalisation

Normalise every vector to unit length and rerun the dot products. Dividing each vector by its own magnitude:

text
q̂     = [0.707, 0.707, 0    ]     (÷1.414)
â     = [0.707, 0.707, 0    ]     (÷2.828)   — identical to q̂
b̂     = [0.707, 0,     0.707]     (÷1.414)
ĉ_new = [0.162, 0.162, 0.973]     (÷6.164)

Now:

text
q̂ · â     = 0.500 + 0.500 + 0     = 1.00
q̂ · b̂     = 0.500 + 0     + 0     = 0.50
q̂ · ĉ_new = 0.115 + 0.115 + 0     = 0.23

The dot products of the normalised vectors are exactly the cosine similarities of the originals. The pathological ranking is gone, and no cosine formula was evaluated at query time — only a plain inner product. This is the identity from L2 doing real work: normalise once when you index, and the cheapest operation in the database becomes the semantically correct one.

Check the Euclidean claim on the same numbers, using ‖a − b‖² = 2 − 2·cos:

text
q̂ to â:      2 − 2(1.00) = 0.00   → distance 0.00   (rank 1)
q̂ to b̂:      2 − 2(0.50) = 1.00   → distance 1.00   (rank 2)
q̂ to ĉ_new:  2 − 2(0.23) = 1.54   → distance 1.24   (rank 3)

Same order, ascending. Three metrics, one ranking, as promised.

05

Worked example: what a realistic cosine score distribution looks like

A practical trap deserves its own worked case, because it is the one that bites people building their first retrieval system.

In a small toy space, orthogonal means 0.00 and unrelated documents score near zero. In a real embedding space produced by a trained model, unrelated documents frequently score 0.5 to 0.7, and only truly near-duplicate text approaches 0.95. This happens because trained embedding spaces are anisotropic — the vectors occupy a cone rather than spreading evenly over the sphere — so almost everything has some positive alignment with almost everything else.

The consequences are practical and testable as reasoning:

  • There is no universal relevance threshold. "Keep everything above 0.8" is a number that means one thing for one model and something entirely different for another. Thresholds must be calibrated per model on your own data.
  • Ranking is reliable where absolute values are not. The ordering of candidates for a given query is what the metric is good for. The magnitude of the score is a much weaker signal.
  • Comparing scores across models is meaningless. A 0.72 from one embedding model and a 0.72 from another are not commensurable, because the two spaces are unrelated.

Illustrative shape, constructed to make the point rather than measured from any named model:

PairToy-space intuitionRealistic trained-space score
Identical text1.00~1.00
Paraphrase of the query~0.90.85–0.95
Same topic, different focus~0.60.75–0.85
Same domain, different topic~0.20.6–0.75
Entirely unrelated text~0.00.4–0.6
Semantically opposed text~−0.9rarely below 0.2

The last row is the one that surprises people: genuine negative cosine values are rare in practice with modern text embedding models. That does not change the metric's range, which is mathematically −1 to 1 and is what an exam item is asking about. Distinguish the mathematical range from the empirical distribution — quoting "0 to 1" as the range because that is what you observed is the classic wrong answer.

06

Why dot products and cosine similarity are on the NCA-GENL exam

Objective 1.8 is "select and use models to create text embeddings," and objective 1.4 is "curate and embed content datasets for RAGs" — neither is meaningful without knowing how two embeddings get compared. Objective 1.6 names vector databases explicitly, and objective 1.10 names NumPy, where the dot product is the workhorse operation. Core ML is 30% of the blueprint, the largest domain.

Cosine similarity is the term most likely to appear by name, in shapes like:

  • "Which metric compares two text embeddings?" → cosine similarity.
  • "What is the range of cosine similarity?" → −1 to 1. Cheap point, easy to lose.
  • Retrieval scenarios where the answer hinges on similarity search being a nearest-neighbour lookup in vector space.
  • Distractor discrimination: cosine similarity is not a loss function, not an accuracy metric, and not a way to compare two models. It compares two vectors.
  • Attention mechanism items where you need to know the score is a scaled dot product between queries and keys.

How the question tends to be phrased

Four recognisable shapes:

  1. Metric identification. "A retrieval system needs to find documents semantically similar to a query. Which measure is most appropriate?" Keyed: cosine similarity. Distractors: accuracy, cross-entropy, BLEU, F1 — all real metrics from other families.
  2. Range recall. A bare factual item about the interval. Keyed: −1 to 1.
  3. Magnitude reasoning. "Why is cosine similarity generally preferred over the raw dot product for comparing document embeddings?" Keyed: it removes the influence of vector magnitude, so document length does not dominate the score.
  4. Cross-model reasoning. "A team replaces its embedding model with a newer one but keeps the existing index. What is the consequence?" Keyed: the stored vectors and the new query vectors live in different spaces, so similarity scores are meaningless and the corpus must be re-embedded.

What the distractors typically look like

The families you will see are: wrong range (0 to 1, or 0 to ∞); wrong family (offering a loss function or a classification metric where a similarity measure belongs); wrong direction (treating a distance as though bigger were better); and wrong object (claiming cosine similarity compares two models or two datasets rather than two vectors). Each of those is a single-word substitution away from the right answer, which is what makes precision here cheap to earn and cheap to lose.

Calibration, and it is calibration rather than official fact: published candidate reports describe the exam as general-level — know what each thing is and when to use it. NVIDIA publishes no per-item detail. For this material that means the identity statement, the range, and the magnitude decision rule are worth more than any derivation.

07

When to reach for cosine similarity — and when not to

The situationUseWhy
Ranking documents against a query using dense embeddingsCosine (or dot product on normalised vectors)Length is noise; direction is meaning
A vector database index where inner product is the fastest pathDot product on L2-normalised vectorsIdentical ranking to cosine at lower cost
Vectors where magnitude genuinely encodes importance or countDot productYou want the magnitude to count
Query-key scoring inside attentionScaled dot productMagnitude is deliberately retained; scaling is by √d, not by norms
Deduplicating near-identical text chunksCosine with a calibrated high thresholdNear-duplicates cluster tightly in direction
Clustering embeddings (e.g. k-means)Euclidean on normalised vectorsEquivalent to cosine there, and the algorithm expects a distance
Keyword or n-gram overlap between two documentsJaccard or another set measureSparse/lexical comparison, not a dense vector problem
Measuring how wrong a prediction isA loss function — 01-05Similarity is not error
Comparing two models' qualityTask metrics on an eval set — 01-08Similarity compares vectors, never models
Comparing embeddings from two different modelsNothing — it is invalidDifferent spaces; the number has no meaning

The last two rows are the ones that convert directly into exam points, because both are common distractor constructions.

08

Common mistakes with cosine similarity and dot products

MistakeSymptom you would actually observeFix
Quoting the range as 0 to 1You lose a bare-recall itemCosine runs −1 to 1; real models rarely go negative, which is a fact about data, not about the range
Treating a high cosine score as proof of a correct answerRetrieval returns on-topic but non-answering passages, and the model answers from them anywayIt measures topical direction, not factual correctness or answer-hood; add reranking and groundedness checks
Comparing vectors from two different embedding modelsRetrieval quality collapses immediately after a model upgradeThe spaces are unrelated; pin model versions and re-index on any change
Using the raw dot product on unnormalised vectorsLong documents dominate every result list regardless of queryNormalise at index time, then inner product is cosine
Forgetting that Euclidean distance runs the other wayYou sort ascending where descending was correct and get the worst results firstSimilarity: bigger is better. Distance: smaller is better
Assuming a fixed similarity threshold transfersA cutoff tuned on one model admits everything, or nothing, on the nextCalibrate thresholds per model on your own labelled examples
Confusing cosine similarity with Pearson correlationSubtly different numbers in an analysis, and a wrong claim about what was measuredPearson is cosine on mean-centred vectors — related, not identical
Calling it a loss functionYou pick cosine similarity on a "which loss" itemIt is a similarity measure; cross-entropy is the loss, and it is the next lesson
Comparing vectors of different lengthsA dimension-mismatch error, or worse, a silent truncationThe dot product is only defined for equal-length vectors; dimension is a hard compatibility constraint
09

Does a high cosine similarity mean the retrieved document answers the question?

No, and this is the single most consequential limitation to carry into the RAG lessons.

Cosine similarity measures directional agreement between two embeddings. What that agreement tracks is topical relatedness, learned from how the embedding model was trained. A passage can be maximally on-topic and still fail to contain the answer: it might discuss the right subject in the wrong year, state the opposite of the true fact, or be a question rather than an answer about the same subject.

There is a specific and instructive failure here: negation. "The service supports SSO" and "The service does not support SSO" are nearly identical in surface form and often score very high against each other, because embedding models capture topic much more strongly than polarity. A retriever ranking on cosine alone cannot reliably prefer one over the other.

Which is why production retrieval is layered rather than a single similarity call. The standard ladder: dense retrieval by cosine for recall, then a reranker that scores query–passage pairs jointly for precision, then a generation prompt that instructs the model to answer only from the supplied passages, then a groundedness check on the output. Cosine similarity is the first stage — cheap, approximate, and indispensable — not the whole system.

10

Why must you re-embed the whole corpus when you change embedding models?

Because a similarity score is only meaningful between two vectors from the same space, and every embedding model defines its own space.

Two independent reasons compound. The obvious one is dimension: a 768-dimensional stored vector and a 1,536-dimensional query vector cannot even have a dot product taken, because the sum in the formula requires matching components. The subtler and more dangerous one is that even at equal dimensions the axes mean different things. Two models both producing 768 numbers have no shared convention about what the 400th number represents. The arithmetic will succeed and return a plausible-looking number that carries no information.

That second case is dangerous precisely because it fails silently. Nothing errors. Retrieval quality just becomes approximately random, and if you were not measuring it against a fixed evaluation set you might not notice for a long time — which is a direct argument for the discipline in 01-08.

Operational consequences worth stating plainly: pin the embedding model version alongside the index; store the model identifier in the index metadata; treat a model change as a full re-index with a migration plan; and keep an evaluation set that would catch a silent regression. Embedding-model choice is one of the stickiest decisions in a RAG system, and this is why.

11

Why does attention use a raw dot product instead of cosine similarity?

Because in attention, magnitude is signal rather than noise — the opposite of the retrieval case.

Query and key vectors are produced by learned projections inside the model, not by a general-purpose text encoder. The model is free to use the length of a query or key vector to express something: how strongly this position should assert itself in the competition for attention. Normalising every query and key to unit length would throw that expressive capacity away and force all positions to compete on angle alone.

What attention does instead is scale by a constant: divide by √d, where d is the per-head dimension. Note carefully what that is and is not. It is a fixed number determined by the architecture, applied identically to every score. It is not a per-vector normalisation. So the operation retains all relative magnitude information between vectors while keeping the overall scale in a range where softmax produces useful gradients.

The clean contrast to memorise:

Retrieval scoringAttention scoring
Vectors come fromA general text embedding modelLearned Q/K projections inside the model
Magnitude meansMostly document length — noiseLearned salience — signal
Therefore divide byBoth vectors' own norms (cosine)A constant √d (scaled dot product)
Result range−1 to 1Unbounded, then softmaxed
12

Glossary recap: the terms this lesson introduced

TermOne-line definition
VectorAn ordered list of numbers; a point or arrow in high-dimensional space
DimensionHow many numbers a vector has; must match for any comparison
Magnitude / L2 norm√(Σaᵢ²) — the length of the vector
L2 normalisationDividing a vector by its magnitude to get a unit vector
Unit vectorA vector of length exactly 1
Dot product / inner productΣ aᵢbᵢ — one number, unbounded, magnitude-sensitive
Cosine similarityDot product ÷ both magnitudes; −1 to 1; magnitude-free
Cosine distanceUsually 1 − cosine similarity; a distance, so smaller is better
OrthogonalAt right angles; cosine similarity 0; treated as unrelated
Euclidean (L2) distance√Σ(aᵢ−bᵢ)² — a distance, smaller is better
Scaled dot-product attentionAttention scoring: Q·K divided by √d, not by vector norms
AnisotropyThe tendency of trained embedding spaces to occupy a narrow cone, inflating baseline similarity
RerankerA second-stage model scoring query–passage pairs jointly for precision after cosine retrieval
Embedding spaceThe model-specific coordinate system in which its vectors are meaningful
13

Key takeaways on dot products and cosine similarity

  • Dot product = element-wise products summed; one number, unbounded, sensitive to magnitude.
  • Cosine similarity = dot product ÷ both magnitudes; range −1 to 1, magnitude-free, the default for text embeddings.
  • Decision rule: cosine when magnitude is noise (almost always, for text), dot product when magnitude is signal (and inside attention).
  • On L2-normalised vectors, cosine, dot product and Euclidean ranking are identical — the reason pipelines normalise once at index time and use the cheapest operation.
  • Cosine ≈ 1 means same direction, ≈ 0 means unrelated, ≈ −1 means opposite. The mathematical range is −1 to 1 even though trained text models rarely produce negatives.
  • Absolute cosine values are not comparable across models and rarely support a fixed universal threshold; the ranking is what the metric is reliable for.
  • High similarity means topically related, not correct or answering — which is why reranking and groundedness checks exist.
  • Similarity metrics compare two vectors. They never compare two models, never compare two datasets, and are never a loss.
  • Changing embedding models invalidates an existing index, sometimes silently. Pin the version and re-embed.
14

Next: loss functions and cross-entropy

You can now compare two vectors. What you cannot yet do is score a prediction — turn "the model said this and the truth was that" into a single number that training can push downward. Similarity is not error, and the exam tests the difference directly under the objective about comparing models with statistical performance metrics.

Next: 01-05 Loss functions and cross-entropy explained — the loss that scores a next-token distribution against the token that actually came next, and its relationship to perplexity.