Part I ยท Ch. 2 โ€” Measuring Similarity

Part I ยท Chapter 2 of 20

Measuring Similarity

The dot product: one number that says how much two arrows agree


One number for agreement

How similar are "cat" and "dog"? Your gut answers before you finish the question. A machine has no gut. But this site is quietly heading somewhere specific: soon every word will become an arrow (that is the payoff we build toward in Part II). And once words are arrows, the fuzzy question "how similar?" turns into a sharp geometric one: do two arrows point roughly the same way?

Let's set the target precisely before we compute anything. We want one number that is positive when two arrows agree in direction, zero when they are unrelated, and negative when they oppose โ€” one number the machine can compare, rank, and sort. Here is that number's behavior, in a picture, before a single symbol.

Three small plane panels. In each, the same mint arrow u points up and to the right to (2,1). Panel 'agree': an amber arrow points the same way, longer, to (4,2); below it, u dot v = 10. Panel 'unrelated': an amber arrow points up-left to (โˆ’1,2) with a right-angle marker at the origin; u dot v = 0. Panel 'oppose': an amber arrow points down-left to (โˆ’2,โˆ’1); u dot v = โˆ’5.
One number, three verdicts. The same mint arrow meets three partners: pointing with it (positive), at a right angle to it (exactly zero), or against it (negative). The dot product is the machine version of the judgment your eye just made.

The operation that produces that number is the dot product, and let me be honest about its importance up front: it is the single most important computation on this entire site. Attention, embeddings, similarity search โ€” the whole machinery of a model reading meaning is this one move, repeated billions of times a second. Learn it well here and half of what is coming will feel like review.

The recipe: multiply matching parts, add

Meet the two arrows we will use for the rest of the chapter โ€” the same pair runs through every figure, the video, and the widget, so it is worth fixing them in mind now. Our first arrow is $\mathbf{u} = \begin{bmatrix} 2 \\ 1 \end{bmatrix}$, drawn in mint; it points shallow and to the right. Our second is $\mathbf{v} = \begin{bmatrix} 1 \\ 3 \end{bmatrix}$, drawn in amber; it points steep and to the right. Picture them: both lean the same general way, up and to the right, so we should expect them to agree.

Here is the recipe in words, before any notation: multiply the two across-parts together, multiply the two up-parts together, then add the two results. That's it. We write the operation with a centered dot, $\mathbf{u} \cdot \mathbf{v}$, read aloud as "u dot v." Let's turn the handle on our pair, showing every digit:

$$\mathbf{u} \cdot \mathbf{v} = \begin{bmatrix} 2 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 3 \end{bmatrix} = 2 \times 1 + 1 \times 3 = 5$$

In words: multiply the across-parts (2 and 1) to get 2, multiply the up-parts (1 and 3) to get 3, and add them: 2 plus 3 is 5. The answer is a single positive number โ€” the arrows agree, exactly as our eyes predicted.

In general, for any two 2D vectors, the recipe reads:

$$\mathbf{u} \cdot \mathbf{v} = u_1 v_1 + u_2 v_2$$

In words: multiply the first components together, multiply the second components together, and add โ€” matching slot with matching slot, never mixing across.

Let's test the promise from the opening โ€” positive for agree, zero for unrelated, negative for oppose โ€” on two more cases you can check by hand.

Worked example

Take $\mathbf{w} = \begin{bmatrix} -1 \\ 2 \end{bmatrix}$, an arrow pointing up and to the left. Dot it with our mint arrow: $\mathbf{u} \cdot \mathbf{w} = 2 \times (-1) + 1 \times 2 = -2 + 2 = 0$. Exactly zero โ€” $\mathbf{w}$ sits at a right angle to $\mathbf{u}$, neither agreeing nor opposing. Now flip $\mathbf{u}$ around to get $\begin{bmatrix} -2 \\ -1 \end{bmatrix}$, pointing the opposite way: $2 \times (-2) + 1 \times (-1) = -4 - 1 = -5$. As negative as this pair of lengths allows โ€” maximal disagreement.

Partner of u = [2, 1]u ยท (partner)verdict
[4, 2] โ€” same way, longer+10agree
[1, 3] โ€” leans the same way+5agree
[โˆ’1, 2] โ€” right angle0unrelated
[โˆ’2, โˆ’1] โ€” reversedโˆ’5oppose

