Cosine similarity

Linear algebra · Formula · Appears in 4 lessons

sim⁡(a,b)=a⋅b∥a∥ ∥b∥\operatorname{sim}(\mathbf{a}, \mathbf{b}) = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\|\,\|\mathbf{b}\|}

The dot product with length divided out, leaving only the angle between two vectors. It runs from -1 to 1 and ignores magnitude entirely, which is exactly what you want when comparing embeddings whose scale carries no meaning.

Symbols

a\mathbf{a}
The first vector, often a query embedding.
b\mathbf{b}
The second vector, often a stored embedding.
Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark