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

Gradient Vector

This note zooms in on the gradient specifically as a vector object โ€” its shape, how to interpret its individual components, and how it's represented in practice for a real neural network with millions of parameters.

Shape of the Gradient

For a loss function \(L(\mathbf{w})\) where \(\mathbf{w}\) is a weight vector of length \(n\), the gradient \(\nabla L(\mathbf{w})\) is also a vector of length \(n\) โ€” one entry per weight, in the exact same shape as \(\mathbf{w}\) itself. This is by design: it lets you update every weight with one aligned vector subtraction, \(\mathbf{w} \leftarrow \mathbf{w} - \eta\nabla L\).

Reading Individual Components

\[ \nabla L(\mathbf{w}) = \left[\frac{\partial L}{\partial w_1}, \frac{\partial L}{\partial w_2}, \ldots, \frac{\partial L}{\partial w_n}\right] \]
Component ValueInterpretation
Large positiveIncreasing this weight would increase the loss a lot โ€” decrease it during the update.
Large negativeIncreasing this weight would decrease the loss a lot โ€” increase it during the update.
Near zeroThe loss is barely sensitive to this weight right now โ€” it's near a local flat spot for this parameter.

Numerical Example โ€” A Weight Vector in Practice

\[ \mathbf{w} = [0.5, -0.2, 1.3], \qquad \nabla L(\mathbf{w}) = [0.8, -0.1, 2.4] \] \[ \mathbf{w}_{\text{new}} = \mathbf{w} - 0.1 \cdot \nabla L(\mathbf{w}) = [0.5-0.08,\ -0.2+0.01,\ 1.3-0.24] = [0.42, -0.19, 1.06] \]

Notice the third weight, with the largest-magnitude gradient component (2.4), moved the most โ€” the update is proportional to how sensitive the loss is to each individual weight.

Code

import torch

w = torch.tensor([0.5, -0.2, 1.3], requires_grad=True)
loss = (w[0]-1)**2 + (w[1]+0.5)**2 + (w[2]-2)**2   # a toy loss over 3 weights

loss.backward()
print(w.grad)   # the gradient vector, same shape as w: tensor([-1.0000, 0.6000, -1.4000])

learning_rate = 0.1
with torch.no_grad():
    w -= learning_rate * w.grad   # gradient descent update, applied to the whole vector at once
print(w)

Where This Shows Up in Deep Learning

In practice, a network's gradient isn't one flat vector โ€” PyTorch stores a separate gradient tensor for every parameter tensor in the model (each layer's weight matrix and bias vector gets its own .grad), matching that parameter's own shape exactly. Conceptually, though, they all together form one giant gradient vector over the network's full parameter space, and every optimizer (SGD, Adam, ...) operates on that structure.

Common Mistakes

  • Forgetting that PyTorch accumulates gradients by default across multiple .backward() calls โ€” without calling optimizer.zero_grad() before each new backward pass, gradients from previous steps silently add into the current one.
  • Interpreting the gradient vector's raw magnitude as "how wrong the model is" โ€” the loss value itself measures that; the gradient measures sensitivity/direction, not error magnitude directly.

Interview Relevance

Q: "Why must PyTorch's optimizer.zero_grad() be called before every .backward()?" Because gradients accumulate (sum) into .grad by default across successive backward passes. Without resetting them to zero first, each new gradient computation would be added on top of the previous step's gradient, corrupting the update direction.

Practice Question

A weight vector has gradient \([-3, 0.5, -0.01]\). With a learning rate of 0.01, which weight will change the most in this update step, and in which direction (increase or decrease)?

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

Quick answers about learning Gradient Vector in Deep Learning.

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