← MAIN
LAB // OPTIMIZATION

OPTIMIZER RACE

Six optimizers start from one point on a loss landscape. Some settle at a minimum, and some blow up.
STEP 0 BEST LOSS — SETTLED 0/0

HOW TO USE

The goal is to see which optimizer reaches the bottom of a loss landscape, and which ones fail on the way. Every track starts at the same point and moves downhill by its own rule.

Press RUN RACE to watch all six, or STEP to advance one step at a time. Click the map to move the start point, and pick a surface in LANDSCAPE.

Each track leaves a trail. Its badge turns green when it settles at a minimum, or pink when it diverges. Hover the map to read the loss and the gradient at the cursor.

Click the map to drop a start point. Hover to read the gradient at the cursor.
hover the map
LOSS VS STEP
Race log
LANDSCAPE
SURFACE
OPTIMIZERS
More settings
MOMENTUM AND ADAM
MOMENTUM
ADAM β₁
ADAM β₂
EPSILON (ADAM, RMSPROP, ADAGRAD)
RMSPROP ρ
NOISE, SEED AND SPEED
GRADIENT NOISE
NOISE SEED (same seed, same jitter for every optimizer)
SPEED

HOW IT WORKS

A loss landscape is a function f(x, y) that gives a loss to every point of the plane, and an optimizer looks for a point where the loss is low. Each optimizer here uses the gradient \nabla f, the vector of partial derivatives that points uphill, and moves downhill from the current point. The optimizers differ in the size of each step and in the past gradients they remember. Every track starts from the same point, and when noise is on, every track sees the same noise draw at each step, so the differences come from the update rules.1

Gradient descent (SGD)

Gradient descent moves the position against the gradient. The step size is the learning rate \eta, a positive number that each track has its own copy of, or one shared value for all tracks. The race calls the method SGD, the name used when the gradient may carry noise. With gradient noise at 0 the gradient is exact, and the method is plain gradient descent.23

x_{t+1} = x_t - \eta\, g_t
23

Here x_t = (x, y) is the position at step t, and g_t is the gradient the optimizer uses at that step, which includes any noise (see Gradient noise below). The same rule is applied to both coordinates. The default learning rate for SGD is \eta = 0.05.

On a bowl every step shrinks the distance to the minimum, so the track converges. A ravine is ill conditioned: its curvature is 25 across the valley and 1 along it, so its gradient in y is 25y.4 Applied to the y coordinate, one step multiplies y by 1 - 25\eta. With \eta = 0.05 that factor is 1 - 1.25 = -0.25. The sign flips at every step and the size falls to a quarter, which is the zigzag across the ravine. Along the valley the factor is 1 - \eta = 0.95, so progress is slow.

y_{t+1} = (1 - 25\eta)\, y_t = -0.25\, y_t \qquad (\eta = 0.05)
42

Example. Starting from y = 0.9, one step gives y = -0.25 \times 0.9 = -0.225. The iterate has crossed the valley floor at y = 0 and landed on the other side, closer to the floor than before.

23
function GRADIENT-DESCENT(x, η, steps):
    repeat steps times:
        g ← ∇f(x)                                # gradient at the current point
        x ← x − η g                              # step against the gradient
    return x

The full version is the update the race applies to an SGD track. The gradient g is computed by the race, at the query point q, with noise added (see Race step below). Here q = x because SGD does not look ahead, and the update returns a new position instead of changing x in place.2

Momentum and Nesterov

Momentum, also called heavy-ball momentum, keeps a velocity v that adds up past steps. A velocity that is kept between steps lets the track build speed along a consistent direction and damp the zigzag across a ravine. The coefficient \beta is the fraction of the old velocity that is kept at each step, and its default is 0.9.56

v_{t+1} = \beta\, v_t - \eta\, g_t, \qquad x_{t+1} = x_t + v_{t+1}
56

