Taylor, Optimality and Gradient Descent

Local approximation, optimality tests and gradient descent, which later ML models rely on.

8 lessons · 28 past questions · 89 marks in the papers collected here

Open the interactive lessons →Taylor, Optimality and Gradient Descent past questions

Diagrams, animations, quizzes and checked solutions are on the interactive pages.

Contents

  1. Taylor series
  2. Taylor approximation
  3. Optimality tests
  4. Closest point problems
  5. Applied problems with constraints
  6. Gradient descent steps
  7. Choosing the step size
  8. Extra: convexity

1. Taylor series

Match the function's value, slope, bend and so on at one point.

writing a smooth function as a sum of powers around a point

The question this lesson answers. How can a complicated function be copied by a polynomial near one point?

Building a copy near one point

Match a function at one point: first its height, then its slope, then how the slope bends. Each new term fixes one more feature, so the copy stays close over a wider stretch. Far from the point, the copy can be badly wrong.

Taylor series about $a$

$$f(x)=f(a)+f'(a)(x-a)+\frac{f''(a)}{2!}(x-a)^2+\frac{f'''(a)}{3!}(x-a)^3+\cdots$$ Condition: $f$ is differentiable as often as needed near $a$, and for the infinite series to equal $f$, $x$ must lie where the series converges to $f$.

Why it works this way. Differentiating $(x-a)^k$ exactly $k$ times leaves $k!$ and wipes out the other powers at $a$, so matching derivatives forces each coefficient.

Cutting after the power $(x-a)^k$ gives the Taylor polynomial $T_k$. It is the only polynomial of degree at most $k$ whose value and first $k$ derivatives agree with those of $f$ at $a$.

Reading a given polynomial backwards is just as common: if $T_3(x)=c_0+c_1x+c_2x^2+c_3x^3$ about 0, then $$f(0)=c_0,\quad f'(0)=c_1,\quad f''(0)=2c_2,\quad f'''(0)=6c_3.$$

Series worth knowing, about 0

FunctionFirst termsValid for
$e^x$$1+x+\tfrac{x^2}2+\tfrac{x^3}6$all $x$
$\dfrac1{1-x}$$1+x+x^2+x^3$$\lvert x\rvert<1$
$\ln(1+x)$$x-\tfrac{x^2}2+\tfrac{x^3}3$$-1<x\le1$
$\sin x$$x-\tfrac{x^3}6$all $x$
$\cos x$$1-\tfrac{x^2}2$all $x$
Tool: the derivative rules these questions use
  • Powers: $\dfrac{d}{dx}x^n=nx^{n-1}$ for any real $n$, so $\dfrac1x\to-\dfrac1{x^2}$ and $\sqrt x\to\dfrac1{2\sqrt x}$. A constant gives $0$.
  • Others: $e^x\to e^x$, $\ln x\to\dfrac1x$, $\sin x\to\cos x$, $\cos x\to-\sin x$.
  • Sum: differentiate term by term. A constant multiple stays in front.
  • Product: $(uv)'=u'v+uv'$.
  • Chain: $\dfrac{d}{dx}g(h(x))=g'(h(x))\,h'(x)$. For example $e^{3x}\to3e^{3x}$ and $\sqrt{2-x^2}\to\dfrac{-x}{\sqrt{2-x^2}}$.
  • On this site $\log$ means the natural logarithm $\ln$.

Worked example: A reciprocal about 2

Problem.

Expand $f(x)=1/x$ about $x=2$ up to degree 2.

Idea.

Derivatives at 2, divided by factorials.

  1. $f=\tfrac12$, $f'=-\tfrac14$, $f''=\tfrac14$ at $x=2$.
  2. Divide by $0!,1!,2!$.

Answer.

$\tfrac12-\tfrac14(x-2)+\tfrac18(x-2)^2$.

Common mistake.

Forgetting to divide the second derivative by $2!$.

Worked example: Reading derivatives from a polynomial

Problem.

$T_3(x)=5-x+4x^2+2x^3$ about 0. Find $f''(0)+f'''(0)$.

Idea.

Multiply coefficients by factorials.

  1. $f''(0)=2!\cdot4=8$.
  2. $f'''(0)=3!\cdot2=12$.

Answer.

20.

Common mistake.

Using the coefficients themselves, which gives 6.

Exam tip

Test an option at the centre and just beside it: the value at $a$ must be $f(a)$ and the sign of the linear term must match $f'(a)$.

