Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #219

Gradient

The gradient collects every partial derivative of a function into a single vector — and for a neural network's loss, that vector points in the exact direction that increases the loss fastest. Gradient descent simply walks in the opposite direction.

Definition

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

For a function of \(n\) variables, the gradient \(\nabla f\) is a vector of \(n\) partial derivatives — one per input variable. For a neural network's loss \(L(w_1, \ldots, w_n)\), the gradient \(\nabla L\) is a vector with one entry per weight, telling you exactly how sensitive the loss is to each individual weight.

Numerical Example

\[ f(x, y) = x^2 + y^2 \] \[ \nabla f = [2x,\ 2y], \qquad \nabla f(3, 4) = [6, 8] \]

Why the Gradient Points "Uphill"

minimum ∇f (uphill) -∇f (downhill)

The gradient always points in the direction of steepest increase; gradient descent moves in exactly the opposite direction, \(-\nabla f\), to decrease the loss fastest.

Code

import torch

x = torch.tensor(3.0, requires_grad=True)
y = torch.tensor(4.0, requires_grad=True)

f = x**2 + y**2
f.backward()

print(x.grad, y.grad)   # tensor(6.) tensor(8.) -- matches [2x, 2y] at (3,4)

Where This Shows Up in Deep Learning

Gradient descent's weight update rule is built directly on this: \(\mathbf{w} \leftarrow \mathbf{w} - \eta \nabla L(\mathbf{w})\), where \(\eta\) is the learning rate. Because \(\nabla L\) points toward increasing loss, subtracting it (moving in the \(-\nabla L\) direction) is exactly how every weight in the network gets nudged toward reducing the loss. This is covered in full in the Optimization category next.

Common Mistakes

  • Forgetting the minus sign — the gradient points uphill; forgetting to negate it in a weight update would make the loss get worse, not better.
  • Confusing the gradient (a vector) with a single derivative (a scalar) — the gradient only makes sense for functions of more than one variable, which is every practical loss function in deep learning.

Interview Relevance

Q: "Why does gradient descent subtract the gradient instead of adding it?" The gradient points in the direction of steepest increase of the function. Since training wants to minimize the loss, weights are updated in the opposite direction — hence subtracting \(\eta\nabla L\), not adding it.

Practice Question

For \(f(x,y) = 3x^2 + 5y\), compute the gradient \(\nabla f\) and evaluate it at \((x,y) = (2, -1)\). In which direction would gradient descent move from this point?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →