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

Gradient

The gradient of a function is a vector of its partial derivatives — one per input variable — that points in the direction where the function increases fastest. It's the compass every optimization algorithm in ML follows.

Formula

\[ \nabla f(x, y) = \left[\ \frac{\partial f}{\partial x},\ \frac{\partial f}{\partial y}\ \right] \]

\(\nabla f\) ("nabla f", or "grad f") is a vector; each component is the partial derivative of \(f\) with respect to one variable, holding the others constant. It always points in the direction of steepest increase — which is exactly why ML moves in the opposite direction to minimize error.

Geometric Intuition — Contour Lines

min point (3,4) ∇f = [6, 8] contour line (tangent)

Contour lines are curves of equal function value. The gradient always points perpendicular to the contour, straight uphill.

Numerical Example

For \(f(x,y) = x^2 + y^2\), the partial derivatives are \(\frac{\partial f}{\partial x}=2x\) and \(\frac{\partial f}{\partial y}=2y\), so \(\nabla f = [2x, 2y]\).

\[ \text{At } (x,y)=(3,4):\quad \nabla f = [2(3),\ 2(4)] = [6, 8], \qquad \lVert \nabla f \rVert = \sqrt{36+64} = 10 \]
import numpy as np

def f(x, y):
    return x**2 + y**2

def gradient(x, y):
    return np.array([2*x, 2*y])   # analytical gradient for this function

g = gradient(3, 4)
print(g)                    # [6 8]
print(np.linalg.norm(g))     # 10.0

Why This Matters for ML

Training a model means minimizing a loss function — and the gradient tells you exactly which direction makes the loss worse fastest. So every optimizer takes a step in the negative gradient direction, which is precisely what Gradient Descent does.

Common Mistakes

  • Confusing the gradient's direction (steepest increase) with the direction you actually move in during training (steepest decrease — the negative gradient).
  • Thinking the gradient is a single number — for a function of multiple variables, it's always a vector, one component per input variable.

Interview Relevance

Q: "What does it mean when the gradient is the zero vector?" It means you're at a flat point of the function — a local minimum, maximum, or saddle point. Optimizers stop making progress there, which is exactly the signal gradient descent uses to know it has (approximately) converged.

Practice Question

For \(f(x,y) = x^2 + 2y^2\), derive \(\nabla f\) and evaluate it at \((x,y) = (1, 2)\).

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 – FAQs

Quick answers about learning Gradient in Machine Learning.

This free note from CodingNow 2.0 explains Gradient 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, is 100% free with no signup required.
With focused practice, most students grasp Gradient 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