The recipe

StepDo thisCheck before moving on
1Write the centre $a$.Every power is of $(x-a)$.
2List $f(a),f'(a),f''(a),\dots$ up to the degree asked.Evaluate at $a$, not at $x$.
3Divide the $k$-th value by $k!$.$0!=1$ and $1!=1$.
4Assemble the terms.At $x=a$ the polynomial gives $f(a)$.

Substitution saves work. If you know the series of $e^u$, put $u=x^2$ or $u=3x$ into it. There is no need to differentiate repeatedly.

Worked example: A fourth derivative without differentiating

Problem.

For $f(x)=e^{x^2}$ find $f^{(4)}(0)$.

Idea.

Substitute into the exponential series, then read the coefficient.

  1. $e^u=1+u+\tfrac{u^2}{2}+\cdots$.
  2. With $u=x^2$: $1+x^2+\tfrac{x^4}{2}+\cdots$.
  3. The coefficient of $x^4$ is $\tfrac12$.
  4. $f^{(4)}(0)=4!\cdot\tfrac12=12$.

Answer.

$12$

Common mistake.

Answering $\tfrac12$, which is the coefficient and not the derivative.

Check your answer in ten seconds

An even function such as $e^{x^2}$ or $\cos x$ has only even powers about 0.

Memory line

Derivative at the centre, divided by the factorial.

2. Taylor approximation

Start from what you know and add the change.

using a few terms to estimate a nearby value

The question this lesson answers. How do you estimate a value nearby using only the slope and the bend?

Zooming in

Close up, a smooth curve looks straight. A little further out, a parabola is a better description.

First and second order, one variable

$$f(a+d)\approx f(a)+f'(a)\,d+\tfrac12f''(a)\,d^2$$ Condition: $d$ is small. The first two terms alone are the first-order (linear) approximation.

Why it works this way. The first term gets the height right, the second the slope, and the third the curvature. Each extra term shrinks the error for small steps.

Tool: partial derivatives and the gradient

$\partial f/\partial x$ means: differentiate in $x$ and treat every other variable as a fixed number. The gradient $\nabla f$ lists all the partial derivatives in order. Evaluate it at the current point before you take the step.

First and second order, several variables

$$f(a+d)\approx f(a)+\nabla f(a)^Td+\tfrac12d^T\nabla^2f(a)\,d$$ $a,d\in\mathbb R^n$; $\nabla f$ is the gradient and $\nabla^2f$ the Hessian.

Method:

  1. Identify the known point $a$ and the target. Then $d=$ target $-$ $a$, keeping signs.
  2. Use only the order the question asks for.
  3. Keep the factor $\tfrac12$ on the second-order term.

The gradient points in the direction of fastest increase. In the first-order model, the change is $\nabla f^Td$. It is most negative, for a fixed length of $d$, when $d$ points opposite to the gradient. That is the reason behind gradient descent.

If a question gives $f'$ and $f''$ as numbers or formulas, use what is given. If the given $f''$ is not really the derivative of the given $f'$, say so, and state which values you used.

Worked example: Second order

Problem.

$f(2)=3$, $f'(2)=4$, $f''(2)=-2$. Estimate $f(2.1)$.

Idea.

$d=0.1$.

  1. $3+4(0.1)+\tfrac12(-2)(0.01)$.

Answer.

$3+0.4-0.01=3.39$.

Common mistake.

Dropping the $\tfrac12$ and getting 3.38.

Worked example: A gradient step backwards

Problem.

$f(2,1)=7$, $\nabla f(2,1)=(3,-2)$. Estimate $f(1,3)$ to first order.

Idea.

$d=(-1,2)$.

  1. $\nabla f^Td=3(-1)+(-2)(2)=-7$.

Answer.

$7-7=0$.

Common mistake.

Using $d=(1,-2)$, which gives 14.

Worked example: Build the gradient and Hessian, then estimate

Problem.

For $f(x,y)=x^2y+y^3$, estimate $f(1.1,\,1.9)$ to second order around $(1,2)$.

Idea.

