GRADIENT DESCENT PLAYGROUND
Explore 2D & 3D visuals, tune learning rate, and build intuition interactively.
What is Gradient Descent?
Gradient descent is a fundamental algorithm to find the minimum of a function.
Imagine you're lost in a foggy valley: you can't see the landscape, but you can feel the slope beneath your feet. To reach the lowest point, you just keep stepping in the direction where the ground drops the fastest.
We want to minimize a function f(x), with x∈Rd.
- xn: current position
- ∇f(xn): the gradient — the vector of partial derivatives∇f=(∂x1∂f,∂x2∂f,…,∂xd∂f)
- η: the learning rate controlling step size
- 0<η<0.5: smooth convergence, xn keeps its sign;
- η=0.5: the minimum is reached in a single step;
- 0.5<η<1: zig-zags around 0 but still converges;
- η≥1: never converges (it bounces at η=1, diverges beyond).
How does it work, practically?
At each step, compute the gradient at your position; it points toward the fastest increase. To minimize, move in the opposite direction. Repeat until you’re “close enough” to a minimum (where the gradient vanishes).
- ∥∇f(xn)∥≤εgrad
- max(1, ∣f(xn)∣)∣f(xn+1)−f(xn)∣≤εrel
- n≥nmax (max iterations)
- Convex or well-conditioned landscapes
- L–smooth gradients
- Proper step size η
- Batch / mini-batch / SGD
- Momentum / Nesterov
- Adaptive: AdaGrad, RMSProp, Adam
Gradient Descent — 2D Views
We start with functions of a single variable f: R→R. Use the selector and the learning rate η to see how gradient descent behaves on different 1D landscapes.
We use three classic 1D landscapes:
- Quadratic — f(x)=x2🏔️ A simple bowl shape - there's only one bottom point. Easy for gradient descent to find!
- Double well — f(x)=x4−x2🏔️⚡🏔️ Two valleys with a hill between. Where you start determines which valley you end up in!
- Wavy quadratic — f(x)=x2+0.2sin(5x)🌊 A bowl with ripples. Some areas are smooth, others are bumpy - learning rate needs to be careful!
Tip: try different starts and compare f(xn). Watch the color: blue segments decrease, red indicates an increase (overshoot).
For the quadratic, the closed-form map xn+1=(1−2η)xn explains the regimes above. For the non-convex models, the widget displays an estimated L and the no-increase threshold2/L to interpret overshoot.
Next we’ll look at higher dimensions where geometry (conditioning, saddles, narrow valleys) matters even more.
Gradient Descent — 3D Views
We now optimize functions of two variables f: R2→R. Each function f(x,y) is rendered as a 3D surface z=f(x,y), so you can see the landscape while gradient descent moves on it. Adjust the start point and the learning rate η, and observe whether the path heads to a minimum—or gets stuck at saddles or in poor basins.
We use three classic landscapes:
- Quadratic bowl — f(x,y)=x2+y2🍜 A perfect bowl shape. No matter where you start, there's only one way down to the center!
- Himmelblau's function — f(x,y)=(x2+y−11)2+(x+y2−7)2🏔️🏔️🏔️🏔️ Four different valleys! Which one you reach depends on your starting point.
- Rastrigin (A=10) — f(x,y)=20+[x2−10cos(2πx)]+[y2−10cos(2πy)]🧀 Like Swiss cheese - full of holes! Gradient descent often gets trapped in the wrong hole.
Two step rules. Fixed step is plain gradient descent, exactly the formula above: every step is η∇f. Line search (backtracking, Armijo) starts from η and halves the step until f decreases enough: the usual fix when no single η suits the whole landscape.
Tip: try several Random start positions per function and compare final f(x,y). Watch for saddles (gradient ≈ 0 but not a minimum)—descent can stall there.
Plain gradient descent: every step is exactly η·∇f. Too large an η overshoots (red segments). Click on the surface to pick a start point.
Gradient Descent in Neural Networks
A neural network adjusts its weights to better predict the right answer. Gradient descent simply means: measure how each weight changes the error, then nudge it a little in the right direction to reduce that error.
🧠 The neural network combines the inputs with its current weights to make a prediction. Like a recipe: mix ingredients (inputs) in certain proportions (weights) to get a result.
📏 Compare the prediction with the correct answer. If the network predicted 0.8 but the answer was 0.2, that's a big error!
🧭 Figure out which weights caused the error. The gradient is like a compass pointing toward "make the error bigger" - so we'll go in the opposite direction.
⚡ Nudge each weight slightly in the direction that reduces error. The learning rate controls how big these nudges are. Repeat thousands of times → the network gets smarter!
The smallest possible network: a single neuron with two inputs (logistic regression), learning to separate two clouds of points.
- Boundary BEFORE/AFTER: the dashed line (before) moves to the solid line (after) at each update (300 ms animation).
- Update arrow: inside the neuron, a green arrow shows −η∇(direction & step size).
- Numbers panel: live values of y^, y, e, dW, db, Δw for the current sample.
- Current sample: the highlighted point is the one used for this step (one point at a time: stochastic gradient descent). It turns green while its update is applied.
- Loss curve: the pale line is the loss on the current sample (noisy, since each step sees a single point); the bold line is the average loss over all points, the quantity gradient descent actually lowers.
Play with η, step manually (Forward → Backprop → Update) or use Auto-play to watch the boundary move.