Lesson 2 of 3
The dot product
One small operation, multiply matching entries and add them up, measures how much two vectors point the same way. It is the most used calculation in machine learning.
Compute a dot product by hand for vectors of any length.
Explain what the sign and size of a dot product say about two arrows.
Derive why from the law of cosines.
Project one vector onto another, and write the code for it.
Here is a small recommender. You describe your taste in movies with three numbers: how much you like action, romance and comedy. Say your taste is : you like action, dislike romance, and love comedy. A movie gets the same three numbers for how much of each it contains, say .
How good a match is this movie? A sensible score: for each feature, multiply how much you care by how much the movie has, then add it all up.
The romance term counts against the movie because you dislike romance. The comedy term helps the most because you care about comedy most. That multiply-then-add score is the dot product, and by the end of this lesson you will see it everywhere in machine learning: inside every neuron, inside attention, and inside every embedding search.
Multiply matching entries, then add
For two vectors of the same length , the dot product is
The symbol just means "add up the following for ". Three things to notice:
- The answer is one number, not a vector. Two lists go in, a single score comes out.
- Both vectors must have the same length. There is no way to pair up entries otherwise. In code, a mismatch here is one of the most common errors you will see.
- Order does not matter: , because .
A second property matters more than it looks. The dot product is linear in each argument: scaling a vector scales the score, and splitting a vector splits the score.
Double your enthusiasm for every genre and every score doubles. You will lean on this property constantly once we reach matrices, which are nothing more than many dot products at once.
Compute each dot product by hand. Write every product before adding.
Use real paper. Writing each step by hand is the point of this one.
What the number means
The algebra says nothing about arrows. But a 2D vector is also an arrow from the origin, and the dot product turns out to measure something visual. Drag the two arrows below and watch the number.
Play with it until these three facts feel obvious:
- When the arrows point roughly the same way, the dot product is positive. The angle between them is under 90°.
- When they are perpendicular, it is exactly zero. Rotate until the shadow shrinks to a point.
- When they point in roughly opposite directions, it is negative. The shadow lands on the far side of the origin.
And the size of the number grows with both lengths. The formula that captures all of this is
where is the length of and is the angle between the arrows. The cosine is at , at and at , which is exactly the sign behavior you just saw.
It is not obvious that these two formulas agree. Multiplying coordinates and adding them has no visible angle in it. Here is why they must be the same.
Go slower: Why the two formulas agree
Draw and from the origin. The third side of that triangle is the vector .
Step 1, with geometry. The law of cosines says that for any triangle with sides of length and and angle between them, the third side satisfies
Step 2, with algebra. The squared length of any vector is its dot product with itself: . So expand using linearity:
Step 3, compare. Both lines describe the same length. Cancel from each and divide by :
Nothing in this argument used two dimensions. It works for vectors with 3 entries or 4,096 entries, which is how we can talk about "the angle" between two word embeddings that live in spaces we cannot picture.
Let and . Without drawing them, what can you say about the angle between them?
The shadow: projection
The widget's green shadow has a precise meaning. Shine a light straight down onto the line through . The shadow that casts on that line has signed length
The second form comes from dividing the key formula by . So another way to say what the dot product is: the length of 's shadow on , times the length of . If has length 1, the dot product is the shadow length. That is why we so often normalize vectors to length 1 before comparing them, which is the subject of the next lesson.
To get the shadow as a vector, rather than just its length, walk that far along the unit direction :
The last form needs no square roots, which is how you should compute it by hand.
Find for and .
Projection splits into two pieces: the part along , and the leftover .
- Prove that for any and any nonzero . Use only linearity.
- Check it on , by computing .
Use real paper. Writing each step by hand is the point of this one.
Where this shows up
You now know the operation that most of a neural network's time is spent on.
The numbers in these systems have hundreds or thousands of entries, but the operation is exactly the one you did on paper.
Now write it yourself. Python lists and a loop first, then numpy.
Implement dot(a, b) with a plain loop over Python lists (no numpy inside it), and raise ValueError if the lengths differ. Then implement project(a, b), which returns the projection of a onto the line through b as a list. Reuse your dot.
Compute for and .
Without formulas, explain to a friend why the dot product of two perpendicular arrows is zero, and what a negative dot product tells you. Use the shadow picture.
Your explanation stays in this browser.