A partial derivative differentiates in one variable and holds the other constant. The gradient lists the two first partials. The Hessian is the table of second partials: $\begin{bmatrix}f_{xx}&f_{xy}\\f_{yx}&f_{yy}\end{bmatrix}$.

  1. Value: $f(1,2)=2+8=10$.
  2. First partials: $f_x=2xy$ and $f_y=x^2+3y^2$. At $(1,2)$: $\nabla f=(4,\,13)$.
  3. Differentiate again: $f_{xx}=2y$, $f_{xy}=f_{yx}=2x$, $f_{yy}=6y$. At $(1,2)$: $H=\begin{bmatrix}4&2\\2&12\end{bmatrix}$.
  4. Step: $d=(1.1,1.9)-(1,2)=(0.1,-0.1)$.
  5. Linear term: $\nabla f^Td=4(0.1)+13(-0.1)=-0.9$.
  6. Quadratic term: $Hd=(0.2,\,-1.0)$, so $d^THd=0.02+0.10=0.12$, and half of it is $0.06$.
  7. Add: $10-0.9+0.06=9.16$. The exact value is $9.158$.

Answer.

$9.16$.

Common mistake.

Counting the mixed term once: $f_{xy}$ appears twice in $H$. Forgetting the $\tfrac12$ is the other common slip.

From the lecture notes: directional derivative

For a differentiable $f$ and a unit vector $u$: $$D_uf(v)=\nabla f(v)^Tu$$ When $\nabla f(v)\ne0$, the fastest increase is along $u=\nabla f(v)/\lVert\nabla f(v)\rVert$ at rate $\lVert\nabla f(v)\rVert$, and the fastest decrease is along $-u$. Any $u$ with $\nabla f(v)^Tu<0$ is a descent direction.

Worked example: Estimate against exact

Problem.

Estimate $1.1^2$ to first order around 1, and give the error.

Idea.

First order keeps only the slope.

  1. $f(1)=1$, $f'(1)=2$.
  2. The estimate is $1+2(0.1)=1.2$.
  3. The exact value is $1.21$.

Answer.

Estimate $1.2$, error $0.01$.

Common mistake.

Adding the second-order term when first order is asked.

Tips and tricks
  • Write the step $d$ = new point minus old point on its own line first. Most errors are a wrong sign here.
  • Put $d=0$ into your approximation: it must return $f$ at the old point.
  • For a quadratic function the second-order approximation gives the exact value, so no rounding is needed.
Memory line

Value, plus slope times step, plus half the bend.

Method for any exam question

StepDo this
1Choose the known point and the step.
2Compute value, gradient and, if needed, Hessian.
3Keep only the order asked.
4Remember the factor one half.

3. Optimality tests

Flat first; then look at how it curves.

deciding what a flat point is

The question this lesson answers. The gradient is zero. Is it a lowest point, a highest point or neither?

Standing on level ground

Level ground may be a valley floor, a hilltop or a mountain pass. The bending in every direction tells which.

At a point with $\nabla f(x^*)=0$ (twice continuously differentiable $f$)

Hessian at $x^*$Conclusion
positive definitestrict local minimum
negative definitestrict local maximum
indefinitesaddle point
semidefinite but not definitethe test is silent

A point with zero gradient is stationary. At a local minimum or maximum inside the domain the gradient must be zero (necessary condition). Zero gradient alone is not enough.

At a minimum the Hessian must be positive semidefinite (necessary). Positive definite is enough to guarantee a strict local minimum (sufficient). The two are not the same: $f=x^4$ has a minimum at 0 with $f''(0)=0$.

The test is local. It says nothing about values far away.

A useful shortcut. If $f(x)=g(h(x))$ with $g$ strictly increasing, then $f$ and $h$ have the same minimisers. So to minimise $e^{h(x)}$, minimise $h$. The minimum value is then $g(\min h)$.

Worked example: Classify a flat point

Problem.

Classify the stationary point of $f=x^2+4y^2-2x$.

Idea.

Gradient zero, then Hessian.

  1. $\nabla f=(2x-2,8y)=0$ at $(1,0)$.
  2. Hessian $\operatorname{diag}(2,8)$ is positive definite.

Answer.

Local minimum at $(1,0)$; since $f$ is a bowl, it is global.

Common mistake.

Stopping after the gradient.

Worked example: A saddle

Problem.

Classify the origin for $f=x^2+4xy+y^2$.

Idea.

Hessian eigenvalues.

  1. Hessian $\begin{bmatrix}2&4\\4&2\end{bmatrix}$.
  2. Determinant $4-16<0$.

Answer.

Saddle point.

Common mistake.

Seeing positive entries and answering "minimum".

