Free preview

189 lessons

Essential Calculus for ML

Free preview

Essential Calculus for ML · 189 lessons

Master the fundamentals

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Steepest ascent and descent

The rate as ∥∇f∥cos⁡θ\|\nabla f\|\cos\theta

Explanation

At a point (a,b)(a,b), in which direction does ff rise fastest?

Every unit direction u⃗\vec{u} has its own rate Du⃗f(a,b)D_{\vec{u}}f(a,b), and only the direction varies from one rate to the next. There are infinitely many directions, so we cannot compare the rates by evaluating Du⃗f(a,b)D_{\vec{u}}f(a,b) one direction at a time. We need a formula for the rate in which the direction appears only through one angle.

Every one of those rates is a dot product, and the dot product also has a geometric form: the two lengths multiplied by the cosine of the angle between the vectors. Writing θ\theta for the angle between u⃗\vec{u} and ∇f(a,b)\nabla f(a,b),

Du⃗f(a,b)=∇f(a,b)⋅u⃗=∥∇f(a,b)∥ ∥u⃗∥cos⁡θ.D_{\vec{u}}f(a,b) = \nabla f(a,b) \cdot \vec{u} = \|\nabla f(a,b)\| \, \|\vec{u}\| \cos\theta.

A direction u⃗\vec{u} is a unit vector, so ∥u⃗∥=1\|\vec{u}\| = 1 and the second length drops out:

Du⃗f(a,b)=∥∇f(a,b)∥cos⁡θ.D_{\vec{u}}f(a,b) = \|\nabla f(a,b)\| \cos\theta.

At a fixed point ∥∇f(a,b)∥\|\nabla f(a,b)\| is a fixed number, so turning the direction changes only cos⁡θ\cos\theta, which takes every value from −1-1 to 11.

(a,b)
∇f(a,b)
u→
θ

Move the slider to turn the unit direction u→.

cos⁡θ=0.50
Du→f(a,b)=‖∇f(a,b)‖cos⁡θ=1.12

Here ∇f(a,b)=[21], so ‖∇f(a,b)‖=5≈2.24. The angle θ is always the smaller angle between u→ and ∇f(a,b), so it rises to 180∘ and then falls back to 0∘.

Example

At a point (a,b)(a,b) where ∥∇f(a,b)∥=10\|\nabla f(a,b)\| = 10, find the rate of change of ff at (a,b)(a,b) in a unit direction u⃗\vec u at θ=60∘\theta = 60^\circ to ∇f(a,b)\nabla f(a,b), and in a unit direction at θ=90∘\theta = 90^\circ to it.

Solution

Both directions are unit vectors, so each rate is the length of the gradient multiplied by the cosine of the angle:

Du⃗f(a,b)=∥∇f(a,b)∥cos⁡θ=10cos⁡θD_{\vec u}f(a,b) = \|\nabla f(a,b)\| \cos\theta = 10\cos\theta

The length 1010 is the same for both directions, because the point is fixed. Only θ\theta changes.

At θ=60∘\theta = 60^\circ we have cos⁡60∘=12\cos 60^\circ = \dfrac{1}{2}, so

Du⃗f(a,b)=10×12=5D_{\vec u}f(a,b) = 10 \times \dfrac{1}{2} = 5

and ff rises at 55 per unit of distance in that direction.

At θ=90∘\theta = 90^\circ we have cos⁡90∘=0\cos 90^\circ = 0, so

Du⃗f(a,b)=10×0=0D_{\vec u}f(a,b) = 10 \times 0 = 0

and ff neither rises nor falls in that direction.

Practice questions

4 questions

At a point (a,b)(a,b) where ∇f(a,b)≠0⃗\nabla f(a,b) \neq \vec 0, two unit directions make angles θ=30∘\theta = 30^\circ and θ=150∘\theta = 150^\circ with ∇f(a,b)\nabla f(a,b). How do the rates of change of ff at (a,b)(a,b) in these two directions compare?

Select the correct answer:

+ 3 more questions

Steepest ascent is along the gradient

Explanation

We can now find the rate of change of ff in any unit direction, and we can compute ∇f(a,b)\nabla f(a,b) at any point. Together they answer the question of which direction makes ff rise fastest, and the answer is the gradient itself.

At each point (a,b)(a,b) where ∇f(a,b)≠0⃗\nabla f(a,b) \neq \vec 0, the direction of ∇f(a,b)\nabla f(a,b) is the direction of steepest ascent, and its length ∥∇f(a,b)∥\|\nabla f(a,b)\| is the rate of steepest ascent. This holds at one point at a time: ∇f(a,b)\nabla f(a,b) changes from point to point, and the direction of steepest ascent changes with it.

The reason is the formula Du⃗f(a,b)=∥∇f(a,b)∥cos⁡θD_{\vec{u}}f(a,b) = \|\nabla f(a,b)\| \cos\theta. At a fixed point only θ\theta changes, so the largest and smallest rates come from the largest and smallest values of cos⁡θ\cos\theta:

θ\thetacos⁡θ\cos\thetaRate Du⃗f(a,b)D_{\vec{u}}f(a,b)Direction of u⃗\vec{u}
0∘0^\circ11∥∇f(a,b)∥\Vert\nabla f(a,b)\Vert, the largestalong ∇f(a,b)\nabla f(a,b)
180∘180^\circ−1-1−∥∇f(a,b)∥-\Vert\nabla f(a,b)\Vert, the smallestalong −∇f(a,b)-\nabla f(a,b)

At a point where ∇f(a,b)≠0⃗\nabla f(a,b) \neq \vec 0, both directions and their rates come from the gradient alone:

Finding the directions of steepest ascent and descent

