Vector Similarity
Goal
Compute dot product and cosine similarity, explain the role of vector magnitude, and use one consistent similarity contract for indexing and querying.
Before the formulas, picture arrows on graph paper. Two arrows can point in the same direction while one is much longer. A scoring rule can care about both direction and length, or it can compare mostly the direction after normalizing the lengths.
That is the intuition behind the two scores in this Lesson. Dot product is influenced by alignment and magnitude. Cosine similarity divides out vector length and compares direction more directly. The tiny two-dimensional vectors below let you see that difference with arithmetic before applying the same rule to high-dimensional retrieval embeddings.
Remember from L5.4 — Dot Products as Similarity: a dot product multiplies matching vector components and adds them. Here you reuse that same operation for retrieval, then compare it with cosine similarity.
After embedding a query and document chunks, a retriever needs a score. Two common choices are:
- dot product;
- cosine similarity.
They are related but not identical.
Dot product mixes alignment and magnitude
Take:
q = [1, 1]
d1 = [1, 1]
d2 = [10, 10]
Dot products:
q·d1 = 2
q·d2 = 20
Both document vectors point in the same direction, but d2 has larger magnitude. So raw dot product rewards both alignment and scale. That may be intended if the embedding model was trained for dot-product retrieval.
Cosine similarity normalizes magnitude
Cosine similarity is:
cos(q,d) = (q·d) / (||q|| ||d||)
For the two vectors above, both cosines are 1 because their directions match exactly. Cosine asks more directly about angle/direction. For:
q = [1,0]
d3 = [0,1]
the dot product is 0 and cosine is 0.
Normalized vectors make dot and cosine agree
If every vector has unit norm:
||q|| = 1
||d|| = 1
then:
q·d = cos(q,d)
Some systems normalize embeddings first and then use inner-product search. The important rule is consistency. Do not build an index assuming normalized cosine-style vectors and then query with unnormalized vectors under another score.