Lesson 7.4: Optimizing the Brain: SGD, Momentum, and Adam
We know from Lesson 2.2 that 'training' a model is just 'finding the bottom of a valley' using Gradient Descent. This lesson explores the advanced, modern optimization algorithms that allow our massive neural networks to navigate these complex valleys efficiently. We'll discover why vanilla Gradient Descent is too slow and how concepts like Momentum and Adaptive Learning Rates led to the development of Adam, the de facto standard optimizer for deep learning.
Part 1: The Problem with Vanilla Gradient Descent
Our basic Gradient Descent algorithm from Module 2 has a major flaw for deep learning:
The gradient, , is calculated based on the **entire training dataset**. For a neural network with millions of data points (e.g., ImageNet), calculating the gradient requires a full pass through all the data. This makes each step incredibly slow and computationally expensive.
The 'Slow but Steady' Problem
Vanilla ("Batch") Gradient Descent is like a cautious hiker who stops and surveys the entire landscape before taking a single step. It's guaranteed to walk in the right direction, but it's agonizingly slow.
Part 2: The Solutions - Faster & Smarter Steps
The Idea: Instead of calculating the true gradient on all 1 million data points, what if we just picked **one random data point** and calculated the gradient for that single point? This gradient will be very "noisy" and point in a slightly wrong direction, but it's incredibly fast to compute.
SGD takes thousands of these small, noisy, drunken steps. While each step might be slightly wrong, on average, they move in the right direction and stumble towards the minimum much, much faster than one slow, perfect step of Batch Gradient Descent.
The compromise: Mini-batch SGD. In practice, we use a "mini-batch" (e.g., 32 or 64 samples) to get a more stable estimate of the gradient at each step. This is the standard in deep learning.
The Problem with SGD: In long, narrow "ravines" in the loss landscape, SGD tends to oscillate back and forth, making slow progress down the valley floor.
The Idea: We give our optimizer "momentum," like a heavy ball rolling downhill. The update at each step is not just based on the current gradient, but is an exponentially weighted average of past gradients.
Momentum Update Rule
is the momentum term (e.g., 0.9). is the "velocity" vector.
The Effect: This averaging process smooths out the updates. Oscillations in irrelevant directions cancel each other out, while movement in the consistent downhill direction accumulates, allowing the "boulder" to accelerate down the valley floor much faster.
The Problem with Momentum: We still have to manually choose a single, global learning rate . What if one parameter needs small, careful steps, while another could safely take giant leaps?
The Idea: The **Adaptive Moment Estimation (Adam)** optimizer solves this by maintaining not only a "momentum" (an average of past gradients, the 1st moment) but also a separate "adaptive learning rate" for **each individual parameter**, based on the average of its past squared gradients (the 2nd moment).
The Intuition:
- If a parameter's gradient has been consistently large, its "2nd moment" estimate will be large. Adam will give it a *smaller* learning rate to be more cautious.
- If a parameter's gradient has been consistently small and sparse, its "2nd moment" estimate will be small. Adam will give it a *larger* learning rate to encourage it to move faster.
Adam combines the best of both worlds: the speed of Momentum and an automatically adapting, per-parameter learning rate. It is the default, go-to optimizer for the vast majority of deep learning problems today.
- Always use Mini-batch SGD. Never use full Batch Gradient Descent for deep learning.
- Adam is the default choice. For most problems, Adam provides the best combination of speed and performance. Its default parameters in libraries like Keras and PyTorch are usually a great starting point.
- The **learning rate ()** is the single most important hyperparameter to tune. Using a "learning rate schedule" (where you start with a larger and gradually decrease it during training) is a common and effective technique.
What's Next? Taming the Beast
We now have a powerful brain (the MLP) and a powerful engine to train it (Adam). This combination is so powerful that it's dangerous. A deep neural network with millions of parameters can easily "learn too well" and simply memorize the training data, a problem we call **overfitting**.
In the next lesson, we will explore the essential **regularization techniques**, like Dropout and Early Stopping, that we use to tame this powerful beast and ensure it learns general patterns, not just noise.