Free preview

64 lessons

Essential Linear Algebra for ML

Free preview

Essential Linear Algebra for ML · 64 lessons

Master the linear algebra that powers modern ML

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

The dot product

Explanation

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.

The dot product

Definition

For v⃗,w⃗∈Rn\vec{v}, \vec{w} \in \mathbb{R}^n, the dot product v⃗⋅w⃗\vec{v} \cdot \vec{w} is

v⃗⋅w⃗=v1w1+v2w2+⋯+vnwn.\vec{v} \cdot \vec{w} = v_1 w_1 + v_2 w_2 + \cdots + v_n w_n.

Each component of v⃗\vec{v} is multiplied by the component of w⃗\vec{w} in the same position, and the nn 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.

Example

Let v⃗=[3−14−2]\vec{v} = \begin{bmatrix} 3 \\ -1 \\ 4 \\ -2 \end{bmatrix} and w⃗=[56−23]\vec{w} = \begin{bmatrix} 5 \\ 6 \\ -2 \\ 3 \end{bmatrix}.

Calculate v⃗⋅w⃗\vec{v} \cdot \vec{w}.

Solution

Three of the four products are negative, since in those positions the paired components have opposite signs:

v⃗⋅w⃗=[3−14−2]⋅[56−23]=3(5)+(−1)(6)+4(−2)+(−2)(3)=15+(−6)+(−8)+(−6)=−5.\begin{align*} \vec{v} \cdot \vec{w} &= \begin{bmatrix} 3 \\ -1 \\ 4 \\ -2 \end{bmatrix} \cdot \begin{bmatrix} 5 \\ 6 \\ -2 \\ 3 \end{bmatrix} \\ &= 3(5) + (-1)(6) + 4(-2) + (-2)(3) \\ &= 15 + (-6) + (-8) + (-6) \\ &= -5. \end{align*}

The result is the single real number −5-5, not a vector.

Practice questions

5 questions

Let v⃗=[43]\vec{v} = \begin{bmatrix} 4 \\ 3 \end{bmatrix} and w⃗=[62]\vec{w} = \begin{bmatrix} 6 \\ 2 \end{bmatrix}.

Calculate v⃗⋅w⃗\vec{v} \cdot \vec{w}.

Select the correct answer:

+ 4 more questions

What the dot product says about the angle

Explanation

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.

The dot product and the angle between two vectors

Theorem

For non-zero v⃗,w⃗∈Rn\vec{v}, \vec{w} \in \mathbb{R}^n,

v⃗⋅w⃗=∥v⃗∥∥w⃗∥cos⁡(θ),\vec{v} \cdot \vec{w} = \|\vec{v}\| \|\vec{w}\| \cos(\theta),

where θ\theta is the angle between the two vectors placed tail to tail at the origin, taken between 0∘0^\circ and 180∘180^\circ.

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 cos⁡(θ)\cos(\theta) always have the same sign. The diagram below is two-dimensional, but the result holds in any number of dimensions.

1234−1−2−3−41234−1−2−3−4
v→
w→
θ

Drag either arrowhead. Pulling it further out changes that length alone; swinging it round changes the angle alone.

‖v→‖=4.12×‖w→‖=3.16×cos⁡(58∘)=0.537=v→⋅w→=7.00
oppositeright anglesame direction
An acute angle, so the dot product is positive
The components give the same number: (4.0)(1.0)+(1.0)(3.0)=7.00.

The angle θ\theta is taken to be the smaller of the two angles the vectors form, so 0∘≤θ≤180∘0^\circ \leq \theta \leq 180^\circ. 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.

θ\thetacos⁡(θ)\cos(\theta)v⃗⋅w⃗\vec{v} \cdot \vec{w}The two vectors
0∘0^\circ11∥v⃗∥∥w⃗∥\lVert\vec{v}\rVert \lVert\vec{w}\rVertpoint the same way
acute, below 90∘90^\circpositivepositivepoint broadly the same way
90∘90^\circ0000are at a right angle
obtuse, above 90∘90^\circnegativenegativepoint broadly opposite ways
180∘180^\circ−1-1−∥v⃗∥∥w⃗∥-\lVert\vec{v}\rVert \lVert\vec{w}\rVertpoint opposite ways

