1.2.12Calculus & Optimization Basics

Gradient descent intuition and update rule

2,277 words10 min readdifficulty · medium

Core Idea

Why this works: The gradient L(θ)\nabla L(\theta) points in the direction of steepest increase. So L(θ)-\nabla L(\theta) points toward stepest decrease. By iteratively moving opposite to the gradient, we "roll downhill" toward a minimum.


The Mathematics: Deriving the Update Rule

Step 1: First-Order Taylor Approximation

Start with why we can use gradients at all. Around a point θ\theta, the loss function can be approximated:

L(θ+Δθ)L(θ)+L(TˆΔθL(\theta + \Delta\theta) \approx L(\theta) + \nabla L(\^T \Delta\theta

Why this step? Taylor's theorem says any smooth function is locally linear. The gradient L(θ)\nabla L(\theta) tells us how LL changes for small Δθ\Delta\theta.

Step 2: Choose the Best Direction

We want to find Δθ\Delta\theta that decreases LL the most. The change in loss is:

L(θ+Δθ)L(θ)L(θ)TΔθL(\theta + \Delta\theta) - L(\theta) \approx \nabla L(\theta)^T \Delta\theta

For this to be negative (loss decreases), we need L(θ)TΔθ<0\nabla L(\theta)^T \Delta\theta < 0.

Key insight: The dot product L(θ)TΔθ=LΔθcos(α)\nabla L(\theta)^T \Delta\theta = |\nabla L| \cdot |\Delta\theta| \cos(\alpha), where α\alpha is the angle between the gradient and our step direction.

This is most negative when cos(α)=1\cos(\alpha) = -1, meaning α=180°\alpha = 180° — we move exactly opposite to the gradient!

Step 3: Control Step Size

If we set Δθ=L(θ)\Delta\theta = -\nabla L(\theta), we might overshoot. Introduce a learning rate η>0\eta > 0 to scale the step:

Δθ=ηL(θ)\Delta\theta = -\eta \nabla L(\theta)

Why this step? The gradient gives direction, but not the optimal magnitude. η\eta lets us tune how agressively we descend.

Figure — Gradient descent intuition and update rule

Worked Examples


Common Mistakes


Deep Dive: Why This Update Rule is Optimal (Locally)

Question: Is the gradient direction provably the best local direction?

Answer: Yes, for infinitesimal steps! Here's the rigorous argument:

We want to solve:

minΔθ=ϵL(θ+Δθ)\min_{\|\Delta\theta\| = \epsilon} L(\theta + \Delta\theta)

Using first-order Taylor:

minΔθ=ϵL(θ)TΔθ\min_{\|\Delta\theta\| = \epsilon} \nabla L(\theta)^T \Delta\theta

This is minimized when Δθ\Delta\theta is antiparallel to L(θ)\nabla L(\theta):

Δθ=ϵL(θ)L(θ)\Delta\theta^* = -\epsilon \frac{\nabla L(\theta)}{\|\nabla L(\theta)\|}

Scaling by η\eta (which absorbs ϵ/L\epsilon / \|\nabla L\|), we recover:

Δθ=ηL(θ)\Delta\theta = -\eta \nabla L(\theta)

Conclusion: Gradient descent is the stepest descent method in the local neighborhood.


Learning Rate: The Critical Hyperparameter

Rule of thumb: For quadratic losses L(θ)=12θTAθL(\theta) = \frac{1}{2}\theta^T A \theta, stability requires:

η<2λmax(A)\eta < \frac{2}{\lambda_{\max}(A)}

where λmax\lambda_{\max} is the largest eigenvalue of AA.

Why? The update becomes θt+1=(IηA)θt\theta_{t+1} = (I - \eta A)\theta_t. This converges only if eigenvalues of (IηA)(I - \eta A) are in (1,1)(-1, 1).


Connections to Other Concepts

  • Loss Functions — Gradient descent minimizes any differentiable loss
  • Backpropagation — Efficiently computes L\nabla L in neural networks
  • Learning Rate Schedules — Adaptive strategies for η\eta (decay, warm-up)
  • Stochastic Gradient Descent — Uses mini-batches instead of full dataset
  • Momentum and Adam — Enhanced GD with velocity/adaptive rates
  • Convex Optimization — GD guarantees global minimum for convex LL
  • Local Minima and Saddle Points — Challenges in non-convex landscapes
  • Second-Order Methods — Newton's method uses Hessian, not just gradient


Recall Explain to a 12-Year-Old

Imagine you're playing a video game where you're stuck in a maze on a hillside, and you need to find the treasure at the bottom. But it's super fogy — you can only see your feet!

Here's your strategy:

  1. Feel the ground. Which way tilts downward? That's your hint.
  2. Take a small step downhill.
  3. Stop and feel again. Repeat.

Gradient descent is exactly this! Your "feeling the ground" is calculating the gradient (the slope). The "step downhill" is updating your position using the rule:

new position=old position(step size)×(slope)\text{new position} = \text{old position} - \text{(step size)} \times \text{(slope)}

The "step size" is called the learning rate. Too big, and you'll trip and overshoot the treasure. Too small, and you'll take forever to get there.

In machine learning, the "treasure" is the best model parameters that make your predictions super accurate!


Flashcards

What is the gradient descent update rule? :: θt+1=θtηL(θt)\theta_{t+1} = \theta_t - \eta \nabla L(\theta_t), where η\eta is the learning rate and L(θt)\nabla L(\theta_t) is the gradient at θt\theta_t.

Why do we use the NEGATIVE gradient in gradient descent?
The gradient L\nabla L points in the direction of steepest INCREASE of the loss. We want to DECREASE loss, so we move in the opposite direction: L-\nabla L.

What happens if the learning rate η\eta is too large? :: The updates overshoot the minimum, causing the loss to oscillate or diverge instead of converging. The linear approximation (Taylor expansion) breaks down.

What happens if the learning rate η\eta is too small? :: Convergence is extremely slow — the algorithm takes tiny steps and requires many iterations to reach the minimum.

In the hill analogy, what does the gradient represent?
The gradient represents the direction and stepness of the slope. It points UPHILL (stepest ascent), so we go the opposite way to descend.
Derive the gradient descent direction from first principles.
Using Taylor expansion: L(θ+Δθ)L(θ)+L(θ)TΔθL(\theta + \Delta\theta) \approx L(\theta) + \nabla L(\theta)^T \Delta\theta. To minimize the change, we need L(θ)TΔθ<0\nabla L(\theta)^T \Delta\theta < 0. This is most negative when Δθ\Delta\theta is antiparallel to L\nabla L, giving Δθ=ηL(θ)\Delta\theta = -\eta \nabla L(\theta).
What is the "stepest descent" property of gradient descent?
Among all directions with the same step magnitude, the negative gradient direction produces the largest decrease in the loss function (locally, under first-order approximation).
In Example 1 (L(θ)=θ2L(\theta) = \theta^2), why does θt=4(0.8)t\theta_t = 4 \cdot (0.8)^t?
Each update multiplies the current value by (12η)=10.2=0.8(1 - 2\eta) = 1 - 0.2 = 0.8 (since L=2θ\nabla L = 2\theta and η=0.1\eta=0.1), forming a geometric sequence converging to 0.

Concept Map

slope measured by

points to steepest increase

justifies local linearity of

expands using

forms

most negative at 180 deg

equals

scales step size of

combined with eta gives

iterated to reach

Loss function L theta

Gradient of L

Negative gradient

Taylor approximation

Dot product with cos alpha

Steepest descent direction

Learning rate eta

Update rule

Local minimum theta*

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Gradient descent ek simple lekin powerful optimization technique hai jo machine learning ka backbone hai. Socho ki tumhe ek loss function minimize karni hai — basically, tumhare model ki errors ko kam karna hai. Loss function ko imagine karoek pahad ki tarah, jahan sabse niche ki point (valley) tumhara target hai — wahi pe model sabse accurate hota hai.

Gradient descent ka logic simple hai: agar tum blindfolded ho ek pahad pe, toh tum kaise niche jaoge? Tum feel karoge ki konsi direction mein slope sabse steep downward hai, aur wahan ek chhota step loge. Phir dobara feel karoge, aur phir se step. Repeat karte raho jab tak tum valley mein nahi pahunch jate. Mathematically, yeh gradient ka negative direction hota hai kyunki gradient uphill direction point karta hai — hume opposite jana hai. Update rule simple hai: theta_new = theta_old - (learning_rate × gradient). Yahan learning rate (eta) controls karo ki kitna bada step lena hai — bahut bada toh tum overshoot karoge aur oscillate karoge, bahut chhota toh bahut time lagega converge hone mein.

Yeh technique isliye powerful hai kyunki yeh differentiable functions ke liye kaam karta hai, aur backpropagation ke through neural networks mein efficiently compute ho sakta hai. Lekin dhyan rakho: agar loss surface non-convex hai (multiple valleys), toh tum local minimumein fas sakte ho instead of global minimum. Real-world mein hum variants use karte hain jaise Stochastic Gradient Descent (mini-batches pe), Momentum (inertia add karta hai), aur Adam (adaptive learning rates). Core concept yahi hai: gradient tumhe direction bata hai, aur tum systematically parameters update karte raho until loss minimize ho jaye.

Practical terms mein, jab tum koi neural network train karte ho — images classify karna ho, language generate karna ho, ya game khelna ho — gradient descent hi hai jo weights ko iteratively adjust karta hai taki predictions better hoti jayein. Starting point random hota hai, phir lakhs of iterations mein model "seekhta" hai by following the gradient. Yeh optimization ka workhorse hai AI mein!

Go deeper — visual, from zero

Test yourself — Calculus & Optimization Basics

Connections