Dot Product vs Cosine Similarity
You'll learn when to use dot product versus cosine similarity for comparing embeddings, and why normalization matters.
The Core Difference
Both dot product and cosine similarity measure how "aligned" two vectors are, but they handle magnitude differently:
- Dot product considers both direction AND magnitude (length) of vectors
- Cosine similarity only considers direction, ignoring magnitude
Think of it like comparing arrows: cosine similarity asks "are they pointing the same way?" while dot product asks "are they pointing the same way AND are they similarly sized?"
When to Use Each
Use cosine similarity when:
- Vector magnitudes vary wildly (documents of different lengths)
- You want pure semantic similarity regardless of "strength"
- Your embeddings aren't normalized
Use dot product when:
- Your embeddings are already normalized (unit vectors)
- Magnitude carries meaningful information (term frequency, confidence scores)
- Speed matters (dot product is computationally faster)
The Normalization Connection
Here's the key insight: for normalized vectors (length = 1), dot product and cosine similarity give identical results!
Many modern embedding APIs (like OpenAI's) return pre-normalized embeddings specifically so you can use the faster dot product without losing the benefits of direction-only comparison.
If your vectors aren't normalized, cosine similarity adds a normalization step internally, which is why it's slower but more robust.
Key Takeaway: Use cosine similarity for general-purpose semantic search with unnormalized embeddings; switch to dot product for speed when working with normalized embeddings, where both measures are equivalent.