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.
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 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
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.
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.
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
constants: η = 0.05 (per track, from the OPTIMIZERS rows)
function SGD-UPDATE(x, g, η): # the race calls this once per step
q ← x # SGD takes its gradient at x itself
for each i in 1, 2:
x[i] ← x[i] − η · g[i] # the same rule for both coordinates
return x # a new position; the old one is kept
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, 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
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
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.
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
constants: β = 0.9, η = 0.02, v starts at (0, 0)
function MOMENTUM-STEP(x, v, σ, ξ, η, β, nesterov):
if nesterov:
q ← x + β · v # Nesterov: look ahead along the velocity
otherwise:
q ← x # classic momentum: gradient at x
g ← ∇f(q) + σ · ξ # gradient at the query point, with noise
for each i in 1, 2:
v[i] ← β · v[i] − η · g[i] # velocity update
x[i] ← x[i] + v[i] # position moves by the updated 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 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
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.
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
constants: ρ = 0.9, ε = 1e−8, s starts at (0, 0)
function RMSPROP-STEP(x, g, s, η, ρ, ε): # g is the gradient at the query point
for each i in 1, 2:
s[i] ← ρ · s[i] + (1 − ρ) · (g[i] · g[i])
x[i] ← x[i] − η · g[i] / (sqrt(s[i]) + ε)
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 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
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.
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
constants: η = 0.5, ε = 1e−8, G starts at (0, 0)
function ADAGRAD-STEP(x, g, G, η, ε): # g is the gradient at the query point
for each i in 1, 2:
G[i] ← G[i] + g[i] · g[i]
x[i] ← x[i] − η · g[i] / (sqrt(G[i]) + ε)
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 (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
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.
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
constants: β₁ = 0.9, β₂ = 0.999, ε = 1e−8, η = 0.1
# m and v start at (0, 0); β₁ᵗ and β₂ᵗ start at 1
function ADAM-STEP(x, g, m, v, β₁ᵗ, β₂ᵗ, η): # g is the gradient at the query point
β₁ᵗ ← β₁ᵗ · β₁ # running products, not a power function
β₂ᵗ ← β₂ᵗ · β₂
for each i in 1, 2:
m[i] ← β₁ · m[i] + (1 − β₁) · g[i]
v[i] ← β₂ · v[i] + (1 − β₂) · (g[i] · g[i])
m̂ ← m[i] / (1 − β₁ᵗ)
v̂ ← v[i] / (1 − β₂ᵗ)
x[i] ← x[i] − η · 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
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].
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
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
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
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
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
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
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
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
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
constants: gtol = 1e−3, h = 1e−5
function IS-MINIMUM(x, y, gtol):
gx, gy ← ∇f(x, y)
if sqrt(gx · gx + gy · gy) is not less than gtol:
return false
a ← (g_x(x, y + h) − g_x(x, y − h)) / (2h)
b ← (g_y(x + h, y) − g_y(x − h, y)) / (2h)
hxx ← (g_x(x + h, y) − g_x(x − h, y)) / (2h)
hyy ← (g_y(x, y + h) − g_y(x, y − h)) / (2h)
hxy ← 0.5 · (a + b)
return hxx · hyy − hxy · hxy > 0 and hxx + hyy > 0
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
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
constants: GTOL = 1e−3, BLOW = 1e6
function RACE-STEP(race, σ):
t ← t + 1
ξ₁ ← NORMAL() # the x noise, one draw per step
ξ₂ ← NORMAL() # the y noise, drawn after ξ₁
for each track T in race:
if T has diverged:
skip T # a stopped track does not move again
otherwise:
q ← QUERY(T, x) # SGD: x; Nesterov: x + β·v
g ← ∇f(q) + σ · (ξ₁, ξ₂) # gradient with the shared noise
x' ← UPDATE(T, x, g) # the optimizer's own rule
if x' is not finite in either coordinate:
T diverges at step t, cause NaN
otherwise:
if |x'₁| > BLOW or |x'₂| > BLOW:
T diverges at step t, cause blow-up
otherwise:
loss ← f(x')
if loss is not finite:
T diverges at step t, cause NaN
otherwise:
move T to x', append x' and loss to its path
if T has not settled and IS-MINIMUM(x', GTOL):
record t as the step T settled
return race
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 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
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
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].
| Optimizer | Setting | Default | Why |
|---|---|---|---|
| SGD66 | \eta | 0.05 | On 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, \beta | 0.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, \beta | 0.02, 0.9 | The same as momentum, so the two can be compared directly. Only the point where the gradient is taken differs. |
| RMSProp66 | \eta, \rho | 0.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, \varepsilon | 0.1, 0.9, 0.999, 1e-8 | The 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, \varepsilon | 0.5, 1e-8 | Its accumulated squared gradients only grow, so the effective step falls over time. A large \eta makes the early steps useful. |
| Noise61 | \sigma, seed | 0, 7 | Off by default, so the race is deterministic. When it is on, every optimizer sees the same noise draw at each step. |
| Tolerance46 | gradient norm | 1e-3 | A track counts as settled once the gradient norm is below this and the Hessian is positive definite. |
| Divergence47 | position | 1e6 | A track is stopped once it leaves the box [-10^6, 10^6]^2 or its position or loss is not finite. |
| Hessian step67 | h | 1e-5 | The step of the central differences that estimate the Hessian for the settling test. |
| Surface and start | Optimizer | Steps to settle | Outcome after 500 steps |
|---|---|---|---|
| Bowl, (2.4, 1.6) | SGD / Momentum / Nesterov | 83 / 119 / 74 | All settled. |
| Bowl, (2.4, 1.6) | RMSProp / Adam / AdaGrad | 77 / 146 / 69 | All settled. RMSProp then hovers at a loss of about 1e-3. |
| Ravine, (3.2, 0.9) | Nesterov | 86 | Fastest on the ravine at these settings. |
| Ravine, (3.2, 0.9) | Momentum / SGD / Adam | 150 / 158 / 155 | Momentum edges out SGD. Adam is close to SGD here, not clearly faster. |
| Ravine, (3.2, 0.9) | AdaGrad | 108 | Settles, between Nesterov and the three slower tracks. |
| Ravine, (3.2, 0.9) | RMSProp | never | Hovers near (−0.025, 0.025) and does not reach the tolerance. |
| Rosenbrock, (−1.5, 2.0) | SGD / Momentum / Nesterov | diverge at steps 3 / 4 / 3 | Each one overshoots the banana's steep walls at its default step size. |
| Rosenbrock, (−1.5, 2.0) | RMSProp / Adam / AdaGrad | not within 500 | None 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.