Model Pipeline - requires_grad flag
This pipeline shows how the requires_grad flag in PyTorch controls whether a tensor tracks operations for gradient calculation during training. It helps decide which parts of the model learn by updating weights.
This pipeline shows how the requires_grad flag in PyTorch controls whether a tensor tracks operations for gradient calculation during training. It helps decide which parts of the model learn by updating weights.
Loss 0.8 |**** 0.5 |*** 0.3 |** Epochs -> 1 2 3
| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.8 | 0.5 | Initial loss high, gradients computed for requires_grad=True tensors |
| 2 | 0.5 | 0.7 | Loss decreases as model learns, gradients update weights |
| 3 | 0.3 | 0.85 | Loss further decreases, accuracy improves, requires_grad=True tensors updated |
requires_grad=True on a PyTorch tensor do?requires_grad=True tells PyTorch to remember all operations on the tensor so it can compute gradients later.requires_grad=True.requires_grad=True, which is correct. Options C and D use invalid argument names, and B disables gradient tracking.import torch x = torch.tensor([2.0, 3.0], requires_grad=True) y = x * 2 y.backward(torch.tensor([1.0, 1.0])) print(x.grad)
import torch x = torch.tensor([1.0, 2.0, 3.0]) y = x * 3 y.backward()
requires_grad to False.param.requires_grad = False for each parameter. for param in model.layer1.parameters():
param.requires_grad = False does this correctly.Pass the test and this pattern moves unknown