Procedure

  1. Compute the gradient ∇f(a,b)\nabla f(a,b) at the point.
  2. Compute its length ∥∇f(a,b)∥\|\nabla f(a,b)\|, and divide ∇f(a,b)\nabla f(a,b) by it to get the unit direction of steepest ascent, ∇f(a,b)∥∇f(a,b)∥\dfrac{\nabla f(a,b)}{\|\nabla f(a,b)\|}.
  3. Negate that unit direction to get the unit direction of steepest descent, −∇f(a,b)∥∇f(a,b)∥-\dfrac{\nabla f(a,b)}{\|\nabla f(a,b)\|}.

The board below draws the gradient at a point and then the two unit directions it gives.

(a,b)
∇f(a,b)
u→
θ

Step 1: The gradient at the point

The gradient at (a,b) is ∇f(a,b).

A unit direction u→ has length 1, so it starts at (a,b) and ends on the dashed circle. Its angle to ∇f(a,b) is θ.

Step 2: Steepest ascent

At θ=0∘, u→ points along ∇f(a,b): this is the direction of steepest ascent. Dividing ∇f(a,b) by its length gives

u→=∇f(a,b)‖∇f(a,b)‖

The rate is ‖∇f(a,b)‖, the largest.

Step 3: Steepest descent

At θ=180∘, u→ points opposite to the direction of steepest ascent: this is the direction of steepest descent. Negating the steepest-ascent direction gives

u→=−∇f(a,b)‖∇f(a,b)‖

The rate is −‖∇f(a,b)‖, the smallest.

Example

For f(x,y)=3x2+y2f(x,y) = 3x^2 + y^2, find the unit directions of steepest ascent and steepest descent at (1,4)(1,4), and the rate of change of ff in each.

Solution

First we compute the gradient at the point. The power rule gives the first partials, and substituting x=1x = 1 and y=4y = 4 turns them into numbers:

∇f=[6x2y],∇f(1,4)=[68]\nabla f = \begin{bmatrix} 6x \\ 2y \end{bmatrix}, \qquad \nabla f(1,4) = \begin{bmatrix} 6 \\ 8 \end{bmatrix}

Next we compute the length of the gradient:

∥∇f(1,4)∥=62+82=100=10\|\nabla f(1,4)\| = \sqrt{6^2 + 8^2} = \sqrt{100} = 10

This length is the largest rate of change at (1,4)(1,4), so in the direction of steepest ascent ff rises at 1010 per unit of distance. Dividing the gradient by its length gives that direction as a unit vector:

∇f(1,4)∥∇f(1,4)∥=110[68]=[3545]\dfrac{\nabla f(1,4)}{\|\nabla f(1,4)\|} = \dfrac{1}{10}\begin{bmatrix} 6 \\ 8 \end{bmatrix} = \begin{bmatrix} \dfrac{3}{5} \\[10pt] \dfrac{4}{5} \end{bmatrix}

Steepest descent is the opposite unit direction, so we negate both components:

−∇f(1,4)∥∇f(1,4)∥=[−35−45]-\dfrac{\nabla f(1,4)}{\|\nabla f(1,4)\|} = \begin{bmatrix} -\dfrac{3}{5} \\[10pt] -\dfrac{4}{5} \end{bmatrix}

In this direction the rate of change is −10-10, so ff falls at 1010 per unit of distance.

Practice questions

4 questions

At (1,2)(1,2) a function ff has ∇f(1,2)=[−512]\nabla f(1,2) = \begin{bmatrix}-5\\12\end{bmatrix}. In which unit direction does ff rise fastest at that point, and how fast does it rise there?

Select the correct answer:

+ 3 more questions

The gradient on a contour plot

Explanation

A contour plot usually comes with no formula for ff, but it still shows which way ∇f\nabla f points at any point.

At a point PP on a contour, ff has the same value all along that contour, so the rate of change along it is zero. In a unit direction at angle θ\theta to the gradient the rate is ∥∇f∥cos⁡θ\|\nabla f\|\cos\theta, which, when the gradient is not zero, is zero only at θ=90∘\theta = 90^\circ. So the gradient crosses the contour at a right angle. Of the two directions at a right angle, it is the one pointing towards the higher levels, because the gradient is the direction of steepest ascent.

The gradient on a contour plot

Theorem

At a point PP where the gradient is not zero, ∇f(P)\nabla f(P) crosses the contour through PP at a right angle and points towards the higher levels.

6
10
14
18
P

Step 1: The contour through P

The contour plot shows one surface at the levels 6, 10, 14 and 18. The point P lies on the contour at level 14, so every point of that contour has f=14.

Step 2: Two directions of zero change

A step from P either way along the contour stays at level 14, so both directions along the contour give Du→f(P)=0.

Step 3: A right angle to the contour

Zero change needs cos⁡θ=0, so both directions along the contour are at 90∘ to ∇f(P). The gradient therefore lies on the dashed line, which crosses the contour at a right angle.

Step 4: Towards the higher levels

Of the two ways along the dashed line, ∇f(P) is the one pointing towards the higher levels, from 14 towards 18, because it is the direction of fastest increase.

ML Context

Gradient descent steps along −∇f-\nabla f, so on a contour plot of the loss each step leaves the contour it starts on at a right angle.

Practice questions

5 questions

The contour plot of a function ff is shown below, with the point PP marked on one of its contours and four arrows drawn from PP. Which arrow points along ∇f(P)\nabla f(P)?

12
18
24
30
P
A
B
C
D
One surface, drawn at the levels 12, 18, 24 and 30, in steps of 6. The four red arrows from P all have the same length.

Select the correct answer:

+ 4 more questions