Enable autograd on existing data
An existing tensor was loaded or initialized without gradient tracking and must participate in later differentiation.
PyTorch · Module
Changes a tensor’s requires_grad setting in place. By default, it enables autograd recording for subsequent operations involving that tensor.
Call this method on a tensor to change its requires_grad setting in place. With the default True value, subsequent operations involving the tensor can be recorded by autograd; passing False disables recording for that tensor.
Tensor.requires_grad_(requires_grad=True)| Name | Type | Description |
|---|---|---|
requires_grad | bool | Whether autograd should record operations on this tensor; defaults to True. |
The same tensor, after its requires_grad setting is changed.
import torch
x = torch.tensor([1.0, 2.0])
x.requires_grad_()
y = (x ** 2).sum()
y.backward()
print(x.grad)Enables recording on the tensor in place, then computes and prints gradients for the resulting scalar.
An existing tensor was loaded or initialized without gradient tracking and must participate in later differentiation.
A preprocessing pipeline produces a tensor that should become the starting point for differentiable computation.
A tensor should stop participating in gradient recording for later operations.
Tensor.requires_grad_() do?It changes the tensor's requires_grad attribute in place and returns that same tensor. Its primary purpose is to start autograd recording for an existing tensor that currently does not require gradients.
Follow-up: Why might an existing tensor have gradients disabled? It may come from a DataLoader or from preprocessing or initialization, after which this method can enable recording.
The requires_grad parameter is a Boolean that determines whether autograd should record operations on the tensor. It defaults to True, so an argument-less call enables recording.
Follow-up: What call disables recording? Use tensor.requires_grad_(False), which changes the same setting in place.
The method returns the tensor after changing its setting. Because the change is in place, the original tensor is configured for subsequent autograd-tracked operations.
Follow-up: How can the enabled setting be used? Perform later operations and then apply backward computation to obtain gradients where autograd supports them.
torch.Tensor.requires_gradtorch.Tensor.is_leaftorch.Tensor.gradtorch.Tensor.backwardtorch.nn.Module.requires_grad_