Reading and Zeroing .grad
Why gradients accumulate and must be reset.
Reading and Zeroing .grad is a free Deep Learning Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Gradients Accumulate
Here is the surprise: backward adds to .grad rather than replacing it. Call backward twice without clearing and the numbers pile up.
Why Accumulation Exists
This adding behavior is on purpose. It lets you sum gradients from several mini-batches before one update, handy for simulating a larger batch.
The Hidden Bug
Forget to clear and old gradients poison your next step, so the model trains on the wrong numbers. This silent bug trips up almost everyone once.
Zero Them Each Step
The fix is to reset gradients before each backward. With an optimizer you simply call zero_grad() at the top of every training step.
optimizer.zero_grad()The Right Order
The loop rhythm is fixed: zero_grad, forward, loss, backward, step. Zeroing first guarantees each step uses only this batch's gradients.
optimizer.zero_grad()
loss.backward()
optimizer.step()Clearing Without an Optimizer
No optimizer yet? You can null the gradients yourself by setting each tensor's .grad back to None before the next backward call.
w.grad = Noneset_to_none Is Cheaper
Newer code prefers zero_grad(set_to_none=True). Setting grads to None instead of filling zeros saves a little memory and time.
Reading Grads to Debug
Peeking at .grad is great for debugging. All zeros may mean a dead neuron, and huge values warn of exploding gradients before training blows up.
A Habit Worth Forming
Make zeroing automatic in your head. Every reliable training loop clears gradients first, so the model only ever learns from the current batch.
Quick Check
Recap
Gradients live in .grad and accumulate across backward calls, so you must clear them each step with zero_grad. Forgetting that quietly breaks training. 🧹
Frequently asked questions
Is the “Reading and Zeroing .grad” lesson free?
Yes — the full text of “Reading and Zeroing .grad” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading and Zeroing .grad”?
Why gradients accumulate and must be reset. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading and Zeroing .grad” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- requires_grad and the Computation Graph
- Call backward() to Get Gradients
- Reading and Zeroing .grad
- torch.no_grad() for Inference