Supervised Learning
Gradient Descent
The gradient measures local change; descent subtracts the learning-rate-scaled gradient and measures the actual result.
Selected observation 1, x -4.500000, y -8.646135. 34 observations.
- Observation
- Selected observation
Edit an observation
Edited coordinates round to 0.01 units; other coordinates keep their exact seeded values. Plot bounds: x ±5, y ±15.
What changes when you move one observation?
Drag any observation, use its arrow keys or edit its coordinates. These are the same 34 observations used by every later calculation.
Observation
One measured input x and target y.
Check this stage
Check your understanding
A gradient measures local change. Subtract a scaled gradient, then measure the result: a finite step can lower the loss, overshoot or remain stationary.
Move a line, then move its parameters
Start by moving an observation. Drag it, focus its handle and use the arrow keys, or open Edit an observation and enter its coordinates. Each edit changes the same dataset used by the line, loss, gradient and OLS reference.
In A guess, change the starting slope and intercept. The line predicts . Its residual is , positive when an observation is above the line. One number compares the mean squared residual, MSE, with the mean absolute residual, MAE. A residual of magnitude 4 contributes 16 to the squared sum; four residuals of magnitude 1 contribute only 4 between them.
The loss curve holds the current intercept fixed while it varies the slope. The neighbouring contour map varies both parameters. Its circles and trails live in parameter space: a horizontal move changes the slope; a vertical move changes the intercept. They are not movements of an observation.
What does the gradient tell you?
For the regression objective,
The gradient is a vector of partial derivatives. It points locally uphill under the Euclidean norm; its negative gives a local steepest-descent direction. It doesn't tell the optimiser where the minimum is. This local meaning and its dependence on the chosen norm are formalised in Boyd and Vandenberghe's optimisation text.
In The gradient, predict how the slope will change. Switch Vectors between the direction aids and Actual update. Direction aids have a fixed illustrative length of 0.75 parameter units. The actual displacement is , whose length changes with both the gradient and the rate .
A local slope, not a destination
Feeling the slope beneath your feet can tell you which nearby direction descends. It cannot tell you where the whole valley ends, or whether a long stride lands on the opposite wall. The analogy describes local direction; the lab computes the finite endpoint instead of assuming it will be lower.
Check one update by hand
For observations and parameters , predictions are . Prediction errors are , giving
At rate , the displacement is , so the next parameters are . MSE falls from to approximately . The full update is
The small hand fixture illustrates the same calculation as the larger seeded dataset. Open Under the hood for the live gradient, displacement, endpoint, endpoint loss and incoming rate for every recorded iteration.
Step, inspect and branch
Press Step in the sixth stage. The parameter trail and loss history record that exact computation. Select Full update slice to inspect : is the current state, is the proposed endpoint. The diamond there corresponds to the diamond at the end of the parameter arrow. This differs from the retained fixed-intercept slice, which never carries a trajectory evaluated at changing intercepts.
Play is reader-started and bounded. Pause stops it. Replay starts again from the chosen starting parameters with the current settings. Seeking a recorded step pauses; the next update replaces its former future. A rate change pauses and preserves earlier measurements, while changing the start, objective, multiplier or observations starts a fresh history.
Run to convergence computes bounded chunks and offers Cancel. Its name is an intention, not a promise. The status distinguishes a known optimum within the stated tolerances, a small gradient without a global certificate, three material loss increases, a numerical bound and the 240-update limit. Cancellation retains completed updates. A tiny loss change alone is never called convergence.
Break the rate, then question the guarantee
Choose Make the valley diverge in Too far, then Step. This objective is . At and rate , the first endpoint is and loss rises from 21 to 180.64. The steep coordinate is multiplied by . The next steps amplify that alternating error. Back to working restores your complete preceding experiment and history, paused.
Try Contracting zig-zag instead. At rate , the steep-coordinate multiplier is : its sign alternates, but its magnitude contracts. Oscillation alone doesn't mean divergence.
For the convex quadratic objectives, each Hessian eigenmode has error multiplier . Positive-curvature modes contract for . Above , some can alternate. At the upper boundary the largest mode need not shrink; above it a present unstable mode grows. A start exactly at the optimum, or without a component in that mode, is an exception. Increasing the rate within the contracting interval needn't improve the convergence speed.
The regression boundaries come from the actual x coordinates. Moving y alone changes the fit and gradient, but cannot change this Hessian. Multiplying the objective by 10 multiplies gradient and curvature by 10; divide the rate by 10 to preserve the same steps. The slider zones state mode behaviour, not an invented goodness score.
A small gradient can be a saddle
Choose Start at a saddle. At the origin of , both partial derivatives are zero, so plain descent takes a zero step. Nearby b values lower the objective: this is not a minimum. Nudge the parameter handle and compare the new behaviour. The status reports a small gradient with saddle curvature, never convergence to a global optimum.
The two-minima polynomial has unequal wells. Different starts can settle near different local minima; no global certificate is supplied. Curved valley uses the standard Rosenbrock function and analytic derivatives, , with known minimum zero at . Its curvature changes with position. Neither it nor the two-minima objective receives the quadratic global rate guarantee. The distinction between stationary saddles and minima is also illustrated in Dive into Deep Learning.
What the display and links preserve
Contours approximate the analytic objective on a labelled 49×49 sample grid. Computed parameters are never projected into the plotting window or clipped to the slider range. Cropped arrows, boundary handles and omitted off-window trajectory segments are labelled; raw values stay in inspection. The loss plot uses a linear vertical scale that adapts to the recorded values.
Every gradient here is exact for the selected objective. Data noise belongs to the generated observations; it is not per-step stochastic gradient noise. The gradient-descent discussion in D2L provides further worked examples and explains why finite steps can overshoot.
Reset controls restores default controls and removes observation edits while retaining the stage and seed. New data changes actual generated observation noise and removes point edits. Shared links reproduce the stage, seed, edits, starting parameters and settings, opening paused at step zero. Recorded history, predictions and quiz answers stay in the session. Local progress only records exploration on this device.
Stage recap
The data
Drag any observation, use its arrow keys or edit its coordinates. These are the same 34 observations used by every later calculation.
A guess
Slope and intercept are parameters. Moving either changes every prediction; the dashed residuals join predictions to observations.
One number
MSE averages squared residuals. MAE averages their absolute values. Move a point and compare both measurements.
The loss curve
The curve holds the current intercept fixed. The contour map varies both slope and intercept. Equal-loss contours are sampled approximations to the same objective.
The gradient
The gradient points locally uphill. Its negative is a local steepest-descent direction under the Euclidean norm. The solid update arrow scales that direction by the rate; it does not point straight at the minimum.
Step
Step records one real update. Play is finite. Seek an earlier recorded step, then Step to create a new branch. Rate changes pause and retain past measurements.
Too far
Try other objectives and positive multipliers. Convex quadratics have mode-based rate boundaries. Saddles and curved nonquadratic objectives have no universal rate marker here.
Reference
- Prediction and residual
- ŷ = wx + b; residual = y − ŷ.
- MSE
- L = mean((wx + b − y)²); no half factor.
- Gradient
- g = (2 mean((ŷ−y)x), 2 mean(ŷ−y)); locally uphill in Euclidean coordinates.
- Actual update
- (w,b) ← (w,b) − ηg. Unit arrows show directions; the scaled arrow shows displacement.
- Quadratic modes
- Error in eigenmode λ is multiplied by 1−ηλ. Positive modes contract for 0<η<2/λmax; an absent unstable mode cannot grow.
- Nonquadratic objectives
- Rosenbrock and two-minima curvature varies with position. Local curvature is not a global stability guarantee.
- Stopping
- Known-optimum and gradient tolerances, small gradient alone, observed growth, numerical bound, cancellation and iteration budget are distinct.
Break it on purpose
On the narrow valley, start at (1, 1) with rate 0.1 and multiplier 1. The steep-coordinate error grows by a factor of −3 per step. Loss rises even though every update opposes its local gradient.
Explore next
- SGD, momentum and AdamAn optimiser combines the current gradient with its own memory to choose an actual displacement.
- Learning Rate SchedulingA schedule chooses the multiplier for each next gradient update; its effect is measured by the actual objective after a stated budget.
- Linear Regression (OLS)Least squares chooses the line that minimises squared vertical residuals for the observed points.