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

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?

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 โ€“ FAQs

Quick answers about learning Gradient in Deep Learning.

This free note from CodingNow 2.0 explains Gradient 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, 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