Those middle-to-bottom rows are the three panels of the figure above, now with their arithmetic filled in.

But why should "multiply matching slots and add" measure agreement at all? Here is the honest intuition. Think of each matching pair of components as casting a vote. If both parts have the same sign โ€” both positive, or both negative โ€” their product is positive, a vote that the arrows push the same way along that axis. Opposite signs multiply to a negative, a vote for disagreement. And big aligned components cast big votes. The sum tallies the election. That is a real intuition, not a proof; the geometric view in a moment will make it airtight.

How long is an arrow?

We owe a debt from the last chapter: we kept saying an arrow has a length but never computed one. Time to pay up โ€” and it turns out length and the dot product are the same idea in different clothes, so this is the perfect moment. Picture first.

A grid in the first quadrant. A mint arrow runs from the origin to (3,4). A dashed horizontal leg along the bottom is labeled 3; a dashed vertical leg up to the tip is labeled 4; a small right-angle marker sits at (3,0). Along the arrow, the label reads: length of v equals the square root of (3 squared plus 4 squared) equals 5.
Length is Pythagoras wearing vector clothes. The component lines are the legs of a right triangle, so the arrow's length is $\sqrt{3^2 + 4^2} = 5$.

Look at the arrow to the point (3, 4). Its two dashed component lines are the legs of a right triangle, and the arrow itself is the hypotenuse. That is Pythagoras, which many of us met in school: the square of the hypotenuse equals the sum of the squares of the legs. So $3^2 + 4^2 = 9 + 16 = 25$, and the length is the square root, $\sqrt{25} = 5$. We write the length of a vector with double bars, $\|\mathbf{v}\|$, read aloud as "the length of v." In general:

$$\|\mathbf{v}\| = \sqrt{v_1^2 + v_2^2}$$

In words: square each component, add the squares, and take the square root โ€” the straight-line distance from the arrow's tail to its tip.

Now the lengths of our two stars. Keeping every number honest:

$$\|\mathbf{u}\| = \sqrt{2^2 + 1^2} = \sqrt{5} \approx 2.2 \qquad \|\mathbf{v}\| = \sqrt{1^2 + 3^2} = \sqrt{10} \approx 3.2$$

In words: our mint arrow has length root-five, a little over 2.2; our amber arrow has length root-ten, a little over 3.2.

Notice these do not come out to tidy whole numbers, and that is completely fine. We carry them along as $\sqrt{5}$ and $\sqrt{10}$ โ€” exact, honest โ€” and only round to a decimal at the very end, when a human wants to read the answer.

The shadow view

The dot product has a second face, and seeing it is where the whole idea clicks into place. Picture first. Shine a light straight down onto the line our mint arrow lies along; the amber arrow casts a shadow onto that line. The dot product turns out to equal the length of that shadow times the length of the mint arrow โ€” agreement, in this view, is how much of one arrow lies along the other's direction.

A plane with the mint arrow u to (2,1) and a dashed thin line extending its direction. The amber arrow v points to (1,3). A dashed amber line drops perpendicularly from the tip of v onto u's line, meeting it at a dot at (2,1). A thick amber segment from the origin to that foot marks the shadow. A muted arc near the origin is labeled theta equals 45 degrees, and an annotation points to the shadow reading 'shadow length approximately 2.24'.
The second face of the dot product. Drop v's shadow onto u's line: $\mathbf{u} \cdot \mathbf{v}$ is the shadow's length times u's length โ€” here about $2.24 \times 2.24 \approx 5$, the same 5 the component recipe gave.

To write this down we need the angle between the two arrows. We call it $\theta$ โ€” theta, the Greek letter โ€” and it measures how far apart the two directions are.

And here is one tool from school I am going to hand you rather than rebuild: the cosine. For everything on this site it is just a machine. Feed it the angle between two directions, and it hands back one number between $-1$ and $+1$ saying how aligned they are. Feed it 0ยฐ โ€” the same direction โ€” and it hands back exactly 1. Feed it 90ยฐ โ€” a right angle โ€” and it hands back exactly 0. Feed it 180ยฐ โ€” dead opposite โ€” and it hands back $-1$. In between it slides smoothly: at 45ยฐ, halfway to a right angle, it reads about 0.71. That behavior โ€” 1, fading to 0, falling to $-1$ โ€” is the only fact about the cosine this entire site ever uses. Pocket those four checkpoints and you own the tool.

A mint curve on axes labeled 'angle you feed in' along the bottom (marked 0, 45, 90, 135 and 180 degrees) and 'number it hands back' up the side (marked plus 1, 0 and minus 1). The curve starts at plus 1 above 0 degrees, slides down through 0 at 90 degrees and reaches minus 1 at 180 degrees. Four amber dots mark it: 0 degrees goes to 1, same direction; 45 degrees goes to 0.71; 90 degrees goes to 0, a right angle; and 180 degrees goes to minus 1, dead opposite. A faint dashed line marks the zero level.
The cosine as a machine, read off its outputs. Angle in, one number between $-1$ and $+1$ out. The four amber checkpoints are the whole of what this site asks you to remember; the smooth slide between them is why cosine makes a usable score.

With that machine in hand, the shadow's length works out to $\|\mathbf{v}\| \cos\theta$ (the cosine of the angle scales the full length down to just the part lying along $\mathbf{u}$), which gives the dot product's second form:

$$\mathbf{u} \cdot \mathbf{v} = \|\mathbf{u}\| \, \|\mathbf{v}\| \cos\theta$$

In words: multiply the two lengths together, then scale the result by $\cos\theta$ โ€” a number that says how aligned the two directions are.

Let's check this against the component recipe on our stars โ€” this is the satisfying part. I picked these two arrows so that the angle between them comes out to exactly 45ยฐ, chosen on purpose to keep the arithmetic clean. That one step you will have to take on trust โ€” my protractor work โ€” because reading an arrow's angle back out of its coordinates needs a tool called the arctangent, and we deliberately never build that one here. For the record, the mint arrow $\mathbf{u} = [2, 1]$ sits at about 26.6ยฐ above the horizontal, the amber arrow $\mathbf{v} = [1, 3]$ sits at about 71.6ยฐ, and $71.6ยฐ - 26.6ยฐ = 45.0ยฐ$. Everything after that you can check, because 45ยฐ is one of the four checkpoints we just pocketed: its cosine is about 0.71. So the shadow formula gives $\|\mathbf{u}\| \, \|\mathbf{v}\| \cos\theta = \sqrt{5} \times \sqrt{10} \times 0.71 \approx 2.24 \times 3.16 \times 0.71 \approx 5.0$. The component recipe said 5. Two recipes that look nothing alike, landing on the same number โ€” and they always will.

One quirk of these particular numbers, worth flagging so you don't read a false rule into it: the shadow happens to end exactly at the tip of $\mathbf{u}$, the point (2, 1). That's only because the shadow's length, $\sqrt{5} \approx 2.24$, equals $\|\mathbf{u}\|$, which is also $\sqrt{5}$ โ€” a coincidence of the numbers we picked, not a general rule. Usually the foot lands somewhere else along the line entirely.

Now we can cash out the whole sign story geometrically. When the angle is less than 90ยฐ, $\cos\theta$ is positive, the shadow falls forward along $\mathbf{u}$, and the dot product is positive. At exactly 90ยฐ the shadow collapses to a single point of zero length, and the dot product is zero. Past 90ยฐ the shadow falls backward, $\cos\theta$ goes negative, and so does the dot product. That is precisely why the component recipe measured agreement all along.

Cosine similarity: agreement, pure

There is a flaw in the raw dot product if what we truly want is direction-agreement: it grows with length, not just alignment. Double the amber arrow without turning it at all and the dot product doubles too โ€” $\mathbf{u} \cdot 2\mathbf{v} = 2 \times 2 + 1 \times 6 = 10$ โ€” even though the directions never budged. If length changes the score while the directions stay fixed, then length is contamination.

The fix is a single division. Rearrange the shadow formula to put $\cos\theta$ by itself โ€” divide the dot product by both lengths โ€” and the length dependence cancels clean away:

$$\cos\theta = \frac{\mathbf{u} \cdot \mathbf{v}}{\|\mathbf{u}\| \, \|\mathbf{v}\|} = \frac{5}{\sqrt{5} \times \sqrt{10}} \approx 0.71$$

In words: take the dot product and divide out both lengths. What remains is the cosine of the angle โ€” pure alignment, with size stripped away.

For our pair, $\cos\theta = 5 / (\sqrt{5} \times \sqrt{10}) = 5 / \sqrt{50} \approx 0.71$. And now the scale is universal: $+1$ means identical directions, $0$ means unrelated, $-1$ means exactly opposite. Those are the same three checkpoints we pocketed a page ago, now doing real work โ€” and the reading holds no matter how long the vectors are.

A horizontal ruler from โˆ’1 to +1 with ticks at โˆ’1, โˆ’0.5, 0, +0.5, +1. Above โˆ’1, a pair of arrows point in opposite directions, labeled 'opposite'. Above 0, two arrows at a right angle, labeled 'unrelated'. Above +1, two arrows pointing the same way, labeled 'identical'. A mint marker dot sits at +0.71, labeled 'our pair: 0.71'.
One fixed ruler for every possible pair of directions. Cosine similarity pins every comparison between $-1$ and $+1$. Our u and v land at 0.71 โ€” solidly "pointing the same general way" โ€” and no amount of stretching either arrow can move that mark.

This quantity has a name we will use constantly: cosine similarity. When you hear that embeddings are compared "by cosine similarity," it means literally this fraction โ€” the same one we just computed โ€” evaluated on arrows with thousands of components instead of two. Nothing about the recipe changes; the lists just get longer.

See it move

Two things are far easier to feel than to read: that the number climbs and falls smoothly as one arrow swings, flipping sign exactly at the right angle, and that the shadow view and the component view land on the very same 5. The short video puts both in motion.

Watch (1:29): what to notice โ€” the live number as the amber arrow swings, the sign flipping exactly at the right angle, and the shadow view landing on the same 5 the component recipe produced.

Now take the controls. Drag either arrow's tip and watch the panel recompute the actual arithmetic โ€” with the current digits, not a canned answer โ€” alongside both lengths and the cosine on its $-1$-to-$+1$ gauge. Flip on the shadow to see the projection you just met.

Why the LLM cares

When a model reads a sentence and has to decide how much the word "it" should pay attention to the word "cat" mentioned earlier, it answers by computing a dot product between two vectors โ€” one standing for "it," one for "cat." Every attention score, in every layer, is a dot product measuring how much two words agree. We build that machine in full in Chapter 10, Attention; the arithmetic will be exactly what you just learned.

The second use is search. "Find me the passage most relevant to this question" becomes "find the stored vector with the highest cosine similarity to the question's vector" โ€” the same fraction from a few paragraphs ago, run across a whole library. That is what powers retrieval and the systems people call RAG, which we reach in Chapter 19.

And the scale is less mysterious than it sounds. Inside a real model the recipe is identical โ€” multiply matching slots, add โ€” just with the number of components $d$ in the thousands instead of two. Modern hardware exists in large part because this one operation had to be made fast at enormous scale; when you hear that "GPUs multiply matrices," the next chapter shows you that even that is dot products in bulk. Back at the start of Part I we said that direction is information โ€” the dot product is how a machine reads it.

What you now know

  • The dot product turns two vectors into one number: multiply matching components and add, so $\mathbf{u} \cdot \mathbf{v} = u_1 v_1 + u_2 v_2$ โ€” for our stars, $2 \times 1 + 1 \times 3 = 5$.
  • That one number reads agreement: positive when arrows point the same general way, exactly zero at right angles (unrelated), and negative when they oppose.
  • A vector's length comes from Pythagoras on its own components, $\|\mathbf{v}\| = \sqrt{v_1^2 + v_2^2}$, and dotting a vector with itself gives its length squared.
  • The dot product has a second, geometric face โ€” shadow length times arrow length, $\|\mathbf{u}\| \, \|\mathbf{v}\| \cos\theta$ โ€” and the two faces always produce the same number.
  • Dividing the lengths out leaves cosine similarity, $\cos\theta = \mathbf{u} \cdot \mathbf{v} / (\|\mathbf{u}\| \, \|\mathbf{v}\|)$: a pure direction-agreement score locked between $-1$ and $+1$ that stretching cannot fool.
  • Attention scores and embedding search are, at bottom, this chapter's arithmetic performed on much longer lists.

Where we're headed. You can now measure how much two arrows agree. Next come machines that change arrows: a matrix takes a vector in and hands a different vector back โ€” rotated, stretched, remixed. And here is the teaser that makes it friendly: a matrix does its work entirely with dot products, one per row. When people say a model has "billions of weights," they mean the entries of these grids. Chapter 3 opens the machine.