The silent case

$x^4$, $-x^4$ and $x^3$ all have $f'(0)=f''(0)=0$, and give a minimum, a maximum and neither.

From the lecture notes: two-variable test where $\nabla f=0$, with $D=f_{xx}f_{yy}-f_{xy}^2$

SignsConclusion
$D>0$ and $f_{xx}>0$local minimum
$D>0$ and $f_{xx}<0$local maximum
$D<0$saddle point, whatever the sign of $f_{xx}$
$D=0$the test is silent
From the lecture notes: stationary and critical points

Stationary: $f'(x)=0$. Critical: $f'(x)=0$ or $f'(x)$ does not exist. So $x=0$ is a critical point of $\lvert x\rvert$ but not a stationary one.

At an interior local extremum of a differentiable function, $f'=0$ (Fermat). The converse is false: $x^3$ is flat at $0$ without an extremum.

Why it works this way. Where the gradient vanishes, the second-order term decides how the function changes. Its sign pattern is the definiteness of the Hessian.

Worked example: A silent test

Problem.

For $f=x^4+y^4$ at the origin, what is the Hessian and what kind of point is it?

Idea.

A zero Hessian decides nothing.

  1. The second derivatives are 0 at the origin.
  2. But $f>0$ everywhere else.

Answer.

Zero matrix; still a minimum.

Common mistake.

Calling it a saddle because the test fails.

Tips and tricks
  • For a $2\times2$ Hessian: determinant below 0 means saddle. Determinant above 0 means minimum if the top-left entry is positive, maximum if negative.
  • Determinant equal to 0 decides nothing from this test. Look at the function itself.
  • Maximising $f$ is the same as minimising $-f$; the point is the same and the value changes sign.
Memory line

Positive definite: minimum. Negative: maximum. Mixed: saddle.

Method for any exam question

StepDo this
1Solve for zero gradient.
2Build the Hessian there.
3Classify its definiteness.
4If it is only semidefinite, test further.

4. Closest point problems

Minimise squared distance along the curve.

the point of a line or curve nearest to a given point

The question this lesson answers. Which point of a curve is nearest to a given point?

A growing circle

Draw circles around the given point and let them grow. The first moment a circle touches the curve gives the closest point. At that moment the radius meets the curve at a right angle.

Why it works this way. At the nearest point you cannot get closer by sliding along the curve, so the line to the given point is perpendicular to the curve.

To find the point of a curve closest to $(p,q)$, minimise $$D(x,y)=(x-p)^2+(y-q)^2$$ subject to the curve. Squaring does not move the minimiser, because squaring is increasing on non-negative numbers, and it removes the square root.

Then substitute. For a line $y=mx+c$, $D$ becomes a parabola in $x$. Sometimes it is easier to remove $x$: on $y=x^2$, writing $x^2=y$ gives $D=y+(y-q)^2$. When you substitute this way, remember the range: $x^2=y$ forces $y\ge0$.

Check three things: (1) the derivative is zero, (2) the value of the removed variable exists and is real, (3) whether two symmetric points tie.

For a line there is a geometric check: the segment from the given point to the closest point is perpendicular to the line. So (closest − given) · (direction) $=0$.

Name the curve correctly. $x^2+y^2=4$ is a circle. $y^2-x^2=4$ is a hyperbola, even if a question calls it something else. Work from the equation.

Worked example: A line

Problem.

Closest point on $y=x+2$ to $(2,0)$.

Idea.

Substitute and differentiate.

  1. $D=(x-2)^2+(x+2)^2=2x^2+8$.
  2. $D'=4x=0$, so $x=0$.

Answer.

$(0,2)$. Check: $(-2,2)\cdot(1,1)=0$.

Common mistake.

Minimising only the vertical distance, which gives $(2,4)$.

Worked example: A parabola with two answers

Problem.

Closest points on $y=x^2$ to $(0,3)$.

Idea.

Use $x^2=y$.

  1. $D=y+(y-3)^2$ with $y\ge0$.
  2. $D'=1+2(y-3)=0$, so $y=2.5$.
  3. $x=\pm\sqrt{2.5}$.

Answer.

Two points; $D=2.5+0.25=2.75$. The vertex $y=0$ has $D=9$.

Common mistake.

Answering the vertex $(0,0)$.

The recipe

