Interactive optimization playground

Gradient descent visualization

Put SGD, Momentum, and Adam on the same loss surface. Drag their starting point, tune the learning rate, and animate how optimizer state changes the route to a minimum.

Free to useNo upload or sign-inRuns entirely in your browser
Live optimizer simulationPowered by @kanaries/ml
Iteration0 / 60
Momentum loss4.540
Adam loss4.540

Optimizer trajectories on the loss surface

parameter xparameter y
SGD: 4.5405Momentum: 4.5405Adam: 4.5405

What the animation computes

This is an explicit educational simulation of the published SGD, Momentum, and Adam update equations. @kanaries/ml supplies the browser-safe numerical sum used by each objective; the chart does not claim to expose private training trajectories from a library estimator.

Same starting point, different state

SGD uses only the current gradient. Momentum accumulates a velocity. Adam maintains bias-corrected first and second moments, adapting the step per coordinate. Their different state explains the diverging paths.

Implement the update loop in JavaScript or Python

import { KMath } from '@kanaries/ml';

const loss = ([x, y]) => KMath.sum([x * x, 0.45 * y * y]);
const gradient = ([x, y]) => [2 * x, 0.9 * y];

let point = [-1.8, 1.7];
const learningRate = 0.08;

for (let step = 0; step < 60; step += 1) {
  const [dx, dy] = gradient(point);
  point = [
    point[0] - learningRate * dx,
    point[1] - learningRate * dy,
  ];
}

What gradient descent does

Machine-learning training often means finding parameters that minimize a loss function. Gradient descent approaches that problem through local slope information. The gradient is a vector of partial derivatives pointing toward the steepest increase in loss. Subtracting a fraction of that vector moves parameters downhill. Repeating the update turns derivatives into a sequence of candidate solutions.

The idea is simple enough to write in a few lines, yet its behavior depends on the objective geometry, starting point, learning rate, gradient noise, and optimizer state. A round convex bowl is easy. A narrow curved valley can produce slow zigzags. A non-convex surface adds saddle points and local minima. This visualization makes those differences visible rather than hiding them inside a training call.

Compare SGD, Momentum, and Adam interactively

Choose a loss surface, drag the shared start marker, and press Play. The chart advances three update rules from identical initial coordinates. Plain SGD follows the current gradient. Momentum combines the gradient with a decaying velocity. Adam estimates both the first moment of the gradient and its uncentered second moment, then uses those estimates to adapt the update in each coordinate.

The simulation runs entirely in JavaScript. Its objectives use KMath.sum from @kanaries/ml, while the optimizer equations are intentionally written in the component so learners can inspect them. This distinction matters: the lines are a transparent educational simulation, not undocumented internal traces extracted from a model.

Reading the optimizer trajectories

On the convex bowl, SGD usually takes a direct path but may move faster along the steep axis than the shallow one. Momentum builds speed where gradients agree and can cross the minimum before damping out. Adam normalizes updates using recent squared gradients, so its path may look more balanced across differently scaled directions.

The Rosenbrock objective contains a long, curved valley. Reaching the valley is not the same as following it to the minimum, which exposes oscillation and coordinate imbalance. The rippled surface adds several local basins. No first-order optimizer can promise the global minimum on every non-convex objective; initialization changes which basin the path encounters.

Learning rate is part of the algorithm

The learning rate multiplies every update. Raise it and optimization covers more distance per iteration, but overly aggressive steps overshoot or bounce between sides of a valley. Lower it and the path becomes stable but may make negligible progress within the iteration budget. Try several rates on each surface and compare loss values rather than judging motion alone.

Real training commonly changes the rate over time. Decay schedules reduce it as training approaches a solution, warmup starts cautiously, and adaptive methods scale coordinates based on gradient history. Batch size changes gradient noise as well. The clean surfaces here isolate update behavior before those production considerations are introduced.

From equations to machine-learning models

For a model with millions of parameters, the chart would be impossible to draw directly, but the update logic is the same. Automatic differentiation computes gradients, the optimizer updates tensors, and a validation metric checks whether lower training loss improves generalization. Regularization adds terms or constraints so the chosen parameters do not merely memorize training examples.

Gradient clipping in this playground caps very steep gradients so all paths remain visible. Clipping is also used in neural-network training, especially for exploding gradients, but it changes the effective update and should be monitored. Likewise, finite precision, stopping criteria, and reproducible random seeds matter when an experiment becomes production software.

JavaScript and Python implementation

The code tabs implement the same convex objective and SGD loop with @kanaries/ml in JavaScript and NumPy in Python. Momentum would add a velocity vector; Adam would add first- and second-moment vectors plus bias correction. Keeping that state explicit is a useful way to understand what an optimizer contributes beyond the raw gradient.

Continue with the logistic regression calculator to connect optimization to classification probabilities, inspect the JavaScript logistic regression API for a fitted estimator, or contrast iterative optimization with the projection geometry in the PCA visualization.

Diagnosing optimization in practice

Record training loss, validation loss, gradient norm, update norm, and learning rate together. A flat training loss with tiny updates can indicate a rate that is too low, saturated activations, poor feature scaling, or a coding error. Exploding loss and non-finite numbers point toward an excessive rate, unstable arithmetic, or gradients that need clipping. Falling training loss paired with worsening validation performance is an overfitting signal, not an optimizer victory.

Optimizer comparisons should use the same initialization, batches, preprocessing, stopping budget, and random seeds. One run is rarely enough when minibatch order or initialization is stochastic. Repeat experiments and report the distribution of the metric that matters to the application. Checkpoints should include optimizer state as well as model parameters; restoring weights without Momentum velocity or Adam moments changes the subsequent trajectory. Finally, remember that faster reduction of the training objective does not guarantee better generalization. The optimizer is one part of a system that includes the objective, data, regularization, schedule, architecture, and evaluation protocol.

Frequently asked questions

Questions about gradient descent visualization

What is gradient descent?

Gradient descent is an iterative optimization method. At each step it computes the gradient of an objective and moves parameters in the opposite direction, which locally decreases the loss when the learning rate is appropriate.

How is Momentum different from ordinary SGD?

Ordinary SGD follows the current gradient. Momentum also carries a velocity built from earlier gradients, which can accelerate progress along consistent directions and reduce back-and-forth oscillation across narrow valleys.

What does Adam add?

Adam tracks exponential moving averages of gradients and squared gradients. Bias correction and per-coordinate scaling let it adapt step sizes, often making it effective when gradients differ greatly by direction or training data is noisy.

What happens when the learning rate is too high?

Updates can overshoot a minimum, oscillate, or diverge. A very low rate is usually stable but slow. Schedules, warmup, normalization, and adaptive optimizers help, but the rate still needs validation.

Are these paths internal @kanaries/ml training logs?

No. The playground openly implements the textbook update equations for teaching and uses @kanaries/ml numerical utilities for the objective. It does not present simulated points as hidden estimator logs.

Powered by @kanaries/ml — the scikit-learn-style ML library for JavaScriptnpmGitHub