Hello! Welcome to the next lesson in our journey through optimization.
In our last session, we focused on the learning rate, , and learned how to diagnose and fix issues when it's too high or too low. We saw that even with a well-chosen learning rate, the path our optimizer takes can be inefficient, especially when using Mini-Batch Gradient Descent. The gradients calculated from small batches of data can be noisy, causing the optimization path to zigzag towards the minimum. This slows down training.
Today, we'll address this "zigzag" problem. Our learning outcome is to explain how momentum can help accelerate and stabilize gradient descent. We'll explore how by remembering the direction of our previous steps, we can build "momentum" to move faster and more smoothly through the loss landscape, much like a ball rolling down a hill.
The Problem: Zigzagging Down the Ravine
Imagine the loss function isn't a simple, symmetrical bowl, but a long, narrow valley or ravine. This is very common in neural network loss landscapes.
Standard gradient descent calculates the steepest direction at its current position. On the steep walls of the ravine, this direction points almost directly across the valley, not down the valley floor toward the minimum. As a result, the optimizer bounces from one side of the ravine to the other, making slow progress toward the actual solution. These oscillations force us to use a smaller learning rate to prevent divergence, which slows down convergence even more.
To visualize this issue, let's watch a segment from a video by Andrew Ng.
Gradient Descent With Momentum (C2W2L06)
This clip clearly illustrates the problem of oscillations when optimizing a function with an elongated, ellipse-like shape.
Watch from 0:24 to 1:57. Pay close attention to the contour plot and how the standard gradient descent steps oscillate back and forth, slowing down the process.
The key issue is that gradient descent is amnesiac—it only considers the gradient at the current step, with no memory of its past direction. Momentum is the technique we use to give our optimizer this memory.
The Intuition Behind Momentum
The core idea of momentum is to simulate physical momentum. Instead of just following the current gradient, we add a fraction of our previous update vector to the current one.
Think of it like a ball rolling down a hill:
- It doesn't just move based on the slope at its exact current location; it carries velocity from its past movement.
- As it rolls down a consistent slope, it builds up speed.
- This accumulated speed helps it power over small bumps (noise in the gradient) or escape shallow valleys (local minima).

This simple idea has a powerful effect on the zigzag problem:
- In the oscillating direction (across the ravine): The gradients are constantly switching signs (e.g., +0.5, -0.5, +0.5...). When we average them over time, they tend to cancel each other out, damping the oscillations.
- In the consistent direction (down the ravine): The gradients are always pointing in the same direction. When we average them, they reinforce each other, building up speed and accelerating convergence.
To dive deeper into this intuition, the following article offers some excellent analogies.
Visualizing Loss Landscapes & Escaping Local Minima
The article 'Visualizing Loss Landscapes & Escaping Local Minima' provides two great analogies for understanding momentum. It also sets up the problems that momentum is designed to solve.
Please read the sections titled 'The Three Hurdles in Non-Convex Optimization', 'Why Use Momentum Optimization?', and 'What is Momentum Optimization?'. Focus on the analogies of the 'Confident Driver' and the 'Ball on a Hill' to solidify your intuitive grasp of the concept.
The Mathematics of Momentum
The "memory" in momentum is implemented using an Exponentially Weighted Moving Average (EWMA) of the gradients. Instead of updating the weights directly with the current gradient, we first calculate a "velocity" vector, , which is a smoothed average of the gradients, and then use this velocity to update the weights.
Here's how it works at each iteration :
- Compute the gradients on the current mini-batch (we'll denote this as ).
- Update the velocity using the EWMA formula:
- is the new velocity.
- is the velocity from the previous step.
- is the momentum hyperparameter.
- Update the parameters using the new velocity:
Notice the key difference: we subtract , not . We are moving in the direction of the smoothed average gradient.
Let's watch Andrew Ng explain this formulation.
Gradient Descent With Momentum (C2W2L06)
This segment details the mathematical implementation of momentum, connecting it directly to the concept of exponentially weighted averages.
Watch from 1:57 to 6:12. Focus on how the velocity update rule is structured and how it leads to the damped oscillations shown in the visualization. The physics analogy of acceleration and friction is also very helpful here.
The Momentum Hyperparameter:
The hyperparameter controls the "friction" in our system. It determines how much of the past velocity is carried over to the current step.
- If , then , and we recover standard gradient descent. There is no memory.
- If is close to 1, we consider a long history of past gradients.
- A common and very effective value for is 0.9.
With , the update is roughly averaging over the last gradients. This is usually enough to smooth out noise without being overly influenced by outdated information.
Test your understanding!
Consider two scenarios for a loss landscape:
- A perfectly straight and steep slope.
- A long, narrow, and winding canyon (ravine).
How would you expect the performance of Gradient Descent with Momentum () to compare to standard Gradient Descent in each scenario?
Show answer
- Straight, steep slope: Both methods would work well. However, momentum would likely be faster because as it moves down the consistent slope, it will "build up speed" and take larger and larger effective steps, accelerating towards the minimum.
- Winding canyon: Momentum would be significantly better. Standard GD would waste time zigzagging from wall to wall. Momentum would average out these oscillations, effectively cancelling the side-to-side movement while accumulating speed along the canyon floor, leading to much faster and more stable convergence.
The Benefits and a Key Trade-Off
The two main benefits of momentum are what we set out to achieve:
- Acceleration: By accumulating gradients in a consistent direction, momentum helps the optimizer move faster toward the minimum.
- Stabilization: By averaging out noisy, oscillating gradients, it smooths the optimization path and makes training more stable.
A powerful side-effect of this is the ability to navigate tricky parts of the loss landscape. The accumulated velocity can help the optimizer "roll through" flat plateaus (saddle points) where the gradient is near zero, or even "power over" shallow local minima.

However, momentum's greatest strength—its speed—can also be a weakness.
The Problem of Overshooting:
Because the optimizer has so much momentum, it may not stop precisely at the bottom of the hill. Instead, it can overshoot the minimum, have to reverse, and then oscillate back and forth a few times before it finally settles. While momentum gets you to the vicinity of the minimum much faster, this final settling process can sometimes be tricky.
Conclusion
You now understand one of the most fundamental and effective improvements to the gradient descent algorithm. By adding a "memory" of past updates, momentum transforms the optimization process from a jittery walk into a smooth, accelerating roll toward the minimum.
Key Takeaways:
- Problem: Standard gradient descent is "memoryless" and can get stuck in slow, oscillating paths in the narrow "ravines" of the loss landscape.
- Solution: Momentum uses an exponentially weighted moving average of past gradients to create a "velocity" vector. This update rule smooths out oscillations and accelerates movement in consistent directions.
- Benefits: This leads to faster convergence and more stable training. It also helps the optimizer escape shallow local minima and saddle points.
- Hyperparameter: The momentum term, , is typically set to 0.9, which corresponds to averaging over the last 10 gradients.
- Trade-off: The accumulated speed can cause the optimizer to overshoot the minimum before settling.
Preview of the Next Lesson:
Momentum is a huge step forward, but it still uses a single, fixed learning rate, , for all parameters in the model. But what if some parameters need large updates while others need very fine-grained changes? In our next lesson, we will explore adaptive learning rate methods like Adam, which calculate a unique learning rate for every single parameter, combining the ideas of momentum with other clever tricks.