Lesson 2: Optimization (Gradient Descent & Adam)
Explore different algorithms used to minimize loss in deep learning models.
By Bishwambhar Sen • Last updated • 60 min
Calculated gradients show the direction of maximum error reduction, but how fast should we move in that direction? This is governed by optimization algorithms.
The Learning Rate
The Learning Rate (α) controls the step size taken towards the local minimum during training. If the learning rate is too high, the model might overshoot the minimum and fail to converge. If the learning rate is too low, training will be incredibly slow and can get stuck in poor local minima.
Stochastic Gradient Descent (SGD)
Standard gradient descent calculates gradients across the entire dataset before making a single update. Stochastic Gradient Descent (SGD) updates the weights after examining a single sample (or a small batch) of data. This adds helpful noise that can rescue optimization from local minima, though it makes the descent path erratic.
Adding Momentum
To smooth out SGD updates, we add Momentum. This keeps a moving average of past gradients, allowing optimization to roll quickly through flat regions and noisy oscillations, much like a ball rolling down a valley.
Adaptive Optimizers: Adam
Adam (Adaptive Moment Estimation) is the industry standard optimization method. Instead of using a fixed learning rate for all weights, Adam dynamically scales the learning rate for each individual weight based on its historical update frequency.
Loss Surface Descent Visualizer
Learning Rate Comparison
The learning rate is the single most important hyperparameter. These charts show the loss over 60 SGD steps with three different rates on the same L(w) = w² surface:
Coding Challenge: Gradient Descent Update
Let's write a standard gradient descent parameter update rule in Python!
- Create a variable
weightinitialized to5.0. - Create a variable
gradientinitialized to0.4. - Create a variable
learning_rateinitialized to0.1. - Update the
weightvariable by subtractinglearning_rate * gradient. - Print the new weight value. It should equal
4.96.
Run this script in the editor on the right to see if your manual parameter tuning works!