🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #30

Gradient Descent

Gradient descent is the algorithm that actually trains most ML models: starting from random parameters, repeatedly take a small step in the direction that reduces the loss the most — the negative gradient — until the loss stops improving.

Formula

\[ \theta := \theta - \alpha \frac{\partial J(\theta)}{\partial \theta} \]

\(\theta\) is a parameter being learned (e.g. a regression coefficient), \(J(\theta)\) is the cost/loss function, \(\frac{\partial J(\theta)}{\partial \theta}\) is its gradient with respect to \(\theta\), and \(\alpha\) (alpha) is the learning rate — how big a step to take. This update repeats until the loss converges.

Geometric Intuition — Rolling Downhill

minimum start

Each step moves opposite the gradient — perpendicular to the contour line — shrinking steps as the surface flattens near the minimum.

Worked Numerical Example

Minimize \(f(x) = x^2\) (so \(\frac{df}{dx} = 2x\)), starting at \(x_0 = 10\), with learning rate \(\alpha = 0.1\):

Stepxgradient = 2xx_new = x − α×gradient
0102010 − 0.1×20 = 8.0
18.0168.0 − 0.1×16 = 6.4
26.412.86.4 − 0.1×12.8 = 5.12
35.1210.245.12 − 0.1×10.24 = 4.10
x = 10.0
alpha = 0.1

for step in range(10):
    grad = 2 * x            # derivative of x^2
    x = x - alpha * grad
    print(step, round(x, 4))
# Output shrinks toward 0.0 — the minimum of x^2

Learning Rate — Why It's the Most Important Hyperparameter Here

Learning RateWhat Happens
Too small (e.g. 0.001)Converges, but takes many more steps than necessary — slow training
Good (e.g. 0.1 above)Steadily shrinks toward the minimum in a reasonable number of steps
Too large (e.g. 1.1)Overshoots the minimum and diverges — the loss gets worse each step

Diverging example: same function, \(\alpha = 1.1\), \(x_0 = 10\): step 1 → \(x = 10 - 1.1(20) = -12\); step 2 → \(x = -12 - 1.1(-24) = 14.4\) — the value is growing in magnitude and flipping sign every step instead of shrinking toward zero.

Practical Use Cases

  • Training linear regression and logistic regression when the closed-form solution isn't used
  • Training every neural network — this exact update rule, applied to millions of parameters via backpropagation

Limitations

  • Can get stuck in a poor local minimum for non-convex loss surfaces (common in deep learning, rare in classical linear models)
  • Sensitive to feature scale — unscaled features distort the loss surface into a narrow valley, slowing convergence. See Feature Scaling.

Common Mistakes

  • Picking a learning rate without ever checking the loss curve — always plot loss vs. iteration to catch divergence or painfully slow convergence early.
  • Forgetting to scale features before gradient-based training — this alone often fixes slow or unstable convergence.

Interview Relevance

Q: "Your model's loss is oscillating wildly and increasing during training — what's the likely cause?" The learning rate is too high, causing gradient descent to overshoot the minimum on every step instead of converging toward it — the fix is to lower \(\alpha\).

Practice Question

Using \(f(x) = x^2\), \(x_0 = 5\), \(\alpha = 0.2\), manually compute \(x_1\), \(x_2\) and \(x_3\).

Want to go beyond the notes?

Join CodingNow 2.0's Machine 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 Machine Learning.

This free note from CodingNow 2.0 explains Gradient Descent in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine 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