Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsOptimization

What is optimization?

P1.F125 min●○○ BeginnerReviewed Dec 1, 2026

Why this matters for machine learning

Training a model is an optimization problem: choose the parameters that make a loss as small as possible. Every later lesson in this module, and almost every training method in the course, is a way to solve that one problem.

  • Gradient descent, two lessons on, is the standard way to search a loss landscape that is far too big to scan. (P1.F3: Gradient descent)
  • Maximum likelihood estimation is an optimization problem: maximize the likelihood of the data, which means minimizing the negative log-likelihood. (P1.D10: Maximum Likelihood Estimation)
  • Choosing the loss for a neural network (squared error, cross-entropy) decides which landscape the optimizer has to cross. (P4.6: Loss functions, coming soon)
  • Choosing hyperparameters such as the learning rate is itself an optimization problem, solved by a search that has no gradient to follow. (P4.23: Hyperparameter tuning, coming soon)

Before this: Functions and graphs, Sets, notation and reading math, The gradient

After this lesson you can

  • Say what an optimization problem is, and name its objective, its parameters and what stays fixed (the data, the model's form and the hyperparameters)
  • Tell the minimum value from the argmin, and turn a maximization problem into a minimization problem
  • Read a loss landscape as a surface and as a contour map, and find its lowest point
  • Explain why a minimum can be local, can fail to be unique and can fail to exist
  • Compare grid search, random search and following the slope, and say why the cost of a grid explodes
  • Name three ways to decide when to stop, and say why a low training loss is not the final goal

In P0.2 you tuned two numbers, ww and bb, until a line fitted three points. You did not look for the answer by thinking. You searched: try a pair of numbers, score it with one number (the loss), try a better pair.

That search has a name, optimization, and it is not only machine learning. A delivery company chooses routes that minimize the total distance. A chef chooses the amounts of three ingredients that maximize the taste score. A model chooses its parameters that minimize its loss. The questions are the same each time. What are we allowed to change? What number do we score? Does a best choice even exist, and how do we find it?

This lesson answers those questions with one picture: a landscape you can turn around in your hands.

Three ingredients

An optimization problem has three parts.

  1. The parameters: the numbers we are free to choose. In P0.2 they are ww and bb. We write all of them together as θ\boldsymbol{\theta} (the Greek letter theta). A neural network can have millions or billions of them.
  2. The objective: one number that scores a choice of parameters. In machine learning it is the loss, also called the cost. These words are used loosely in the field, and some books use “cost” for the average over all the data and “loss” for one example. This course writes LL for one example and L\mathcal{L} for the average over the training data. The objective can also be a loss plus a penalty on large weights (P3.A6), and then people call the whole thing the objective.
  3. What is fixed: everything we are not allowed to change while we search.

The third part is where beginners get confused, so here it is for the P0.2 setup, on the three points (1,2)(1, 2), (2,3)(2, 3), (3,7)(3, 7):

  • Parameters (the numbers the search chooses): ww and bb. They change during the search.
  • Data (the training examples): the three points. Fixed.
  • Form of the model (the shape of the formula): y^=wx+b\hat{y} = wx + b, a line. Fixed.
  • Hyperparameters (settings we choose around the search): η=0.05\eta = 0.05 and 200200 steps. Fixed by us.

The loss is a function of the parameters. The data are constants inside it. Change the data and you get a different landscape; change the parameters and you move around on one landscape. Hyperparameters are not part of the loss formula, but they control how the search runs. Choosing them well is a second, outer optimization problem (see “Optimization in the rest of the course” below).

The minimum and the argmin

Write the problem as

min⁡θ L(θ)\min_{\boldsymbol{\theta}}\ \mathcal{L}(\boldsymbol{\theta})

Read it as “find the smallest value of L\mathcal{L} over all choices of θ\boldsymbol{\theta}”. That smallest value is the minimum. The choice of parameters where it happens is the argmin, the notation from P1.A4:

θ^=arg⁡min⁡θ L(θ)\hat{\boldsymbol{\theta}} = \arg\min_{\boldsymbol{\theta}}\ \mathcal{L}(\boldsymbol{\theta})

They are different kinds of thing. For the P0.2 loss the minimum is the number 0.50.5, and the argmin is the pair (w,b)=(2.5,−1)(w, b) = (2.5, -1). Training wants the argmin, because the argmin is the model. The minimum only tells you how good that model is on the training data.

Maximizing is minimizing the negative

Some problems ask for the largest value (a taste score, a likelihood). We do not need a second theory, because maximizing ff and minimizing −f-f have the same answer:

arg⁡max⁡θf(θ)=arg⁡min⁡θ(−f(θ))\arg\max_{\boldsymbol{\theta}} f(\boldsymbol{\theta}) = \arg\min_{\boldsymbol{\theta}} \big(-f(\boldsymbol{\theta})\big)

Flipping the sign turns every hill into a valley. The largest value of ff is minus the smallest value of −f-f. Example: a coin shows heads, heads, tails. The probability of that sequence, if the chance of heads is θ\theta, is θ2(1−θ)\theta^2(1-\theta) (P1.D10 calls it the likelihood). It is largest at θ=2/3\theta = 2/3, where it equals 4/27≈0.1484/27 \approx 0.148. Minimizing −θ2(1−θ)-\theta^2(1-\theta) over θ\theta between 00 and 11 gives the same θ=2/3\theta = 2/3. Taking a logarithm, which keeps the order of the values, turns the probability into a negative log-likelihood, −ln⁡ ⁣(θ2(1−θ))-\ln\!\big(\theta^2(1-\theta)\big), whose smallest value is 1.911.91 at the same θ\theta. That is the sense in which “maximize the likelihood” and “minimize the negative log-likelihood” are one problem (P1.D10).

A landscape you can turn

Plot the loss over all the parameters and you get a loss landscape: the parameters are the position on the ground, and the loss is the height. A good choice of parameters is low ground. With two parameters the landscape is a surface in 3-D, so you can see all of it. With the P0.2 loss it is

L(w,b)=13[(w+b−2)2+(2w+b−3)2+(3w+b−7)2]\begin{gathered} \mathcal{L}(w, b) = \tfrac{1}{3}\Big[(w + b - 2)^2 \\ \qquad + (2w + b - 3)^2 \\ \qquad + (3w + b - 7)^2\Big] \end{gathered}

Each bracket is the squared error on one of the three points (P0.2). Here are a few heights, computed from the formula:

(w,b)(w, b)L\mathcal{L}
(0,0)(0, 0)20.6720.67
(1,0)(1, 0)66
(3,−1)(3, -1)1.671.67
(2.5,−1)(2.5, -1)0.50.5

The point (1,0)(1, 0) is the start of the P0.2 worked example, and L=6\mathcal{L} = 6 there is the loss you computed by hand. The lowest point, (2.5,−1)(2.5, -1) with L=0.5\mathcal{L} = 0.5, is the best line.

A loss landscape you can turn

ℒ(w, b) = ((w + b − 2)² + (2w + b − 3)² + (3w + b − 7)²) / 3
This is the mean squared error of the line ŷ = w·x + b on the three points of P0.2.

point (w, b) = (1, 0)
height ℒ = 6
lowest in this box: 0.5, one lowest point, w = 2.5 and b = −1
slope length ‖∇‖ here = 10.77 (zero exactly at flat points)

Height 6, which is 5.5 above the lowest height in this box (0.5). The slope here has length 10.77, so this is not a flat point.

Drag the picture sideways to turn it (with a mouse or pen, up and down tilts it; on a touch screen use the Tilt slider, so the page can still scroll). Or focus it and press the arrow keys. The blue dot is your point, with a dashed line down to the floor; markers are drawn on top of the surface, even where a hill hides them. Each axis is scaled to fit: the surface is drawn from w = 0 to 4 and b = −2 to 2.

A loss landscape, as a turnable 3-D surface or as a contour map from above. The blue point is yours to move; the readouts give its height and the lowest height. Green marks the lowest points (for the Saddle, only the lowest inside the box), diamonds mark other flat points.

Things to try:

  1. Start with the P0.2 loss. Turn the surface. It is a long, narrow valley with one lowest point. Press Go to a lowest point and read the height: 0.50.5. (P1.C8 measured how curved the valley is: 11.0911.09 in the sharpest direction and 0.240.24 along the valley floor, so the floor is nearly flat.)
  2. Switch to the contour map. The thin lines connect points of equal height (P1.C5). Where they are close together the ground is steep; they shrink to a point around the lowest spot. Drag the point to another place and watch the height change.
  3. Choose Stretched bowl. This is the bowl x2+10y2x^2 + 10y^2 of P1.F3, ten times steeper in one direction. The lowest point is (0,0)(0, 0).
  4. Choose Two valleys. The valleys have different depths: the right one is a local minimum and the left one the global minimum (more below). Press Go to the local min and read the status.
  5. Choose Saddle and Two multiplied weights, and read the status at every flat point you find.

The same landscape, one parameter at a time

You cannot always draw a surface, but you can always look along a line through it. Fix b=−1b = -1 and let only ww move. The surface turns into a curve, a slice:

ww00112.52.544
L(w,−1)\mathcal{L}(w, -1)29.6729.6711110.50.51111

With a bit of algebra this slice is L(w,−1)=143(w−2.5)2+0.5\mathcal{L}(w, -1) = \tfrac{14}{3}(w - 2.5)^2 + 0.5: a one-parameter bowl, the same kind of curve as in P1.F3. The slice along w=2.5w = 2.5 is L(2.5,b)=(b+1)2+0.5\mathcal{L}(2.5, b) = (b + 1)^2 + 0.5. Both slices have their lowest point at the same place, because the lowest point of the surface lies on both lines. A slice is a cut through the landscape, and slices are the only way to look at landscapes with more than two parameters.

Kinds of low points

Not every flat or low spot is “the minimum”. Four words cover the cases.

  • A global minimum is a point θ∗\boldsymbol{\theta}^* whose loss is less than or equal to the loss at every other point.
  • A local minimum is a point whose loss is less than or equal to the loss at every point close enough to it. Zoomed in, it looks like a global minimum; zoomed out, a lower point may exist elsewhere. A global minimum is also a local one.
  • A maximum is the same idea turned upside down: a hilltop.
  • A flat point (also called a critical point or stationary point) is a point where the gradient is zero (P1.C5): to first order the ground is level in every direction. A minimum of a smooth loss (away from the edge of the region we search) is a flat point, and so is a maximum. But a flat point does not have to be either. At a saddle the ground goes up in some directions and down in others, like a mountain pass or a horse saddle.

Which of the three a flat point is depends on how the ground curves, and that is measured by the Hessian (P1.C8): curving up in every direction gives a minimum, curving down in every direction a maximum, a mixture a saddle, and if some direction is exactly flat the Hessian cannot decide (P1.C8). The Saddle landscape in the widget, x2−y2x^2 - y^2, is flat at (0,0)(0, 0), yet that point is neither a minimum nor a maximum.

The Two valleys landscape shows why this matters. It is a made-up function, f(x,y)=(x2−1)2+0.3x+y2f(x, y) = (x^2 - 1)^2 + 0.3x + y^2. Its left valley floor is at x≈−1.036x \approx -1.036 with f≈−0.305f \approx -0.305. Its right valley floor is at x≈0.960x \approx 0.960 with f≈0.294f \approx 0.294. Both are local minima, because every point close to either one is higher. Only the left one is the global minimum. Between them, at x≈0.075x \approx 0.075, is a pass where the gradient is also zero: it is a minimum along yy but a maximum along xx, so it is a saddle. A search that stops at the first flat point it meets may stop in the right valley. P1.F2 studies this in detail; for now, remember that every global minimum is a local minimum, but a local minimum need not be global.

Go deeper The exact definition of a local minimum

A point θ∗\boldsymbol{\theta}^* is a local minimum of L\mathcal{L} if there is some distance r>0r > 0 such that L(θ∗)≤L(θ)\mathcal{L}(\boldsymbol{\theta}^*) \le \mathcal{L}(\boldsymbol{\theta}) for every θ\boldsymbol{\theta} with ∥θ−θ∗∥<r\lVert\boldsymbol{\theta} - \boldsymbol{\theta}^*\rVert < r. The distance rr may be tiny. It is a global minimum if the inequality holds for every θ\boldsymbol{\theta}, with no distance limit.

It is a strict local minimum if “less than or equal to” can be replaced by “less than” for all θ≠θ∗\boldsymbol{\theta} \ne \boldsymbol{\theta}^* close by. Without strictness, a flat floor makes every point of the floor a local minimum at once. That is what happens on the valley floor in the example further down.

Does a lowest point exist? Is it only one?

So far every landscape had a lowest point. Two things can go wrong.

It may not exist. The function f(x)=exf(x) = e^x on all real numbers is positive and gets as close to 00 as you like (e−5=0.0067e^{-5} = 0.0067, e−20=2.1×10−9e^{-20} = 2.1 \times 10^{-9}), but it never reaches 00. There is no smallest value. What it does have is a greatest lower bound, 00, called the infimum. A real example: logistic regression (P3.A7) on data that one straight line separates perfectly. For a single example with label 11 and input 11, the loss is −ln⁡σ(w)-\ln\sigma(w). It is 0.6930.693 at w=0w = 0, 0.1270.127 at w=2w = 2 and 0.00670.0067 at w=5w = 5, and it keeps falling as ww grows, but never reaches 00. The optimizer can only follow the loss toward 00 and stop by some other rule (see below). The Saddle landscape is another case: it keeps falling past the edge of the box in the widget, so it has no lowest point either.

It may not be unique. Take the tiny model y^=w1w2x\hat{y} = w_1 w_2 x with two parameters multiplied together, and one example x=1x = 1, y=2y = 2. The loss is (w1w2−2)2(w_1 w_2 - 2)^2, and it is exactly 00 whenever w1w2=2w_1 w_2 = 2: for (1,2)(1, 2), (2,1)(2, 1), (0.8,2.5)(0.8, 2.5) and (−1,−2)(-1, -2) alike. Those points form two curves in the widget (the Two multiplied weights landscape), a valley floor where every point is equally low. There is one minimum value, 00, but infinitely many argmin points. The same thing happens in real networks. Swap two hidden neurons (with all their incoming and outgoing weights) and the network computes exactly the same function, so a good set of weights typically comes with copies. That is why the argmin θ^\hat{\boldsymbol{\theta}} is better thought of as a set of lowest points, written arg⁡min⁡\arg\min with a set-valued meaning (P1.A4 noted that ties are possible).

The same landscape has a flat point at (0,0)(0, 0) where L=4\mathcal{L} = 4. The gradient is zero there, but nearby the loss goes down along one direction (toward the valley) and up along another, so it is a saddle, not a minimum.

Go deeper Two hidden units, swapped: a numerical check

Take a small network with two hidden units, y^=a1tanh⁡(w1x)+a2tanh⁡(w2x)\hat{y} = a_1 \tanh(w_1 x) + a_2 \tanh(w_2 x), with (a1,w1,a2,w2)=(1.5,0.7,−0.4,2.0)(a_1, w_1, a_2, w_2) = (1.5, 0.7, -0.4, 2.0). At x=−1,0.5,2x = -1, 0.5, 2 it predicts −0.5209-0.5209, 0.19990.1999, 0.92830.9283. Now swap the two units: (a1,w1,a2,w2)=(−0.4,2.0,1.5,0.7)(a_1, w_1, a_2, w_2) = (-0.4, 2.0, 1.5, 0.7). The predictions are identical, because addition does not care about the order of its terms. So the two parameter settings have the same loss on any data, and whenever one is a minimum so is the other. A layer with nn hidden units can be ordered in n!n! ways; for five units that is 120.

Real models: too many parameters to draw

Two parameters give a surface you can hold. A real model has millions or billions of parameters, so its landscape lives in a space with that many directions. Nobody can draw it. What researchers show instead are slices: pick a point (often a trained set of weights) and two directions in parameter space, and plot the loss over the plane they span. A well-known 2018 study of this kind (Li and co-authors, “Visualizing the Loss Landscape of Neural Nets”) introduced a way of scaling the directions (called filter normalization) so that slices of different networks can be compared. Such pictures are real information about that plane, and they can mislead about the rest of the space. A smooth bowl in one plane says nothing certain about the other directions. This lesson shows no picture of a real network’s landscape, only the made-up and small ones above, and says so.

Go deeper Why a slice of a big landscape can mislead

A slice along two directions d1\mathbf{d}_1 and d2\mathbf{d}_2 from a point θ0\boldsymbol{\theta}_0 is the function g(s,t)=L(θ0+s d1+t d2)g(s, t) = \mathcal{L}(\boldsymbol{\theta}_0 + s\,\mathbf{d}_1 + t\,\mathbf{d}_2) of two numbers. It is exact on that plane. But a plane is a tiny part of a space with millions of directions. A point that looks like a minimum in the plane can still have a downhill direction outside it, and a mountain in the plane can be a pass in the full space. Slices are good for comparing landscapes under the same recipe, and weak for proving that a point is a minimum. The cut you saw above, b=−1b = -1, is the simplest case: one direction.

A landscape with a million parameters cannot be scanned, so how do we find low ground? There are three basic strategies.

1. Grid search: try everything on a grid. Choose mm values for each of the dd parameters and evaluate the loss at every combination. For P0.2 with m=41m = 41 values of ww from 00 to 44 and 41 values of bb from −2-2 to 22, that is 412=168141^2 = 1681 evaluations, and the best one is (2.5,−1)(2.5, -1) with loss 0.50.5 (the code below runs it). With three parameters it is 413=68,92141^3 = 68{,}921. With ten it is 4110≈1.3×101641^{10} \approx 1.3 \times 10^{16}, far too many to compute. With one million parameters the count is a number with more than 1.6 million digits. The cost mdm^d grows exponentially in the number of parameters dd. It is the same ndn^d count you met for integrals on a grid (P1.C9). The fix there was to sample random points instead (Monte Carlo, P1.D9). Grid search is only reasonable for very few parameters.

2. Random search: try random points. Draw points at random and keep the best. It does not need a grid, and the cost does not explode in the same way, but in a high-dimensional space a random point is typically far from low ground. It works well when only a few of the numbers really matter. That is why it is a common way to search hyperparameters (P4.23), where there are few of them and no gradient is available.

3. Follow the slope. Stand at a point, find which direction goes downhill, take a step, repeat. This is local search, and with the gradient as the slope it is gradient descent (P1.F3). Each step uses information about the ground right here, so it does not waste evaluations on faraway regions. Computing the gradient for all dd parameters costs a small multiple of one loss evaluation, not dd evaluations, when backpropagation (P4.8) does it.

Run the comparison yourself. The test loss below is the friendly bowl f(θ)=∑iθi2f(\boldsymbol{\theta}) = \sum_i \theta_i^2, with its lowest value 00 at θ=0\boldsymbol{\theta} = \mathbf{0}. Random search gets 1000 random points in the cube [−1,1]d[-1, 1]^d. Gradient descent takes 50 steps with η=0.1\eta = 0.1 from a random start.

Try it: random search against following the slope

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The output (medians over 20 runs): at d=2d = 2, random search reaches 0.00050.0005 and gradient descent 1.5×10−101.5 \times 10^{-10}. At d=10d = 10 the numbers are 0.740.74 against 7.6×10−107.6 \times 10^{-10}. At d=100d = 100 random search has only moved from 33.733.7 to 23.923.9, while gradient descent is at 6.9×10−96.9 \times 10^{-9}. This bowl is the easiest possible landscape, so read the table as the effect of the dimension, not as a promise that gradient descent always works that well. On a landscape with many valleys it finds a nearby valley (P1.F3), not necessarily the best one.

When do we stop?

An optimizer needs a rule for when to stop. Three common ones, often used together:

  • Small gradient. Stop when ∥∇L∥\lVert\nabla\mathcal{L}\rVert is below a small threshold, because the ground is nearly flat. This is a reason to expect a flat point, but flat does not mean minimum, as the saddle showed.
  • No more improvement. Stop when the loss has not fallen by a useful amount over some number of steps.
  • A budget. Stop after a fixed number of steps or minutes. For large networks this is the usual rule.

A low training loss is not the final goal. The optimizer’s job is to make the loss small on the training data. What we care about is the loss on new data. A model with many parameters can reach a training loss near zero by memorizing its examples, and then do badly on new ones (P0.2 showed this with the wiggly curve). So there are two separate errors. The optimization error is how far the training loss is from its lowest possible value. The generalization error is the loss on new data, and the generalization gap is how much worse the model does on new data than on training data. Solving the optimization problem perfectly does not solve the second one. That is why we hold out validation and test data (P3.A4) and why the bias–variance view exists (P3.A5).

A related fact: the number we optimize is not always the number we report. Accuracy is a step function of the parameters, flat nearly everywhere, so it gives no slope to follow. Classifiers are therefore trained on a smooth stand-in, such as cross-entropy (P4.6), and judged on accuracy afterwards.

Optimization in the rest of the course

  • Training = minimizing a loss over the weights. Linear regression and neural networks follow the P0.2 recipe: pick a loss, then search the weights (P3.A2, P4.6, P2.7). Some classic methods, such as decision trees, learn in other ways.
  • Maximum likelihood and MAP. Maximum likelihood (P1.D10) chooses the parameters that maximize the probability of the data; with the sign flip above it is minimizing the negative log-likelihood. MAP (P1.D11) adds the negative log prior to this loss as a penalty, and a Gaussian prior on the weights adds an L2 penalty (P3.A6).
  • Hyperparameter search is an outer problem: the objective is the validation loss, the parameters are things like η\eta, and each evaluation means a whole training run. It has no cheap gradient, which is why grid, random and Bayesian search appear there (P4.23).
  • Constraints. Sometimes the parameters may not be chosen freely: probabilities must be non-negative and add to 11, a weight vector may be required to have a fixed length. That is constrained optimization, with its own tools (P1.F7).

Intuition

You stand on hilly ground in fog. The parameters are your position, the loss is your height, and your goal is the lowest ground. You cannot see the map. You can feel the slope under your feet, and you can check your height at any place you go. Grid search is walking to every point of a checkerboard. Random search is teleporting to random places. Following the slope is walking downhill.

Where the picture breaks: a real model’s ground has millions of directions, not two, and the “height” of every place costs a computation, so a long walk is expensive. The ground can also have flat floors with no unique lowest point, and some landscapes keep falling forever.

Math

An unconstrained optimization problem is min⁡θ∈RnL(θ)\min_{\boldsymbol{\theta}\in\mathbb{R}^n} \mathcal{L}(\boldsymbol{\theta}). With data (xi,yi)(\mathbf{x}_i, y_i) for i=1…Ni = 1\dots N and a model y^=h(x;θ)\hat{y} = h(\mathbf{x}; \boldsymbol{\theta}), the objective is the average loss

L(θ)=1N∑i=1NL(h(xi;θ), yi)\mathcal{L}(\boldsymbol{\theta}) = \frac{1}{N}\sum_{i=1}^{N} L\big(h(\mathbf{x}_i; \boldsymbol{\theta}),\, y_i\big)

and the data (xi,yi)(\mathbf{x}_i, y_i) are constants. For a smooth L\mathcal{L}, a local minimum has ∇L=0\nabla\mathcal{L} = \mathbf{0} (and a Hessian with no negative eigenvalue). The reverse does not hold: a flat point can be a saddle (P1.C8). Equivalences: max⁡θf=−min⁡θ(−f)\max_{\boldsymbol{\theta}} f = -\min_{\boldsymbol{\theta}}(-f), and for f>0f > 0, arg⁡max⁡f=arg⁡min⁡(−ln⁡f)\arg\max f = \arg\min(-\ln f) because the logarithm is increasing.

A grid with mm values for each of dd parameters has mdm^d points.

Code

The P0.2 loss as a function of the parameters, a grid search, the argmin against the min, a one-parameter slice, and the cost of grids in more dimensions.

Try it: the loss as a function of the parameters, and a grid search

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Expected output: the three losses are 20.666720.6667, 6.06.0 and 0.50.5. The grid minimum is 0.50.5 at (2.5,−1.0)(2.5, -1.0). The slice prints 29.66729.667, 11.00011.000, 1.6671.667, 0.5000.500, 1.6671.667, 11.00011.000. The grid costs are 1.68×1031.68 \times 10^3, 6.89×1046.89 \times 10^4, 1.34×10161.34 \times 10^{16} and 1.9×101611.9 \times 10^{161}.

Go deeper The loss you optimize and the metric you report

The thing we want, such as the share of correct answers (accuracy), often cannot be optimized directly. Accuracy changes in jumps as the parameters move, and it is flat between the jumps, so its gradient is zero almost everywhere and tells the optimizer nothing. The usual fix is a surrogate loss: a smooth function that is small when the model does well, for example cross-entropy for classification (P4.6). We optimize the surrogate and report the metric. The two usually move together, but a lower surrogate loss does not promise a higher accuracy.

Go deeper When no minimum exists: the infimum

A set of numbers may have no smallest member but still have a greatest lower bound, the infimum: the largest number that is still below or equal to all of them. The values of exe^x are all positive, and no positive number is a lower bound larger than 00, so the infimum is 00, but no xx produces 00. We write inf⁡xex=0\inf_x e^x = 0, and say the infimum is not attained. A minimum is an infimum that is attained.

Optimization problems that have an infimum but no minimum are common in the “perfect fit” case (the separable logistic loss) and make an optimizer keep pushing a weight larger and larger. Practical remedies are adding a penalty on the size of the weights (P3.A6) and stopping by one of the rules above.

Misconceptions

Check yourself

Check your understanding

  1. You minimize the mean squared error of the line y^=wx+b\hat{y} = wx + b on a fixed set of examples. What does the optimization change?

  2. The P0.2 loss has its smallest value, 0.50.5, at (w,b)=(2.5,−1)(w, b) = (2.5, -1). Which of these is the arg⁡min⁡\arg\min?

  3. A coin shows heads, heads, tails. The likelihood of that result is θ2(1−θ)\theta^2(1 - \theta), largest at θ=2/3\theta = 2/3. For θ\theta between 00 and 11, which minimization gives the same answer?

  4. A search stops at a point where every nearby point has a higher loss. What can you say for sure?

  5. A grid search tries 1010 values for each of 66 parameters. How many loss evaluations does it need?

  6. After training, the loss on the training set is nearly 00, but the loss on held-out data is large. Which statement fits best?

Exercises

  1. Evaluate the P0.2 loss at (w,b)=(3,−1)(w, b) = (3, -1).
    Show answerthe errors are 3−1−2=03 - 1 - 2 = 0, 6−1−3=26 - 1 - 3 = 2 and 9−1−7=19 - 1 - 7 = 1, so L=(0+4+1)/3=5/3≈1.67\mathcal{L} = (0 + 4 + 1)/3 = 5/3 \approx 1.67, higher than the minimum 0.50.5.
  2. Fix w=2.5w = 2.5 and use the slice L(2.5,b)=(b+1)2+0.5\mathcal{L}(2.5, b) = (b + 1)^2 + 0.5. What is L\mathcal{L} at b=0b = 0, and for which bb is the slice lowest?
    Show answerat b=0b = 0 it is 1+0.5=1.51 + 0.5 = 1.5. It is lowest at b=−1b = -1, where it is 0.50.5.
  3. Turn the maximization of f(x)=5−(x−3)2f(x) = 5 - (x - 3)^2 into a minimization, and solve it.
    Show answerminimize −f(x)=(x−3)2−5-f(x) = (x - 3)^2 - 5. Its argmin is x=3x = 3 and its minimum value is −5-5. So ff has its maximum at x=3x = 3, with value 55.
  4. Does f(x)=1/xf(x) = 1/x on x>0x > 0 have a minimum? A maximum?
    Show answerno minimum: it gets as close to 00 as you like as xx grows, but never reaches it (infimum 00, not attained). It has no maximum either, because it grows without limit as xx approaches 00.
  5. A grid search uses 2020 values for each of 55 parameters. How many evaluations? If each takes 11 millisecond, how long is that?
    Show answer205=3,200,00020^5 = 3{,}200{,}000 evaluations. At 11 ms each that is 32003200 seconds, about 5353 minutes.
  6. For L(w1,w2)=(w1w2−2)2\mathcal{L}(w_1, w_2) = (w_1 w_2 - 2)^2, is (1,2)(1, 2) a minimum? Is it the only one?
    Show answeryes: L(1,2)=0\mathcal{L}(1, 2) = 0 and a squared number cannot be below 00, so it is a global minimum. It is not the only one: (2,1)(2, 1) and (−1,−2)(-1, -2) also give 00, as does any point with w1w2=2w_1 w_2 = 2.
  7. For f(x,y)=x2−y2f(x, y) = x^2 - y^2, the gradient is (2x,−2y)(2x, -2y). What kind of point is (0,0)(0, 0)?
    Show answerthe gradient is zero there, so it is a flat point. Along the xx axis f=x2≥0f = x^2 \ge 0 and along the yy axis f=−y2≤0f = -y^2 \le 0, so it is neither a minimum nor a maximum: it is a saddle.
  8. For the coin with heads, heads, tails, compute the negative log-likelihood −ln⁡(θ2(1−θ))-\ln\big(\theta^2(1-\theta)\big) at θ=0.5\theta = 0.5 and at θ=2/3\theta = 2/3. Which is smaller?
    Show answerat 0.50.5 the likelihood is 0.1250.125, so the value is 2.0792.079. At 2/32/3 the likelihood is 4/27=0.1484/27 = 0.148, so the value is 1.9101.910. The value at 2/32/3 is smaller, which is the maximum likelihood choice.
  9. Training the line y^=wx+b\hat{y} = wx + b on three points with learning rate 0.050.05 for 200200 steps: name the parameters, the data and the hyperparameters.
    Show answerparameters: ww and bb. Data: the three points. Hyperparameters: the learning rate 0.050.05 and the number of steps 200200. The form of the model, a line, is also fixed.

Next: the Two valleys landscape asks when a search ends in the right valley and when in the left. P1.F2 looks at which landscapes have only one valley (convex ones) and which have many. Then P1.F3 shows the step-by-step method that walks downhill on all of them.

Key takeaways

  • Optimization means choosing inputs (here, the parameters) to make an objective as small as possible, or as large as possible. In ML the objective is usually the loss, averaged over the training data.
  • What is fixed while we optimize: the data, the form of the model, and the hyperparameters. What we change: the parameters. The loss is a function of the parameters, not of the data.
  • The minimum is a value; the argmin is where it happens. For the P0.2 loss the minimum is 0.5 and the argmin is (w,b)=(2.5,−1)(w, b) = (2.5, -1).
  • Maximizing ff is the same problem as minimizing −f-f: the argmax of ff is the argmin of −f-f. Maximum likelihood is minimizing the negative log-likelihood.
  • A global minimum is the lowest point of all. A local minimum is only the lowest in its neighbourhood. A flat point (zero gradient) can be a minimum, a maximum or a saddle.
  • A minimum may not exist (the exponential exe^x has none) and may not be unique (the product w1w2w_1 w_2 has a whole curve of lowest points).
  • A loss landscape is the loss drawn as a surface over parameter space. Real models have too many parameters to draw, so we only ever see slices.
  • Grid search costs mdm^d evaluations for mm values of each of dd parameters. Following the slope (gradient descent, P1.F3) is how large models are trained.
  • Optimization error is not generalization error: a lower training loss does not promise a lower loss on new data.
Found a mistake? Tell us