Here v starts at (0, 0) and the gradient g_t is taken at x_t. Each step keeps the fraction \beta of the velocity, so with the default \beta = 0.9 a gradient contributes to about 1/(1 - \beta) = 10 later steps. The default learning rate is \eta = 0.02, smaller than SGD's, because the velocity adds up.

Nesterov momentum, or Nesterov accelerated gradient, is the same rule with the gradient taken at the look-ahead point q_t = x_t + \beta v_t. That is the point the velocity alone would carry the track to, so the gradient there is the one the track is about to meet.78

q_t = x_t + \beta\, v_t, \qquad v_{t+1} = \beta\, v_t - \eta\, g(q_t), \qquad x_{t+1} = x_t + v_{t+1}
78

Example. With \eta = 0.02 and \beta = 0.9, a constant slope of 1 makes the velocity approach -\eta / (1 - \beta) = -0.2 per step. Plain gradient descent with the same \eta moves 0.02 per step, so the momentum track moves ten times as far.

910
function MOMENTUM-STEP(x, v, η, β):
    g ← ∇f(x)                                    # gradient at the current point
    v ← β · v − η · g                            # keep a fraction β of the old velocity
    x ← x + v                                    # move by the new velocity
    return x, v

The race evaluates the gradient at the query point q before the update, so the same function covers both methods: classic momentum queries at x, and Nesterov queries at the look-ahead point. The position update uses the velocity that was just computed for that coordinate. The code keeps the old position and returns a new one, so the update never reads a half-changed value.9

RMSProp

RMSProp divides each coordinate's step by the root of a running average of its squared gradients. The average is an exponential moving average with decay \rho = 0.9: each older squared gradient is weighted by a further power of \rho, so gradients older than about 1/(1 - \rho) = 10 steps have little influence. A steep coordinate has large squared gradients, so its step is shrunk. A gentle coordinate has small ones, so its step is not.1112

s_{t+1} = \rho\, s_t + (1 - \rho)\, g_t^2, \qquad x_{t+1} = x_t - \frac{\eta\, g_t}{\sqrt{s_{t+1}} + \varepsilon}
1112

Here s is kept for each coordinate and starts at 0. The square g_t^2 is taken coordinate by coordinate. The constant \varepsilon is added under the root so that the step never divides by zero; the default is \varepsilon = 10^{-8}. The defaults are \eta = 0.05 and \rho = 0.9.

When the gradient changes slowly, g / \sqrt{s} is close to \pm 1, so each step is about \eta in size whatever the gradient's scale. With \eta fixed, the step does not shrink to zero near a minimum, so the track hovers at a distance set by \eta. On the ravine, the RMSProp track is at about (-0.025, 0.025) after 500 steps and never reaches the gradient tolerance.

1112
function RMSPROP-STEP(x, s, η, ρ, ε):
    g ← ∇f(x)                                    # gradient at the current point
    s ← ρ · s + (1 − ρ) · g · g                  # running average of squared gradients
    x ← x − η · g / (sqrt(s) + ε)                # steps shrink where s is large
    return x, s

The full version updates s before it computes the step, so the step uses the average that includes the current gradient. Each coordinate has its own s, which is why a steep direction and a gentle one can get different step sizes in the same move.13

AdaGrad

AdaGrad divides by the root of the sum of all past squared gradients, with no decay. The sum is kept as G for each coordinate. Because G only grows, the effective step falls over time, so AdaGrad's default learning rate of 0.5 is large to begin with.1415

G_{t+1} = G_t + g_t^2, \qquad x_{t+1} = x_t - \frac{\eta\, g_t}{\sqrt{G_{t+1}} + \varepsilon}
1415

Here G starts at 0 for each coordinate, and \varepsilon = 10^{-8} is the same constant as in RMSProp. The default \eta = 0.5 is used as is.

Example. If the gradient stays near 1, G after n steps is about n, so the step is about 0.5 / \sqrt{n}: 0.5 at step 1 and about 0.16 at step 10.

1415
function ADAGRAD-STEP(x, G, η, ε):
    g ← ∇f(x)                                    # gradient at the current point
    G ← G + g · g                                # sum of every squared gradient so far
    x ← x − η · g / (sqrt(G) + ε)                # the divisor never shrinks
    return x, G

