All courses
Courses › How LLMs really work · foundation

Dot product and cosine similarity

Compute $a\cdot b$, read it as alignment times lengths, and use cosine similarity to compare vectors.

Builds on: Vectors

▶ Watch this lesson Start the whole course

What the animation says

Do their tastes agree?

Two friends rate the same five films, from minus two, hated it, to plus two, loved it. Each person's ratings are just a list of five numbers. A vector. Do their tastes agree? We want one number that says how much. Language models ask this all the time, about lists with thousands of numbers. One simple move answers it: multiply, then add.

Multiply matching entries

Line up the two lists. Multiply the entries that sit in the same position. Film one: Ana gives two, Ben gives one. Two times one is two. Both liked it, so the product is positive. Film three: minus two times minus one is plus two. Both hated it. That counts as agreement too. When the signs differ, one likes and one hates, so the product is negative. Your turn: tap the film where Ana and Ben disagree.

Then add: one number

Film four gives minus two. Film two: two times two, four. Film five: Ben is neutral, so zero. Now we have five products. That list is not the answer yet. Add them all: two, four, two, minus two, zero. Six. That's the dot product: multiply matching entries, then add. Positive means they mostly agree. Now Ben doubles every rating. Same taste, just louder. What happens to the score?

Length sneaks in: cosine

Twelve. Same tastes, double the score. The dot product grows with length too. Arrows make this visible. a is three, four. b is four, three. Their dot product is twenty-four. Now c: six, eight. Same direction as a, twice as long. Dot product fifty. To keep only direction, divide by both lengths. That's cosine similarity. a with b: twenty-four over twenty-five, zero point nine six. a with c: fifty over fifty, exactly one. One more. u is minus four, three. What is its cosine with a?

Remember

Zero. Three times minus four, plus four times three: minus twelve plus twelve. The arrows are perpendicular. Cosine runs from one, same direction, through zero, perpendicular, to minus one, opposite. So the dot product means multiply matching entries, then add. One number. It equals length times length times alignment. Cosine similarity divides the lengths out. Language models ask this constantly: how much does this list of numbers point the same way as that one?

How do you tell if two vectors agree?

Picture two people's ratings of the same 5 films as vectors. Do their tastes match? Later you will see that a language model turns each word into a long list of numbers. It then constantly asks: *how much does this list point the same way as that one?* One simple operation, multiply-and-add, answers this. It sits at the heart of almost everything that follows.

Multiply matching entries, then add

Pair up the entries of $a$ and $b$ in the same positions and multiply each pair. If both are positive or both negative, the product is **positive**, so they agree there. If the signs differ, the product is **negative**, so they disagree. Add all the products to get one number. It grows when the vectors point the same way **and** when they are long. So it mixes two things: alignment and length.

Dot product and cosine similarity

$$a\cdot b=\sum_{i=1}^{n} a_i b_i=\|a\|\,\|b\|\cos\theta$$ $\|a\|=\sqrt{a_1^2+\dots+a_n^2}$ is the length of $a$. $\theta$ (here just the angle, not model parameters) is the angle between the two arrows. $\cos\theta$, the *cosine* of that angle, is a pure alignment score. It equals $1$ at 0° (same direction), $0$ at 90° (perpendicular) and $-1$ at 180° (opposite), and it falls steadily from $1$ to $-1$ as the angle widens. Dividing out the lengths (both nonzero) gives the **cosine similarity**: $$\cos\theta=\frac{a\cdot b}{\|a\|\,\|b\|}$$

Three comparisons with a = (3, 4)

$\|a\|=\sqrt{9+16}=5$. - $b=(4,3)$: $a\cdot b=12+12=24$, $\|b\|=5$, cosine $=24/25=0.96$. Nearly the same direction. - $c=(6,8)$: $a\cdot c=18+32=50$, $\|c\|=10$, cosine $=50/50=1$. Same direction, twice as long. - $u=(-4,3)$: $a\cdot u=-12+12=0$, cosine $=0$. Perpendicular. $c$ has about twice the dot product with $a$ that $b$ has, yet their cosines are almost equal (1 vs 0.96). The extra comes mostly from length.

Remember

Dot product = multiply matching entries and add = (length of $a$) × (length of $b$) × (alignment). Cosine similarity divides out the lengths, which leaves only alignment: $1$ same way, $0$ perpendicular (no alignment), $-1$ opposite.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.