Free preview
64 lessons
Essential Linear Algebra for ML
Free preview
Essential Linear Algebra for ML · 64 lessons
No surprise gaps
Actually remember it
Skip what you know
One subscription. All learning paths included.
Our content is best on a larger screen
The dot product
We have added vectors and scaled them. There is also a way to multiply two vectors together, and the answer is a single number. It is called the dot product.
Definition
For , the dot product is
Each component of is multiplied by the component of in the same position, and the products are added.
In more general settings, where the objects being multiplied need not be columns of numbers, a product of this kind is called an inner product.
Let and .
Calculate .
Solution
Three of the four products are negative, since in those positions the paired components have opposite signs:
The result is the single real number , not a vector.
Practice questions
5 questions
Let and .
Calculate .
Select the correct answer:
+ 4 more questions
What the dot product says about the angle
Computing a dot product from components says nothing about what the result represents. The same number can also be computed from the lengths of the two vectors and the angle between them. Because the two always agree, the lengths and the angle explain what the components compute.
Theorem
For non-zero ,
where is the angle between the two vectors placed tail to tail at the origin, taken between and .
Key Point
The size of a dot product depends on both the lengths and the angle. Two short vectors pointing the same way can give a smaller number than two long ones at a wide angle.
Lengths are always positive, so the dot product and always have the same sign. The diagram below is two-dimensional, but the result holds in any number of dimensions.
Drag either arrowhead. Pulling it further out changes that length alone; swinging it round changes the angle alone.
The angle is taken to be the smaller of the two angles the vectors form, so . The sign of the dot product tells us what kind of angle we have, and the two ends of that range tell us how large the number can get.
| The two vectors | |||
|---|---|---|---|
| point the same way | |||
| acute, below | positive | positive | point broadly the same way |
| are at a right angle | |||
| obtuse, above | negative | negative | point broadly opposite ways |
| point opposite ways |
In particular, taking to be itself puts the angle at , so a vector dotted with itself gives its length squared.
Practice questions
4 questions
The non-zero vectors and have lengths and , and the angle between them is , where .
What is ?
Select the correct answer:
+ 3 more questions
The cosine of the angle
We've seen that the dot product of two non-zero vectors is the product of their two lengths and the cosine of the angle between them. Both lengths are non-zero, so we can divide by them, which expresses the cosine of the angle in terms of the dot product and the two lengths.
Theorem
For non-zero ,
where is the angle between and .
Every quantity on the right comes from the components, so the cosine does too.
Making either vector longer multiplies both the dot product and that vector's length by the same factor, so the cosine does not move. It measures direction and nothing else, which is what lets two vectors of very different sizes be compared.
ML Context
In deep learning, a piece of text, an image or a sound is often represented by a vector of numbers called an embedding, learned so that similar inputs are given vectors pointing in similar directions.
Two embeddings are usually compared with the cosine of the angle between them, called cosine similarity: it is how a search engine ranks documents against a query, and how a recommender system finds things close to what someone already chose.
Dividing by the two lengths is deliberate, because the size of an embedding can reflect incidental things such as how long the input was, and that should not decide whether two inputs count as alike.
Two drones are tracked, each velocity recorded in metres per second to the east, to the north and upwards.
The first has velocity and the second has velocity .
Find , where is the angle between the two velocities.
Solution
The cosine needs three numbers: the dot product and each of the two lengths.
We multiply matching components and add the results:
Each length comes from the squares of its components, and the first square root simplifies because :
The same calculation for gives a square root that simplifies in the same way, since :
Substituting all three into the cosine, both lengths carry a factor of , so their product is rational: .
which is about .
The cosine is negative, so the angle between the two velocities is obtuse: the drones are heading in broadly opposite directions.
Practice questions
4 questions
Two short articles are each summarised by how many times four particular words appear in them.
The counts for the first article are and the counts for the second are . Let be the angle between these two vectors.
What is ?
Select the correct answer:
+ 3 more questions