StepDo thisCheck before moving on
1Write the squared distance to the given point.No square root is needed.
2Use the curve to remove one variable.Write down the allowed range of the other.
3Differentiate and solve.Reject values outside the range.
4Compare all candidates and the ends of the range.Report points, or the distance, as asked.

Symmetry gives ties. If the curve and the given point are mirror images of themselves across a line, solutions off that line come in pairs. Reporting only one of them loses marks in a multiple-select question.

Worked example: A circle

Problem.

Find the point of $x^2+y^2=4$ closest to $(3,4)$, and the distance.

Idea.

The closest point lies on the ray from the centre to the given point.

  1. The length of $(3,4)$ is $5$.
  2. Scale to radius 2: $\tfrac25(3,4)=(1.2,1.6)$.
  3. The distance is $5-2=3$.
  4. The point on the opposite side, $(-1.2,-1.6)$, is the farthest.

Answer.

$(1.2,1.6)$ at distance $3$.

Common mistake.

Reporting the squared distance 9 when the distance is asked.

Check your answer in ten seconds

The vector from your point to the given point must be perpendicular to the curve's tangent.

Memory line

Minimise the squared distance on the curve.

5. Applied problems with constraints

Use the condition to leave one variable, then differentiate.

word problems with one condition linking the variables

The question this lesson answers. A word problem asks for the best design. How do you turn it into calculus?

Goal and rule

Every such problem has one quantity to optimise and one rule that links the variables. Use the rule to leave a single variable.

Steps:

  1. Name the variables, with units.
  2. Write the objective.
  3. Write the constraint.
  4. Solve the constraint for one variable and substitute.
  5. Note the allowed range of what remains.
  6. Differentiate, solve, and check it is a maximum or minimum (sign of the second derivative, or compare with the ends).
  7. Compute the quantity asked, which may not be the variable.

Cost problems: multiply each area by its own price. "Top and bottom cost twice as much" applies to both discs.

Inscribed shapes: the shape must fit inside the region at every point. For a circle centred at the origin inside a region, the largest radius is the smallest distance from the origin to the boundary. So an inscribed-circle question is a closest-point question.

If your computed value disagrees with a printed key, redo the set-up once from the wording, test your point against nearby values, and report the disagreement. Do not change the method to reach the printed number.

Square root in the objective?

If the quantity to maximise or minimise is never negative, you may work with its square instead: the best point is the same and the square root disappears. For $x\sqrt{4-x^2}$ with $0\le x\le2$, study $x^2(4-x^2)=u(4-u)$ with $u=x^2$. It peaks at $u=2$, so the original has maximum $\sqrt{4}=2$. Remember to take the root again at the end.

Worked example: Fixed perimeter

Problem.

A rectangle has perimeter 20. Maximise its area.

Idea.

Substitute $y=10-x$.

  1. $A=x(10-x)$ on $0\le x\le10$.
  2. $A'=10-2x=0$ gives $x=5$.

Answer.

A square of area 25. $A''=-2<0$.

Common mistake.

Using $x+y=20$.

Worked example: An open box

Problem.

A square-based open box has volume 32. Minimise its surface area.

Idea.

$h=32/x^2$.

  1. $S=x^2+4xh=x^2+128/x$.
  2. $S'=2x-128/x^2=0$ gives $x^3=64$.

Answer.

$x=4$, $h=2$, area 48.

Common mistake.

Counting a lid, which gives a different box.

From the lecture notes: a minimum may not exist

$\min x$ and $\min\log x$ (for $x>0$) are unbounded below, so there is no minimum. $\min x$ with $0<x\le5$ has infimum $0$ but no minimum, because $0$ is not feasible.

When a global minimum exists, its value is unique but the point need not be: $\sin x$ reaches $-1$ infinitely often. Maximising $-f$ picks the same points as minimising $f$, and the optimal value changes sign.

From the lecture notes: standard form

$\min_{x\in\mathbb R^d}f(x)$ subject to $g_i(x)\le0$ and $h_j(x)=0$. The feasible region is the set of points that satisfy every constraint.

Why it works this way. A constraint removes one degree of freedom. After substituting it, ordinary one-variable calculus applies on the allowed range.

Worked example: The best point is at the edge

Problem.

Maximise $x(10-x)$ for $0\le x\le3$.

Idea.

Check whether the stationary point is allowed.

  1. The derivative is zero at $x=5$, outside the range.
  2. The function rises on the whole range.

Answer.

$x=3$ with value $21$.

Common mistake.

Answering $x=5$.

Tips and tricks
  • Underline what is asked: the point, the value of the function, or a length. They are different answers.
  • Substitute the constraint early so only one variable is left, then write the range that variable may take.
  • Compare the value at the stationary point with the values at the ends of the range.
Memory line

Objective, constraint, substitute, check.

Method for any exam question

StepDo this
1Name the variables.
2Write the objective and the constraint.
3Substitute and note the allowed range.
4Differentiate, then check it is the right kind of extreme.

6. Gradient descent steps

Step against the slope; repeat.

carrying out the update by hand

The question this lesson answers. When no formula gives the minimum, how can you still walk to it?

Walking downhill in fog

You cannot see the valley, but you can feel the slope under your feet. Step against the slope, feel again, and repeat. The step size is how long each stride is.

Why iterate at all? Linear regression has an exact formula, $(X^TX)^{-1}X^Ty$. Many other problems have none, and even this one is expensive when there are very many features, because a large matrix must be inverted. Gradient descent only needs gradients.

Why the minus sign? Take $f(x)=(x-5)^2$. At $x=7$ you must move left, at $x=3$ you must move right, and at $x=5$ you must stay. The value $-f'(x)$ is $-4$, $4$ and $0$ there, which does all three. In general, to first order the change is $\nabla f(x)^T(\eta d)$, and choosing $d=-\nabla f(x)$ makes it $-\eta\lVert\nabla f(x)\rVert^2\le0$.

What the step size does here. Starting at $7$: with $\eta=1$ the run visits $3,7,3,\dots$; with $\eta=0.5$ it lands on $5$ at once; with $\eta=0.25$ it moves $6,\ 5.5,\dots$ and creeps in.

Steps that shrink too fast also fail. The rates $1,\tfrac12,\tfrac14,\dots$ add up to only $2$. If the gradient never has size above 1, the whole run travels at most a distance of 2, so a start farther away never arrives. The rates $\tfrac1{t+1}$ shrink too, but their sum has no limit.

Update rule

$$x_{t+1}=x_t-\eta_t\,\nabla f(x_t)$$ $\eta_t>0$ is the learning rate (step size). The gradient is evaluated at the current point $x_t$.

Why it works this way. To first order, a step against the gradient changes the function by minus the step size times the squared gradient length, which cannot be positive.

Method for each iteration:

  1. Compute the gradient at the current point.
  2. Compute $\eta_t$ for this iteration.
  3. Subtract $\eta_t$ times the gradient from every coordinate at the same time.
  4. Only then move to the next iteration.

Counting the index. A schedule such as $\eta_t=\frac1{t+1}$ gives different numbers if $t$ starts at 0 or at 1. Read the question. Starting at 0 the rates are $1,\tfrac12,\tfrac13$; starting at 1 they are $\tfrac12,\tfrac13,\tfrac14$.

Minimise or maximise? Gradient descent subtracts. Gradient ascent adds. If a question says "find the optimal value" but names gradient descent, follow descent.

A large first step can overshoot badly. That does not mean you made an error: compute exactly what the rule says, even if the numbers grow.

Worked example: A gradient with a logarithm

Problem.

$f(x,y)=x^2y+\ln(xy)$ at $(1,2)$ with $\eta=0.1$. Do one descent step.

Idea.

Split the logarithm: $\ln(xy)=\ln x+\ln y$ for positive $x,y$.

  1. $\partial f/\partial x=2xy+1/x$ and $\partial f/\partial y=x^2+1/y$.
  2. At $(1,2)$ the gradient is $(5,\;1.5)$.
  3. New point $=(1,2)-0.1\,(5,1.5)$.

Answer.

$(0.5,\;1.85)$.

Common mistake.

Dropping the terms of the other variable's product, such as $x^2$ in the $y$ derivative.

Worked example: Two constant-rate steps

Problem.

$f=x^2+2y^2$, start $(2,1)$, $\eta=0.1$. Do two steps.

Idea.

Gradient $(2x,4y)$.

  1. At $(2,1)$: $(4,4)$, giving $(1.6,0.6)$.
  2. At $(1.6,0.6)$: $(3.2,2.4)$, giving $(1.28,0.36)$.

Answer.

$(1.6,0.6)$ then $(1.28,0.36)$.

Common mistake.

Using the first gradient twice.

Worked example: A schedule from t = 1

Problem.

