Optimizers from momentum to Adam
This lecture compares paths down the error surface from gradient descent to Adam. It covers momentum, Nesterov lookahead, batch and minibatch updates, Adagrad, RMSprop and second-order methods. After this page you can run one update step of each optimizer by hand.
Paths down the error surfaceSearch on YouTube
Optimization moves the parameter vector from high loss to low loss. The slides use Zoey at height A. Zoey must reach point B below. Four routes exist: inclined slide, spiral slide, direct jump and staircase. Each route gives a different arrival time and cost. Training behaves the same way. Different update rules trace different paths to the minimum.
Think of Zoey on a hill. A steep slide is fast but rough. A spiral slide is longer but smoother. Small stairs are safe but slow.
The route decides convergence speed and stability. A good optimizer reaches low loss in fewer updates. It also avoids wild oscillations and long stalls.
is the parameter vector at iteration . is the learning rate. is the gradient of the loss at iteration . The step size scales with the slope. Steep regions give large steps. Gentle regions give small steps.
Gradient sign and sizeSearch on YouTube
The gradient sign shows the direction of rise. The gradient size shows the rate of change. A positive slope means the function rises as rises. A negative slope means the function falls as rises. To descend, move opposite to the gradient.
Worked example. Check with at , and .
Function values.
Slope sign check.
Descent sizes. Move from 2 to 1, then from 1 to 0.
One gradient descent step. Start at with .
Loss falls from 5 to 3.56 in one step.
Error surface and start positionSearch on YouTube
A 2D error surface needs a top view. Take a horizontal slice of the bowl. View the contours from above. The slide marks two paths from level J1 to level J2. Path BD crosses contours in a short distance. Path AC runs a longer way across the same levels. BD is steeper than AC. The steeper direction gives faster descent.
Gentle slopes cause slow progress with plain gradient descent. The start position then matters a lot. Start S1 may sit on a steep face and converge fast. Start S2 may sit on a flat plain and crawl. The same rule with the same learning rate gives very different times.
MomentumSearch on YouTube
Momentum accelerates gradient descent with memory of past steps. It adds a velocity term that accumulates past gradients. It gains speed when gradients point in one direction. It damps oscillations when gradients flip sign. It helps on steep valleys, small local minima and noisy gradients.
is the velocity at iteration . is the momentum coefficient, often 0.9. is the learning rate. is the current gradient. is the parameter vector.
Overshoot and valley oscillationsSearch on YouTube
Momentum can run past the goal. The stored velocity pushes the parameters forward. The profile view on the slides shows this overshoot risk. The top view shows oscillations across a narrow valley. Plain descent creeps down the valley floor. Momentum swings from side to side while moving forward. It still reaches the minimum faster despite larger swings.
The slides fit a sigmoid with weight and bias from start . Each update favours the downhill direction of the loss. The point shifts toward the valley floor. After several swings it settles at the best fitting pair.
Do not set near 1 without care. The stored velocity grows large. The updates then overshoot the minimum and oscillate hard.
Examiners accept as the standard momentum value. State the velocity rule first. Then state that consistent directions speed up and flips cancel out.
Nesterov accelerated gradientSearch on YouTube
Nesterov accelerated gradient (NAG) improves momentum with a lookahead. It does not use the gradient at the current point. It first jumps along the stored velocity. It then measures the gradient at that lookahead point. This peek corrects the course before a bad leap. It cuts overshoot and oscillation.
is the lookahead point. is the gradient at the lookahead point. is the corrected velocity. is the new parameter vector. The four steps are lookahead, gradient, velocity correction and parameter update.
Look, Grade, Velocity, Drop: L-G-V-D
Lookahead point · Gradient at lookahead · Velocity correction · Drop to .
| Momentum | NAG |
|---|---|
| Uses the gradient at the current point | Uses the gradient at the lookahead point |
| Takes a big leap, then sees the slope | Peeks ahead, then adjusts the leap |
| Faster than plain descent but can overshoot | Less overshoot and less oscillation |
| One gradient per step at | One gradient per step at |
Worked example. NAG step on with . Set , , and .
Step 1. Lookahead point.
Step 2. Gradient at lookahead.
The gradient at the current point would be . The lookahead sees the gentler slope ahead.
Step 3. Corrected velocity.
Step 4. Parameter update.
Plain momentum would use gradient 2.0 and give and . NAG takes the shorter corrected step.
Batch, stochastic and minibatch updatesSearch on YouTube
The three methods differ only in data per update. The per-epoch update count decides speed and noise.
Batch gradient descentSearch on YouTube
Batch gradient descent uses all training points for one update. It averages the gradients. It then applies the mean gradient once. One epoch gives one update. The path is smooth and direct. The cost per update grows with . Large datasets make each step costly.
Stochastic gradient descentSearch on YouTube
Stochastic gradient descent (SGD) updates after each single point. One epoch gives updates. Learning starts at once without waiting for the full pass. Early progress is fast. The path is noisy and less stable. The noise helps escape small local minima and saddle points. SGD suits large datasets with frequent updates.
Minibatch gradient descentSearch on YouTube
Minibatch gradient descent sits between the two. Fix a minibatch size . Update after each block of points. One epoch gives updates. It keeps the stability of batch updates. It keeps much of the speed of SGD.
is the training set size. is the minibatch size. One epoch means one full pass over the data. gives SGD. gives batch descent.
| Algorithm | Updates per epoch | Character |
|---|---|---|
| Batch gradient descent | 1 | Smooth, costly per step |
| Stochastic gradient descent | Fast start, noisy path | |
| Minibatch gradient descent | Stable and fast, the common choice |
Worked example. Count updates for points with minibatch size .
Over 5 epochs.
A second case: with gives minibatch updates per epoch.
Write for minibatch updates per epoch in the exam. Add the two ends: gives SGD updates, gives 1 batch update.
AdagradSearch on YouTube
Different weights need different step sizes. Some weights see large gradients often. They need small steps to avoid overshoot. Other weights see tiny or rare gradients. They need large steps to move at all. One global learning rate cannot serve both groups. Uneven feature scales make the gap worse.
Adaptive gradient (Adagrad) gives each weight its own rate. It tracks the sum of squared gradients per weight. Weights with large past gradients get smaller rates. Weights with small past gradients keep larger rates.
is the accumulated sum of squared gradients. is the current gradient. is the base learning rate. is a small constant that avoids division by zero. All operations act per weight. The fraction is the effective learning rate for that weight.
Worked example. Two weights share . Weight sees large gradients. Weight sees small gradients. Start with for both. Use for hand calculation.
Iteration 1. Gradients are for and for .
So gets the smaller effective rate and gets the larger one.
Iteration 2. Same gradients again.
Both rates shrink by . The denominator only grows. Learning stalls with time.
Do not expect Adagrad to train to the end. The sum grows without bound. The effective rate falls toward zero and updates freeze.
RMSpropSearch on YouTube
Root mean square propagation (RMSprop) fixes the decay of Adagrad. It replaces the full sum with a moving average. Recent squared gradients matter more. Old ones fade away. The denominator then stays bounded.
is the moving average of squared gradients. is the decay rate, often 0.9. is the current gradient. is the base learning rate. avoids division by zero. Each weight keeps its own .
A short check shows the effect. With , each new squared gradient adds only 10 percent weight. A long run of large gradients still leaves room for new steps. The method keeps adaptive rates without the aggressive freeze.
AdamSearch on YouTube
Adaptive moment estimation (Adam) joins both ideas. Momentum tracks the mean gradient. RMSprop tracks the mean squared gradient. Adam keeps both moving averages. It corrects their early bias. It then applies an adaptive momentum step.
is the first moment, the momentum average. is the second moment, the adaptive average. and are the decay rates. is the current gradient. Both averages start at zero.
and are the bias-corrected moments. is the iteration index starting at 1. is the learning rate. is a small constant. The correction matters most in early steps when the averages still lean toward zero.
The four Adam steps are first moment, second moment, bias correction and parameter update. A variant called Nadam swaps plain momentum for Nesterov lookahead momentum. It applies the lookahead idea inside the Adam first moment.
G-M-A-R-D-A-N: Good Momentum And Rates Defeat All Noise
Gradient descent base · Momentum velocity · Nesterov Ahead lookahead. SGD and minibatch Rate is . Divided rates mark Adagrad · Averaged squares mark RMSprop · Adam and Nadam join both.
Adam in four: M-V-C-U. Mean gradient · Variance term · Corrected , · Update .
Worked example. One Adam step with , , , and .
Moments at from zero start.
Bias correction at .
Parameter change.
Without correction the raw ratio would give times . The correction restores the true gradient scale of 0.6.
Second-order methodsSearch on YouTube
First-order methods use only the gradient . This group holds plain descent, SGD, RMSprop and Adam. The gradient gives the slope. It does not give the curvature. Second-order methods also use the Hessian . The Hessian shows how the gradient itself changes. Curvature guides a more exact step.
is the Hessian matrix of second derivatives. is its inverse. is the gradient. This is Newton's method. It follows curvature in each direction.
The rule is simple. For a sharply curved direction, move a short way. For a flat direction, move a farther way. Convergence needs fewer iterations in theory. It also routes around tricky saddle zones better. Two limits block its use in deep nets. The Hessian has squared size in the parameter count. It is huge for large networks. Its inverse costs even more to compute and store.
Saddle pointsSearch on YouTube
A saddle point has zero gradient without being a minimum or maximum. It slopes up in some directions. It slopes down in others. Gradient methods rely on slope to move. At zero slope they see no direction. They may halt and report a false minimum. Training then stalls or slows hard.
Deep loss surfaces have high dimension. Saddle points there outnumber true local minima by far. Methods with memory or curvature escape more often. Momentum rolls through on stored velocity. Adam adapts each rate and keeps motion. Second-order methods read curvature and step away.
Race four optimizers down one loss surface. Change steps, surface and paths to test each rule.
Valley surface, start (w,b)=(2,6)
| — GD |
| — Momentum |
| — NAG |
| — Adam |
NAG looks ahead to 0.82. It sees slope 1.64, not 2.0. It steps to 0.656. Momentum steps to 0.62.
Adagrad gives 0.05 to w1 and 0.20 to w2. RMSprop holds both rates alive.
| Adagrad w1 0.05 | |
| Adagrad w2 0.20 | |
| RMSprop w1 0.05 | |
| RMSprop w2 0.20 |
Step 28. GD loss 0.00. Momentum loss 4.57. NAG loss 0.00. Adam loss 2.90.
Valley view: GD creeps down the floor. Momentum swings but runs fast. NAG cuts the swing. Adam steadies each direction. Adagrad iter 2 falls to 0.0354 and 0.1414. Both shrink by 1/√2.
Quick self-testSearch on YouTube
1. State the Zoey analogy and the plain gradient descent rule.
Zoey at height A must reach B below by slide, spiral, jump or stairs. Each path gives a different time and cost. Training matches this: . Steep slopes give large steps and gentle slopes give small steps.
2. For , find the slope sign at and . Which change drops more, or ?
. At the slope is 4, positive and steep. At the slope is , negative. The drop is . The drop is . The steep part drops more.
3. Differentiate momentum and NAG in one line each. Write the NAG four steps.
Momentum uses the gradient at the current point. NAG uses the gradient at the lookahead point. The four steps are lookahead , gradient , velocity and update .
4. Run one NAG step: , , , , .
Lookahead is . Gradient is . Velocity is . New .
5. Differentiate batch, SGD and minibatch by updates per epoch for and .
Batch gives 1 update. SGD gives updates. Minibatch gives updates. Over 5 epochs the totals are 5, 50000 and 500.
6. In Adagrad with , and , which weight gets the larger effective rate?
Effective rate is . Weight gets . Weight gets . The weight with small past gradients gets the larger rate.
7. Why does Adagrad stall, and how does RMSprop fix it?
Adagrad sums all squared gradients in . The denominator grows without bound and the rate falls toward zero. RMSprop uses a moving average with near 0.9. Old gradients fade, so the rate stays alive.
8. Write the four Adam steps and the bias correction at for .
Steps are first moment , second moment , bias correction and update. Here and . Correction gives and . The step is .
9. Differentiate first-order and second-order methods. State two limits of Newton updates.
First-order methods use only the gradient . Second-order methods add the Hessian and use . The Hessian is huge for large nets. Its inverse is costly to compute and store.
10. What is a saddle point, and which methods escape it more often?
A saddle point has zero gradient but is neither minimum nor maximum. Plain descent stalls there. Momentum, Adam and second-order methods escape more often through stored velocity, adaptive rates or curvature data.
- Plain gradient descent uses . Steep slopes step far. Gentle slopes step short.
- For , . Slopes are 2 at , 4 at and at .
- Momentum adds velocity . It speeds consistent directions and damps flips. Standard .
- NAG runs four steps: lookahead, gradient at lookahead, velocity correction and update. It cuts overshoot.
- Batch gives 1 update per epoch. SGD gives . Minibatch gives . is SGD and is batch.
- Adagrad scales each weight by . grows forever, so the rate decays to zero.
- RMSprop uses with near 0.9. It keeps adaptive rates alive.
- Adam keeps and , corrects bias with terms and updates with . Nadam uses Nesterov momentum inside Adam.
- Newton uses . It moves short on sharp curves and far on flat ones. Hessian size and inversion block large nets.
- Saddle points have zero gradient but are not minima. They dominate deep loss surfaces. Memory and curvature methods escape them.
SGD Clearly Explained
Adam Optimization Algorithm
Adam is Momentum+RMSProp