๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #93

Gradient Descent

This note formalizes gradient descent as an optimization algorithm in general terms โ€” building on the intuitive introduction in Gradient Descent (Intro) โ€” before the next three notes cover its three practical variants: batch, stochastic, and mini-batch.

The Optimization Problem, Stated Precisely

\[ \mathbf{w}^* = \arg\min_{\mathbf{w}} L(\mathbf{w}) \]

Training a neural network means searching for the weight vector \(\mathbf{w}^*\) that minimizes the loss function \(L\). Gradient descent solves this iteratively, not analytically โ€” repeatedly taking small steps in the direction that locally decreases \(L\) the fastest:

\[ \mathbf{w}_{t+1} = \mathbf{w}_t - \eta\nabla L(\mathbf{w}_t) \]

Convex vs Non-Convex Loss Surfaces

Surface TypePropertyDeep Learning Reality
ConvexExactly one global minimum, no local minima or saddle pointsTrue for simple models like linear/logistic regression
Non-convexMany local minima, saddle points, flat regionsTrue for essentially every neural network's loss surface

This matters practically: gradient descent on a neural network has no guarantee of finding the global minimum โ€” only a local one, or a point where the gradient happens to be near zero (recall from Hessian that this could be a saddle point, not a true minimum). In practice, this turns out to matter less than the theory might suggest โ€” empirically, most local minima found by gradient descent in large networks perform comparably well, and saddle points, not bad local minima, are the more common practical obstacle.

Iterative Descent, Visualized

Each step moves opposite to the gradient โ€” as the surface flattens near the minimum, the gradient shrinks, so steps naturally get smaller.

Code โ€” The General Algorithm

import torch

def gradient_descent(loss_fn, w_init, lr=0.1, steps=50):
    w = w_init.clone().requires_grad_(True)
    for step in range(steps):
        loss = loss_fn(w)
        loss.backward()
        with torch.no_grad():
            w -= lr * w.grad
        w.grad.zero_()
    return w

# Example: minimize f(w) = (w - 3)^2, whose minimum is clearly at w=3
f = lambda w: (w - 3) ** 2
w_final = gradient_descent(f, torch.tensor(0.0), lr=0.1, steps=50)
print(w_final)   # converges toward 3.0

Common Mistakes

  • Assuming gradient descent always finds the global minimum for a neural network's non-convex loss โ€” it only guarantees convergence to a stationary point (zero gradient), which could be a local minimum or a saddle point.
  • Forgetting that "gradient descent" is a family of algorithms, not one fixed procedure โ€” the next three notes cover meaningfully different variants based on how much data each update uses.

Interview Relevance

Q: "Does gradient descent guarantee finding the best possible neural network weights?" No โ€” a neural network's loss surface is non-convex, so gradient descent can only guarantee convergence to a local minimum or a stationary point (which could even be a saddle point), not the global minimum. In practice, this is less catastrophic than it sounds โ€” most local minima found in large, over-parameterized networks tend to generalize comparably well.

Practice Question

For \(f(w) = (w-5)^2 + 2\), what is the true minimum value and at what \(w\)? Starting from \(w=0\) with a small learning rate, would gradient descent eventually reach it?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Gradient Descent โ€“ FAQs

Quick answers about learning Gradient Descent in Deep Learning.

This free note from CodingNow 2.0 explains Gradient Descent in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Gradient Descent, is 100% free with no signup required.
With focused practice, most students grasp Gradient Descent in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now