0PricingLogin
Deep Learning Academy · Lesson

Reading and Zeroing .grad

Why gradients accumulate and must be reset.

The .grad Attribute

After backward, every trained tensor stores its gradient in .grad. Reading it tells you how the loss responds to changes in that tensor.

print(weight.grad)

Gradients Start as None

Before any backward call, .grad is None, not zero. PyTorch only allocates it once the first set of gradients actually arrives.

All lessons in this course

  1. requires_grad and the Computation Graph
  2. Call backward() to Get Gradients
  3. Reading and Zeroing .grad
  4. torch.no_grad() for Inference
← Back to Deep Learning Academy