Math & Statistics

Dot Product & Cosine Similarity

Measuring vector alignment, magnitude projection, and semantic similarity in embedding spaces.

🟢 beginner4 min readlinear-algebraembeddings
Dot Product and Cosine Similarity are the two core metrics for comparing vectors in machine learning. Dot product u · v = ∑ u_i v_i = ||u|| ||v|| cos(θ) combines both directional alignment and vector magnitude. Cosine similarity normalizes by vector lengths, measuring purely directional angle alignment cos(θ) on a [-1, 1] scale. When vectors are pre-normalized to unit length (||u|| = 1), Dot Product and Cosine Similarity become 100% mathematically identical.

Dot Product vs Cosine Similarity

The Dot Product multiplies corresponding elements and sums the results:

u . v = sum( u_i * v_i ) = ||u|| * ||v|| * cos(theta)

The Cosine Similarity isolates the angle theta by dividing out vector lengths:

cos(theta) = ( u . v ) / ( ||u|| * ||v|| )

┌──────────────────────────┬──────────────────────────┐
│  1. DOT PRODUCT          │  2. COSINE SIMILARITY    │
├──────────────────────────┼──────────────────────────┤
│ Combines angle AND length│ Measures PURE angle      │
│ Range: (-inf, +inf)      │ Range: [-1, 1]           │
│ Favors long vectors      │ Scale-invariant          │
└──────────────────────────┴──────────────────────────┘

Why Length Normalization Saves GPU Time

In vector databases and recommendation systems, calculating Cosine Similarity over millions of vectors requires dividing by vector norms millions of times.

Smart engineering fix: Pre-normalize all vectors to unit length (||u|| = 1) during data ingestion.

When vectors have unit length:

Cosine Similarity = u . v / (1 * 1) = u . v

Normalizing vectors upfront converts slow similarity math into lightning-fast GPU matrix multiplications.

When to Use Which Metric

  1. Document Classification (TF-IDF): Use Cosine Similarity. Longer documents naturally contain more words (larger vector magnitude), but should still match short documents with identical word proportions.
  2. Matrix Factorization (RecSys): Use Dot Product. Vector magnitude carries meaningful user interaction intensity or item popularity signal.
  3. Deep Embeddings (Sentence Transformers): Pre-normalize vectors and use Dot Product for sub-10ms vector database retrieval.

Say this out loud

Dot product measures vector alignment weighted by length. Cosine similarity isolates directional angle by dividing by vector magnitudes, scaling results from -1 to +1. When vectors are pre-normalized to unit length, Dot Product becomes identical to Cosine Similarity, enabling fast hardware dot products in vector databases.

Follow-ups to expect

Check yourself

Question 1 of 3

What is the key mathematical difference between Dot Product and Cosine Similarity?

More in Math & Statistics

See all →
Bayes’ Theorem4 minCentral Limit Theorem4 minLaw of Large Numbers4 min