Lensa ML
Lensa ML

How Optimizers Work

Step 1 of 6

Vanilla SGD

Take a step proportional to the gradient

param 1param 2minstart
Learning rateα = 0.080
Steps
>40
Final loss
0.0710
α
0.080

Vanilla SGD updates each parameter by subtracting α · gradient. Notice the zigzag path — in the steep (param 2) direction it oscillates, while in the flat (param 1) direction progress is slow. Try increasing α to see the oscillation get worse.

Step 1 of 6: Vanilla SGD

Take a step proportional to the gradient

param 1param 2minstart
Learning rateα = 0.080
Steps
>40
Final loss
0.0710
α
0.080

Vanilla SGD updates each parameter by subtracting α · gradient. Notice the zigzag path — in the steep (param 2) direction it oscillates, while in the flat (param 1) direction progress is slow. Try increasing α to see the oscillation get worse.

Step 2 of 6: Momentum

Roll downhill like a ball, building speed

param 1param 2minstartSGDMomentum
Momentum (β)β = 0.75
SGD steps
>40
Mom steps
29
β
0.75

Momentum adds a velocity term: v = β·v + ∇L, then θ = θ − α·v. Like a ball rolling downhill, it builds speed in consistent directions and dampens oscillations. Compare the dashed SGD path (>40 steps) to the smoother momentum path (29 steps).

Step 3 of 6: RMSProp

Adapt the learning rate per parameter

param 1param 2minstartSGDRMSProp
Decay (γ)γ = 0.90
SGD steps
>40
RMS steps
30
γ
0.90

RMSProp tracks the running average of squared gradients per parameter, then divides by √(avg). Parameters with large gradients (steep axis) get smaller effective steps, while small-gradient parameters (flat axis) get boosted. Compare SGD (>40 steps) to RMSProp (30 steps).

Step 4 of 6: Adam — Best of Both

Momentum + adaptive rates = the default optimizer

param 1param 2minstartSGDAdam
Learning rate (α)α = 0.200
SGD steps
>40
Adam steps
25
α
0.200

Adam combines momentum (first moment) with RMSProp (second moment), plus bias correction for early steps. Compare SGD (>40 steps) to Adam (25 steps) — Adam is the default choice for most deep learning tasks.

Step 5 of 6: Learning Rate Schedules

Slow down as you approach the minimum

epochloss025507510000.51

Constant LR oscillates near the minimum — steps are too big at the end. Step decay drops the rate at fixed intervals. Cosine annealing smoothly reduces the rate, often giving the best final loss. Most practitioners pair Adam + cosine schedule.

Step 6 of 6: Choosing an Optimizer

When to use what — a practical guide

StartConvexproblem?yesSGD + Mom.noLargedataset?yesAdamnoRMSProp★ default choice
SGD + Momentum
+ Simple, generalizes well Needs LR tuning
RMSProp
+ Good for RNNs, non-stationary No bias correction
Adam
+ Fast convergence, little tuning May generalize worse

In practice, start with Adam (lr=1e-3) — it works well out of the box. If you need the best generalization (e.g. image classification), try SGD + Momentum with a cosine LR schedule. Use RMSProp for RNNs or reinforcement learning.