In particular, taking w⃗\vec{w} to be v⃗\vec{v} itself puts the angle at 0∘0^\circ, so a vector dotted with itself gives its length squared.

Practice questions

4 questions

The non-zero vectors v⃗\vec{v} and w⃗\vec{w} have lengths ∥v⃗∥=8\|\vec{v}\| = 8 and ∥w⃗∥=3\|\vec{w}\| = 3, and the angle between them is 60∘60^\circ, where cos⁡(60∘)=12\cos(60^\circ) = \dfrac{1}{2}.

What is v⃗⋅w⃗\vec{v} \cdot \vec{w}?

Select the correct answer:

+ 3 more questions

The cosine of the angle

Explanation

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.

The cosine of the angle between two vectors

Theorem

For non-zero v⃗,w⃗∈Rn\vec{v}, \vec{w} \in \mathbb{R}^n,

cos⁡(θ)=v⃗⋅w⃗∥v⃗∥∥w⃗∥,\cos(\theta) = \dfrac{\vec{v} \cdot \vec{w}}{\|\vec{v}\| \|\vec{w}\|},

where θ\theta is the angle between v⃗\vec{v} and w⃗\vec{w}.

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.

Example

Two drones are tracked, each velocity recorded in metres per second to the east, to the north and upwards.

The first has velocity v⃗=[41−1]\vec{v} = \begin{bmatrix} 4 \\ 1 \\ -1 \end{bmatrix} and the second has velocity w⃗=[−345]\vec{w} = \begin{bmatrix} -3 \\ 4 \\ 5 \end{bmatrix}.

Find cos⁡(θ)\cos(\theta), where θ\theta 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:

v⃗⋅w⃗=4(−3)+1(4)+(−1)(5)=−12+4−5=−13.\begin{align*} \vec{v} \cdot \vec{w} &= 4(-3) + 1(4) + (-1)(5) \\ &= -12 + 4 - 5 \\ &= -13. \end{align*}

Each length comes from the squares of its components, and the first square root simplifies because 18=9×218 = 9 \times 2:

∥v⃗∥=42+12+(−1)2=18=32.\begin{align*} \|\vec{v}\| &= \sqrt{4^2 + 1^2 + (-1)^2} \\ &= \sqrt{18} \\ &= 3\sqrt{2}. \end{align*}

The same calculation for w⃗\vec{w} gives a square root that simplifies in the same way, since 50=25×250 = 25 \times 2:

∥w⃗∥=(−3)2+42+52=50=52.\begin{align*} \|\vec{w}\| &= \sqrt{(-3)^2 + 4^2 + 5^2} \\ &= \sqrt{50} \\ &= 5\sqrt{2}. \end{align*}

Substituting all three into the cosine, both lengths carry a factor of 2\sqrt{2}, so their product is rational: 32×52=15×2=303\sqrt{2} \times 5\sqrt{2} = 15 \times 2 = 30.

cos⁡(θ)=−1332×52=−1330,\begin{align*} \cos(\theta) &= \dfrac{-13}{3\sqrt{2} \times 5\sqrt{2}} \\ &= -\dfrac{13}{30}, \end{align*}

which is about −0.43-0.43.

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 v⃗=[1224]\vec{v} = \begin{bmatrix} 1 \\ 2 \\ 2 \\ 4 \end{bmatrix} and the counts for the second are w⃗=[2012]\vec{w} = \begin{bmatrix} 2 \\ 0 \\ 1 \\ 2 \end{bmatrix}. Let θ\theta be the angle between these two vectors.

What is cos⁡(θ)\cos(\theta)?

Select the correct answer:

+ 3 more questions