The full version updates G before the step, as RMSProp does with s, but it adds the squares and never multiplies the old sum by a decay factor. That absence of decay is the whole difference between AdaGrad and RMSProp.16

Adam

Adam (adaptive moment estimation) combines the two ideas above. It keeps a decaying mean m of the gradients, which plays the part of momentum, and a decaying mean v of the squared gradients, which plays the part of RMSProp. Both start at zero, so early on they are biased toward zero. Adam corrects this with a bias correction, which divides each mean by 1 - \beta^t.1718

m_{t} = \beta_1\, m_{t-1} + (1 - \beta_1)\, g_t, \qquad v_{t} = \beta_2\, v_{t-1} + (1 - \beta_2)\, g_t^2
1920
\hat m_t = \frac{m_t}{1 - \beta_1^t}, \qquad \hat v_t = \frac{v_t}{1 - \beta_2^t}, \qquad x_{t+1} = x_t - \frac{\eta\, \hat m_t}{\sqrt{\hat v_t} + \varepsilon}
2122

Here t counts steps from 1, g_t is the gradient, and \beta_1 = 0.9 and \beta_2 = 0.999 are the decay rates of the two means. These are the values from the Adam paper. The learning rate \eta = 0.1 and the constant \varepsilon = 10^{-8} are the page's defaults. The code keeps \beta_1^t and \beta_2^t as running products, one multiplication per step, instead of calling a power function.23

Example. On step 1, \hat m_1 = g_1 and \hat v_1 = g_1^2, so each coordinate moves by \eta\, g_1 / |g_1| = \eta, up to \varepsilon. The first step is therefore about \eta in size per coordinate, whatever the scale of the gradient. The default \eta = 0.1 is much larger than the 0.001 suggested in the Adam paper, since the lab's surfaces are small two-dimensional problems where a larger step settles within a few hundred steps.

1718
function ADAM-STEP(x, m, v, t, η, β₁, β₂, ε):
    g ← ∇f(x)                                    # gradient at the current point
    m ← β₁ · m + (1 − β₁) · g                    # mean of the gradients (momentum)
    v ← β₂ · v + (1 − β₂) · g · g                # mean of the squared gradients (RMSProp)
    m̂ ← m / (1 − β₁^t)                           # bias correction: both means start at 0
    v̂ ← v / (1 − β₂^t)
    x ← x − η · m̂ / (sqrt(v̂) + ε)
    return x, m, v

The two running products are multiplied once per step, before the loop over coordinates, so every coordinate of a track sees the same \beta_1^t and \beta_2^t. Each coordinate keeps its own m and v. The corrected means \hat m and \hat v are computed for each coordinate, and only the stored m and v carry over to the next step.24

Loss landscapes

Each surface is a function f with its gradient written out by hand, and the tests check every gradient against central differences.2526 The map colours each point by its loss on a logarithmic scale, measured above the floor (the known minimum, or the lowest sampled value for a painted surface). The height in the 3D view uses the same value, rescaled to [0, 1].

c(x, y) = \log_{10}\bigl(\max(f(x, y) - f_{\min}, 0) + 0.01\bigr)
2728

Here f_{\min} is the floor, 0.01 keeps the logarithm finite at the floor, and c is then rescaled between the lowest and highest values on the grid to give the colour and the height.29

\begin{aligned} \text{Bowl:}\quad & f = x^2 + y^2 \\ \text{Ravine:}\quad & f = \tfrac12\bigl(x^2 + 25\,y^2\bigr) \\ \text{Saddle:}\quad & f = x^2 - y^2 + \tfrac14\, y^4 \\ \text{Rosenbrock:}\quad & f = (1 - x)^2 + 100\,(y - x^2)^2 \\ \text{Himmelblau:}\quad & f = (x^2 + y - 11)^2 + (x + y^2 - 7)^2 \\ \text{Rastrigin:}\quad & f = 20 + \bigl(x^2 - 10\cos 2\pi x\bigr) + \bigl(y^2 - 10\cos 2\pi y\bigr) \end{aligned}
2530
\begin{aligned} \nabla f_{\text{bowl}} &= (2x,\ 2y) \\ \nabla f_{\text{ravine}} &= (x,\ 25\,y) \\ \nabla f_{\text{saddle}} &= (2x,\ -2y + y^3) \\ \nabla f_{\text{Rosenbrock}} &= \bigl(-2(1 - x) - 400\,x\,(y - x^2),\ 200\,(y - x^2)\bigr) \\ \nabla f_{\text{Himmelblau}} &= (4x\,a + 2b,\ 2a + 4y\,b), \qquad a = x^2 + y - 11,\ b = x + y^2 - 7 \\ \nabla f_{\text{Rastrigin}} &= \bigl(2x + 20\pi \sin 2\pi x,\ 2y + 20\pi \sin 2\pi y\bigr) \end{aligned}
2530

Here \pi is the circle constant and \cos and \sin are taken in radians. The ravine's gradient is (x, 25y) because its loss is half the sum of x^2 and 25y^2. The Himmelblau gradient uses the two terms a and b of its loss.

Every built-in surface has a known minimum value, and the chart uses it as zero. The bowl, the ravine, the Rosenbrock surface and the Rastrigin surface all have f = 0 at their minimum. The saddle surface has its origin at f = 0, which is a saddle point, and its two minima at (0, \pm\sqrt2) with f = -1.31 The Rosenbrock surface is a curved, banana-shaped valley with its single minimum at (1, 1). Himmelblau has four minima with f = 0, at (3, 2), (-2.805118, 3.131312), (-3.779310, -3.283186) and (3.584428, -1.848126). Rastrigin is a regular grid of local minima around one global minimum at the origin.3233

Paint your own adds up to 12 Gaussian bumps to a gentle bowl. Each bump has a centre (c_x, c_y), an amplitude a and a width \sigma. A positive amplitude is a hill and a negative one a valley. The landscape is f = 0.1\,(x^2 + y^2) + \sum_k a_k \exp\!\bigl(-\frac{(x - c_{x,k})^2 + (y - c_{y,k})^2}{2\sigma_k^2}\bigr), and the gradient is the bowl's gradient plus a term for each bump.3435

\nabla f = \Bigl(0.2\,x - \sum_k e_k\,(x - c_{x,k}),\ 0.2\,y - \sum_k e_k\,(y - c_{y,k})\Bigr), \qquad e_k = \frac{a_k}{\sigma_k^2}\exp\!\Bigl(-\frac{(x - c_{x,k})^2 + (y - c_{y,k})^2}{2\sigma_k^2}\Bigr)
3637

The lab starts with a hill of amplitude 3.0 at (0.6, -0.4) with \sigma = 0.7, and a valley of amplitude -2.5 at (-1.2, 1.1) with \sigma = 0.9. Each click adds a bump with amplitude +2.4 (hill) or -2.0 (valley) and width \sigma = 0.55, up to 12 bumps.3839

Conditioning and the Hessian

The Hessian H is the matrix of second derivatives, and it describes the curvature of the landscape. The lab does not write it out. It estimates H from the gradient by central differences, with step h = 10^{-5}, and symmetrises the off-diagonal entry.4041

h_{xx} = \frac{g_x(x + h, y) - g_x(x - h, y)}{2h}, \qquad h_{yy} = \frac{g_y(x, y + h) - g_y(x, y - h)}{2h}, \qquad h_{xy} = \tfrac12\!\left(\frac{g_x(x, y + h) - g_x(x, y - h)}{2h} + \frac{g_y(x + h, y) - g_y(x - h, y)}{2h}\right)
4041

Here g = (g_x, g_y) is the gradient function and the subscripts name the entries of H. Because h_{xy} is averaged over its two estimates, H is symmetric. The test suite checks that the Hessian of the bowl is 2I.42

The condition number \kappa is the ratio of the largest to the smallest eigenvalue of H. The bowl's Hessian is 2I, so \kappa = 1 and one step size fits every direction. The ravine's Hessian is \operatorname{diag}(1, 25), so \kappa = 25. Gradient descent on a quadratic whose largest curvature is L converges only for \eta < 2/L, which is 2/25 = 0.08 on the ravine. A step that is safe across the ravine is therefore too slow along it.

A saddle point has zero gradient but is not a minimum: H has one positive and one negative eigenvalue. At the origin of the saddle surface H = \operatorname{diag}(2, -2).43

Settling and divergence

A track is settled when the gradient norm is below the tolerance 10^{-3} and H is positive definite. For a 2×2 symmetric matrix, that means its determinant and its trace are both positive. The gradient test alone would also accept a saddle or a maximum, and the Hessian test rejects them. The first step at which the test holds is recorded, and the track keeps moving after that.4445

\lVert \nabla f(x) \rVert < 10^{-3} \quad\text{and}\quad h_{xx}\,h_{yy} - h_{xy}^2 > 0 \quad\text{and}\quad h_{xx} + h_{yy} > 0
4445

Here \lVert \cdot \rVert is the Euclidean length of a vector. The tolerance and the determinant and trace tests are the three conditions, and all three must hold.46

A track diverges when its next position leaves the box [-10^6, 10^6]^2, or when the position or the loss is not finite. A finite position outside the box is a blow-up, where the iterate grows each step and runs away. A position or loss that is not finite is a NaN, a value that is not a number, and the lab labels it NaN DETONATION. In both cases the track stops, and the lab marks the failure at its last finite position.474849

\text{diverged} \iff |x'| > 10^6 \ \text{or}\ |y'| > 10^6 \ \text{or}\ \text{the position or } f(x') \text{ is not finite}
484950

Example. On the Rosenbrock surface, plain SGD with \eta = 0.05 goes from (-1.5, 2.0) to (6.25, 4.5) at step 1 and to (-4314.6, 350.1) at step 2. Both are inside the box. At step 3 the next position is far outside it, so the track is marked as a blow-up at step 3, and its last finite point is the one at step 2.51

5253
function IS-MINIMUM(x, gtol):
    g ← ∇f(x)
    if the length of g is not less than gtol:
        return false                             # not flat enough to be a minimum
    H ← HESSIAN(x)                               # numerical second derivatives
    return det(H) > 0 and trace(H) > 0           # positive definite: a bowl, not a saddle

The gradient test comes first, so the Hessian is only estimated near a flat point. The off-diagonal entry h_{xy} is the average of the two finite-difference estimates, which is the same as the code's 0.5 \cdot (a + b). The determinant test separates a bowl from a saddle, since a saddle's determinant is negative.52

5455
function RACE-STEP(race, σ):
    t ← t + 1                                    # one step for the whole race
    ξ ← (NORMAL(), NORMAL())                     # one noise draw, shared by every track
    for each track T in race:
        if T has not diverged:
            q ← the point where T takes its gradient
            g ← ∇f(q) + σ · ξ                    # gradient plus the shared noise
            x' ← the position T's update rule gives from g
            if x' is outside the box or f(x') is not finite:
                T diverges at step t             # blow-up or NaN: the track stops
            otherwise:
                move T to x', record its loss
                if T is settled and not yet recorded: record step t

The noise is drawn once per step and shared by every track, so the tracks differ only by their update rules. Each track checks its new position before it evaluates the loss, so the loss is only computed at a finite point inside the box. A diverged track is never updated again. The race keeps running the other tracks, and the page pauses once every track has either settled or diverged.565758

Gradient noise and the random stream

Gradient noise adds the same random vector to every track's gradient at each step. The noisy gradient is g_t = \nabla f(q_t) + \sigma\, \xi_t, where \sigma is the noise level and \xi_t = (\xi_1, \xi_2) is drawn once per step. With the default \sigma = 0 the race is deterministic. The seed (default 7) starts the random stream, and RANDOM picks a new seed at random.596061

