Lesson 2: Optimization (Gradient Descent & Adam)

Explore different algorithms used to minimize loss in deep learning models.

By • 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

weight (w)loss0481216-4-2024min
Step
0
Weight
4.000
Loss
16.0000
0.0011.0
SGD: w ← w − α · ∇L(w)+ stochastic noise

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:

🐢 Too LowLR = 0.001
Barely moves — needs thousands of steps
✅ Just RightLR = 0.1
Smooth, rapid convergence to minimum
💥 Too HighLR = 1.0
Oscillates wildly — never converges

Coding Challenge: Gradient Descent Update

Let's write a standard gradient descent parameter update rule in Python!

  1. Create a variable weight initialized to 5.0.
  2. Create a variable gradient initialized to 0.4.
  3. Create a variable learning_rate initialized to 0.1.
  4. Update the weight variable by subtracting learning_rate * gradient.
  5. 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!

main.py
Run your code to see output here.