One flag flips the whole machine on
requires_grad=True tells PyTorch "watch this tensor — record everything that happens to it so I can compute gradients later." It's the single switch that turns a calculator into a learning machine.
Three things to know:
- Tensors default to
requires_grad=False. You opt in. - If any input to an operation has
requires_grad=True, the output also hasrequires_grad=True— the property propagates. - Model parameters (created via
nn.Parameterinside annn.Module) are automatically set torequires_grad=True. You almost never need to set it manually.
The grad_fn attribute on a tensor tells you which operation produced it (if any). Leaf tensors — the ones you created directly — have grad_fn=None. Intermediates have things like <AddBackward0>, <MulBackward0>, etc. We'll dive into autograd properly in the next track; for now, just know that requires_grad=True is the door, and the door is opened by either you or by nn.Parameter on your behalf.