$f=(x-3)^2$, $x=0$, $\eta_t=1/(t+1)$ for $t=1,2$.

Idea.

Rates $\tfrac12,\tfrac13$.

  1. $f'(0)=-6$; $x=0+3=3$.
  2. $f'(3)=0$; $x$ stays 3.

Answer.

3.

Common mistake.

Starting with rate 1, which jumps to 6.

From the lecture notes: when to stop, and three ways to fail

Stop after a fixed number of steps, or when $\lvert f(x_{t+1})-f(x_t)\rvert<\epsilon$.

  1. Starting where $\nabla f(x_0)=0$, even at a maximum: the iterate never moves.
  2. A poor starting point: the run can settle in a worse local minimum, or diverge.
  3. A step size that is too large: overshooting, oscillation or divergence.

Stochastic gradient descent accepts a less exact gradient in exchange for much cheaper steps.

Worked example: A step from a stationary point

Problem.

Gradient descent on $f=x^2-y^2$ starts at the origin. Where is it after 100 steps?

Idea.

The update uses the gradient.

  1. The gradient at the origin is zero.
  2. Every update subtracts zero.

Answer.

Still $(0,0)$, a saddle point.

Common mistake.

Expecting it to slide downhill along $y$.

Memory line

New point = old point minus step size times gradient at the old point.

7. Choosing the step size

Too small is slow, too large overshoots.

when gradient descent converges, oscillates or diverges

The question this lesson answers. Why does the same method crawl, jump about or blow up?

Pouring water to a mark

Add too little each time and you take forever. Add a bit too much and you overshoot, then correct. Add far too much and every correction is worse than the last.

Gradient descent on $f(x)=ax^2$, $a>0$

$$x_{t+1}=(1-2a\eta)\,x_t\quad\Rightarrow\quad x_t=(1-2a\eta)^t\,x_0$$ So everything is decided by the factor $r=1-2a\eta$.

Why it works this way. On a quadratic each step multiplies the distance from the minimum by a fixed factor. Its size decides convergence and its sign decides zigzagging.

With $x_0\ne0$

Step sizeFactor $r$Behaviour
$0<\eta<\frac1{2a}$between 0 and 1approaches from one side
$\eta=\frac1{2a}$0reaches the minimum in one step
$\frac1{2a}<\eta<\frac1a$between $-1$ and 0jumps across, still converges
$\eta=\frac1a$$-1$alternates $x_0,-x_0$ for ever
$\eta>\frac1a$below $-1$diverges

So "a large learning rate" can mean slow bouncing around the minimum or outright divergence, and "a small learning rate" means slow but steady progress.

Several minima. Gradient descent follows the local slope. With a very small step, it goes downhill into the nearby valley and stays there; it does not know that another valley is deeper. Starting just left of a local minimum $a$ where the slope is negative, it moves right toward $a$. Starting just right of a minimum $b$, the slope is positive, so it moves left toward $b$.

Worked example: Find the safe rates

Problem.

For $f=3x^2$, which $\eta$ converge from any start?

Idea.

$\lvert1-6\eta\rvert<1$.

  1. $0<6\eta<2$.

Answer.

$0<\eta<\tfrac13$, with one-step convergence at $\tfrac16$.

Common mistake.

Using $a=3$ as $2a$.

Worked example: Two valleys

Problem.

A curve has local minima at $x=1$ (value 0) and $x=4$ (value $-3$). With a tiny rate, where does a run from $x=0.9$ go?

Idea.

Follow the local slope.

  1. Just left of a minimum the function is falling, so the step goes right.
  2. Near 1 the slope vanishes.

Answer.

It converges to 1, not to the deeper valley at 4.

Common mistake.

Assuming it finds the global minimum.

The recipe

StepDo thisCheck before moving on
1Write the update as new offset = factor times old offset.For $a x^2$ the factor is $1-2a\eta$.
2With several coordinates, write one factor for each.Use each coordinate's own curvature.
3Require every factor to have size below 1.The steepest direction gives the tightest limit.
4Name the behaviour from the sign.Positive creeps, negative jumps across.

Worked example: Two directions, one rate

Problem.

For $f(x,y)=x^2+4y^2$, which constant rates converge from every start? What happens at $\eta=0.25$ from $(1,1)$?

Idea.

