Summary - Autograd
- Machine Learning course
- Advanced
- Lesson 24/78
- 6 min
In this group you learned autograd, requires_grad, and backward().
Summary: autograd
In this group:
- Tensors with
requires_grad=Trueare tracked; derived tensors are tracked too. backward()computes gradients from a scalar result and fills the leaves'.grad.- Gradients accumulate; they must be zeroed every step.
no_grad()anddetach()stop tracking.- The computational graph is built dynamically;
grad_fnshows the nodes, the chain rule is applied backwards.
Practice Gradient descent by hand
Minimise kayip = (w - 3) ** 2 with 50 steps of gradient descent starting from w = 0 with a learning rate of 0.1: at each step compute the loss, call backward(), update inside torch.no_grad() and zero the gradient. Print the final w with round(..., 3).
Starter code
import torch
w = torch.tensor(0.0, requires_grad=True)
# buraya yazSample solution
import torch
w = torch.tensor(0.0, requires_grad=True)
for _ in range(50):
kayip = (w - 3) ** 2
kayip.backward()
with torch.no_grad():
w -= 0.1 * w.grad
w.grad.zero_()
print(round(w.item(), 3))Expected output
3.0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why is the update in gradient descent with autograd done inside <code>torch.no_grad()</code>?
- It isn't required
- To zero the gradient
- So the update itself isn't recorded in the graph Correct answer
- Because it is more accurate
Explanation: w -= lr·grad is a parameter adjustment, not a computation to differentiate.
2 Do the update without tracking.
with torch.no_grad():
w ____ 0.1 * w.grad
with torch.no_grad():
w -= 0.1 * w.grad
Explanation: A step is taken in the opposite direction of the gradient.
3 Match the tools.
| Term | Meaning |
|---|---|
backward() |
Gradient computation |
.grad |
Gradient value |
zero_() |
Zeroing |
no_grad() |
Untracked block |