g_t = \nabla f(q_t) + \sigma\, \xi_t
5960

The random stream is mulberry32, a 32-bit generator whose steps are integer operations: addition, exclusive OR, shifts and multiplication modulo 2^{32}. The browser and the Python package compute the same bits, so a seed gives the same race in either. A uniform value u in [0, 1) comes straight from the stream. A normal sample is built from twelve of them, which has mean 0 and variance 1, because each uniform value has variance 1/12.6263

\xi = \sum_{i=1}^{12} u_i - 6, \qquad u_i \in [0, 1)
6465

Here u_i are twelve uniform draws. Each step draws two normal values, one per coordinate, so it uses 24 uniform draws in all. The sum is bounded in [-6, 6].

Default hyperparameters

OptimizerSettingDefaultWhy
SGD66\eta0.05On the ravine, 25\eta = 1.25 is above 1, so the steep direction flips sign each step (the oscillation the lab shows) while the gentle direction still moves.
Momentum66\eta, \beta0.02, 0.9\beta = 0.9 averages about the last 10 gradients. \eta is smaller than SGD's because the velocity adds up to about \eta / (1 - \beta) = 0.2.
Nesterov66\eta, \beta0.02, 0.9The same as momentum, so the two can be compared directly. Only the point where the gradient is taken differs.
RMSProp66\eta, \rho0.05, 0.9\rho = 0.9 forgets squared gradients after about 10 steps. With a fixed \eta it hovers at a size set by \eta, not by the landscape.
Adam66\eta, \beta_1, \beta_2, \varepsilon0.1, 0.9, 0.999, 1e-8The decay rates are the Adam paper's values. \eta is larger than the paper's 0.001 because the lab's problems are small and two-dimensional.
AdaGrad66\eta, \varepsilon0.5, 1e-8Its accumulated squared gradients only grow, so the effective step falls over time. A large \eta makes the early steps useful.
Noise61\sigma, seed0, 7Off by default, so the race is deterministic. When it is on, every optimizer sees the same noise draw at each step.
Tolerance46gradient norm1e-3A track counts as settled once the gradient norm is below this and the Hessian is positive definite.
Divergence47position1e6A track is stopped once it leaves the box [-10^6, 10^6]^2 or its position or loss is not finite.
Hessian step67h1e-5The step of the central differences that estimate the Hessian for the settling test.

Measured at the defaults

Surface and startOptimizerSteps to settleOutcome after 500 steps
Bowl, (2.4, 1.6)SGD / Momentum / Nesterov83 / 119 / 74All settled.
Bowl, (2.4, 1.6)RMSProp / Adam / AdaGrad77 / 146 / 69All settled. RMSProp then hovers at a loss of about 1e-3.
Ravine, (3.2, 0.9)Nesterov86Fastest on the ravine at these settings.
Ravine, (3.2, 0.9)Momentum / SGD / Adam150 / 158 / 155Momentum edges out SGD. Adam is close to SGD here, not clearly faster.
Ravine, (3.2, 0.9)AdaGrad108Settles, between Nesterov and the three slower tracks.
Ravine, (3.2, 0.9)RMSPropneverHovers near (−0.025, 0.025) and does not reach the tolerance.
Rosenbrock, (−1.5, 2.0)SGD / Momentum / Nesterovdiverge at steps 3 / 4 / 3Each one overshoots the banana's steep walls at its default step size.
Rosenbrock, (−1.5, 2.0)RMSProp / Adam / AdaGradnot within 500None settles. Adam ends near (0.90, 0.82) with its loss still falling.

How these were measured: the Python reference (optlab/race.py) with the defaults above, no noise, and the stated start point.68 The browser runs the same arithmetic (web/js/optlab-core.js), and the parity test checks that the two agree to 1e-9 over the scenarios it covers.6970 Numbers depend on the start point and settings. Change either and the ranking can change. Nothing here is tuned for a particular start.