Skip to content
GenAI Learn/Neural Networks From First Principles
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Optimizers: Beyond Vanilla Gradient Descent

7 min read

You'll learn to

  • -Understand the two specific weaknesses of vanilla gradient descent that better optimizers fix
  • -Write the momentum and RMSprop update rules and explain what each one tracks
  • -Understand Adam as the combination of both, and why it is the default optimizer almost everywhere

Every gradient descent example so far in this course has used the plain update rule from two chapters ago: subtract the learning rate times the gradient. In practice, almost no real network is trained with plain gradient descent. It has two specific weaknesses that a family of smarter optimizers were built to fix, and knowing them by name, especially the one you should reach for by default, is standard interview territory.

Weakness One: No Memory Between Steps

Plain gradient descent treats every step independently. It has no memory of which direction it was moving in previously. On a loss surface shaped like a narrow ravine, this causes visible zig-zagging. The gradient keeps pointing back and forth across the narrow direction while making frustratingly slow progress along the long, gentle direction toward the actual minimum.

Momentum: Remembering Where You Were Heading

Momentum fixes this by keeping a running velocity, an exponentially weighted average of recent gradients, and stepping in that direction instead of the raw current gradient alone. Like a heavy ball rolling downhill, it builds up speed in a consistent direction and resists sudden changes, smoothing out the zig-zag and accelerating through gentle, consistent slopes.

Momentum Update Rule
v ← βv + (1−β)∇L(x) , x ← x − α·v
v is the velocity, starting at 0. β, typically 0.9, controls how much of the previous velocity carries over, with values closer to 1 meaning longer memory. The parameter update uses v instead of the raw gradient ∇L(x).

Weakness Two: One Learning Rate for Every Parameter

Plain gradient descent also applies the exact same learning rate to every single parameter, even though different parameters can have wildly different-sized gradients. A parameter with a consistently large gradient can overshoot, while one with a consistently tiny gradient barely moves at all, both problems happening simultaneously, with no single learning rate that is right for both.

RMSprop: A Learning Rate That Adapts Per Parameter

RMSprop keeps a running average of each parameter's recent squared gradients, then divides that parameter's learning rate by the square root of that average. A parameter that has been seeing large gradients gets its effective learning rate shrunk. A parameter seeing small gradients gets a relative boost. Every parameter effectively gets its own, self-adjusting learning rate.

RMSprop Update Rule
s ← βs + (1−β)(∇L(x))² , x ← x − α·∇L(x)/(√s + ε)
s tracks a running average of squared gradients, always positive since it is squared. ε is a tiny constant, like 1e-8, purely to avoid dividing by zero when s is near 0.

Adam: Momentum and RMSprop, Combined

Adam, short for Adaptive Moment Estimation, tracks both quantities at once: a momentum-style running average of the gradient itself, and an RMSprop-style running average of the squared gradient, then combines them into a single update. This gives Adam both benefits simultaneously, a smoothed, momentum-driven direction and a per-parameter adaptive step size. It is, by a wide margin, the most commonly used optimizer in deep learning today, and "just use Adam" is a genuinely reasonable default answer in most practical situations.

Watch all three race on the same landscape below. The gap between them is not subtle.

Three Optimizers, One Landscape

Same starting point, same landscape. Watch what happens once vanilla GD hits the shallow dip.

Vanilla GD

x=5.50

loss=14.55

Momentum

x=5.50

loss=14.55

Adam

x=5.50

loss=14.55

step 0 / 30
Vanilla GD vs. momentum vs. Adam, on the same starting point

Run the code above and look closely. Vanilla GD settles near x=3.15 (loss around 4.08), while momentum and Adam both land near x=1.9 (loss around 2.7 to 2.9), a meaningfully deeper minimum. That is not a coincidence of these particular numbers. It is the exact real-world advantage this chapter has been building toward. On a bumpy, non-convex loss surface, plain gradient descent can settle into the first shallow dip it meets, while the accumulated velocity in momentum and Adam is often enough to carry the parameter through a shallow dip and into a genuinely better minimum beyond it.

Optimizer Cheat Sheet
Same learning rate for every parameter, no memory. Rarely used directly in practice
Vanilla GD
Smooths direction using a running average of gradients. Fixes zig-zagging
Momentum
Adapts the learning rate per parameter using squared gradients. Fixes one-size-fits-all step sizes
RMSprop
Momentum plus RMSprop together. The default choice almost everywhere
Adam

In practice, you would never hand-implement Adam. PyTorch's torch.optim.Adam(model.parameters(), lr=0.001) or TensorFlow's tf.keras.optimizers.Adam() gives you exactly the algorithm above in one line, with the gradients themselves computed automatically via autograd rather than hand-derived backpropagation. Knowing the mechanism is what an interview tests. Knowing the one-line API call is what actually building something requires. You need both.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

ScaleDojo Logo
Initializing ScaleDojo