One factor per coordinate.

  1. The factors are $1-2\eta$ and $1-8\eta$.
  2. $\lvert1-2\eta\rvert<1$ needs $0<\eta<1$.
  3. $\lvert1-8\eta\rvert<1$ needs $0<\eta<0.25$.
  4. At $0.25$ the factors are $0.5$ and $-1$, so $x$ shrinks while $y$ flips between $1$ and $-1$.

Answer.

$0<\eta<0.25$. At $0.25$ it does not converge.

Common mistake.

Using only the flatter direction and answering $0<\eta<1$.

Check your answer in ten seconds

Do one step with your rate. If the function value went up on a quadratic, the rate is too large.

Memory line

The factor must have size below 1.

8. Extra: convexity

A bowl-shaped function has no traps.

a short bonus on functions with no false valleys

The question this lesson answers. For which functions is a local minimum guaranteed to be the best one?

A bowl that holds water

A convex graph is shaped like a bowl: roll a ball anywhere and there is no second dip to get stuck in. A flat-bottomed bowl is still convex, and a bowl can slope away for ever without reaching a bottom.

Convex function

$f$ is convex on a convex set if $f(tx+(1-t)y)\le tf(x)+(1-t)f(y)$ for all $x,y$ in it and all $t\in[0,1]$: the graph lies on or below every chord.

Why it works this way. If a lower point existed anywhere, the chord to it would force lower values right next to the local minimum, which is a contradiction.

For a twice continuously differentiable $f$ on an open convex set, convexity is the same as a positive semidefinite Hessian everywhere, not just at one point. For a convex function every local minimum is a global minimum, and for a differentiable convex function every stationary point is a global minimum. A convex function need not have a minimum at all: $e^x$ never reaches its lower bound 0.

Worked example: Test by the second derivative

Problem.

Is $f=x^4$ convex?

Idea.

$f''=12x^2$.

  1. $f''\ge0$ everywhere.

Answer.

Yes, even though $f''(0)=0$.

Common mistake.

Demanding $f''>0$.

Worked example: Two variables

Problem.

Is $f=x^2+4xy+y^2$ convex?

Idea.

Hessian.

  1. $\begin{bmatrix}2&4\\4&2\end{bmatrix}$ has a negative determinant.

Answer.

No: indefinite.

Common mistake.

Checking only the diagonal.

Convex sets come first. A set is convex if the straight segment between any two of its points stays inside it. A disc, a line and a half plane are convex. A ring is not, because a segment can cross the hole. The overlap of convex sets is convex; their union need not be.

Four ways to decide

TestConvex whenUse it when
Chordthe graph is on or below every chordthe function has corners, such as $\lvert x\rvert$
Tangent$f(y)\ge f(x)+\nabla f(x)^T(y-x)$ for all $x,y$$f$ is differentiable and a statement mentions tangents
Second derivative$f''(x)\ge0$ everywhereone variable
Hessianpositive semidefinite everywhereseveral variables

What keeps convexity

OperationResult
sum of convex functionsconvex
convex function times a number $\ge0$convex
maximum of convex functionsconvex
convex function of $Ax+b$convex
product of convex functionsnot always: $x\cdot(-x)=-x^2$

Why every local minimum is global. Suppose $x$ is a local minimum but some $y$ is lower. Every point on the chord from $x$ to $y$ other than $x$ then has a value below $f(x)$, including points very close to $x$. That contradicts $x$ being a local minimum.

Worked example: Corners and flat parts

Problem.

Which are convex on all real numbers: $\lvert x\rvert$, $\max(0,x)^2$, $x^3$?

Idea.

Use the chord test or the second derivative where it exists.

  1. $\lvert x\rvert$ is a maximum of the two linear functions $x$ and $-x$, so it is convex.
  2. $\max(0,x)$ is convex and never negative; squaring it keeps it convex because squaring is increasing and convex on non-negative numbers.
  3. For $x^3$, $f''(x)=6x<0$ when $x<0$.

Answer.

$\lvert x\rvert$ and $\max(0,x)^2$.

Common mistake.

Rejecting a function only because it is not differentiable somewhere.

Check your answer in ten seconds

One chord that dips below the graph is enough to prove a function is not convex.

Memory line

Convex: every local minimum is global.

Method for any exam question

StepDo this
1Check the domain is a convex set.
2Test the second derivative or Hessian everywhere.
3Or use the rules for sums, maxima and linear changes.

Practise this topic →

Weeks covering this topic

Other MLF topics