Selective differentiation
A model or mathematical expression produces selected tensors and gradients are needed for only particular inputs.
PyTorch · Function
Computes and returns gradients of selected outputs with respect to selected inputs, without accumulating them in the inputs' .grad fields. It supports vector-Jacobian products, graph retention, higher-order derivatives, and batched gradient vectors.
The function differentiates one or more output tensors with respect to specified inputs and returns the resulting gradients as a tuple. It forms a vector-Jacobian product using grad_outputs, can retain or build a derivative graph, and returns gradients directly instead of accumulating them in input .grad fields.
torch.autograd.grad(*outputs: _TensorOrTensorsOrGradEdge*, *inputs: _TensorOrTensorsOrGradEdge*, *grad_outputs: Tensor | Sequence[Tensor | None] | None = None*, *retain_graph: bool | None = None*, *create_graph: bool = False*, *only_inputs: bool = True*, *allow_unused: bool | None = None*, *is_grads_batched: bool = False*, *materialize_grads: bool = False*) → tuple[Tensor, ...]| Name | Type | Description |
|---|---|---|
outputs | Tensor or sequence of Tensor or GradientEdge | Outputs of the differentiated function. |
inputs | Tensor or sequence of Tensor or GradientEdge | Inputs for which gradients are returned rather than accumulated in .grad. |
grad_outputs | Tensor or sequence of Tensor or None, optional | Vector-Jacobian-product vectors corresponding to outputs; optional for suitable outputs. |
retain_graph | bool, optional | Whether to preserve the graph used by this call; defaults to the value of create_graph. |
create_graph | bool, optional | Whether to construct a graph for the derivative, allowing higher-order derivatives. |
only_inputs | bool, optional | Legacy option; the current API returns gradients only for inputs. |
allow_unused | bool, optional | Whether to allow inputs unrelated to outputs; otherwise such inputs can raise an error. |
is_grads_batched | bool, optional | Whether the first dimension of grad_outputs is a batch of vectors. |
materialize_grads | bool, optional | Whether unused inputs should receive zero tensors instead of being treated as an error. |
A tuple of tensors containing gradients with respect to inputs.
import torch
x = torch.tensor([2.0, 3.0], requires_grad=True)
y = x.pow(2)
grad_x, = torch.autograd.grad(y.sum(), x)
print(grad_x) # tensor([4., 6.])Computes the gradient of the summed output with respect to x without populating x.grad.
A model or mathematical expression produces selected tensors and gradients are needed for only particular inputs.
A scalar loss or output must be differentiated while preserving the input tensors' .grad attributes.
Optimization, meta-learning, or sensitivity analysis requires derivatives of derivatives.
Several vector-Jacobian products share a leading batch dimension.
It computes and returns the sum of gradients of outputs with respect to inputs. Unlike backward, the gradients are returned and are not accumulated into the inputs' .grad attributes.
Follow-up: How would you differentiate a non-scalar output? Supply grad_outputs containing the vector used for the vector-Jacobian product, with a compatible entry for each output.
grad_outputs supplies the vector in the vector-Jacobian product. It normally matches the output structure and can contain None for scalar outputs or outputs that do not require gradients.
Follow-up: How can a batch of vectors be processed? Set is_grads_batched=True so the first dimension of each grad_outputs tensor is treated as the batch dimension.
create_graph=True constructs a graph for the derivative, enabling higher-order derivative products. retain_graph controls whether the graph used for the current gradient computation is freed.
Follow-up: What memory concern can arise when repeatedly retaining graphs? Retaining graphs keeps the computation history available and can increase memory use, so enable it only when another traversal is required.
torch.autograd.backwardtorch.autograd.functional.jacobiantorch.autograd.functional.hessiantorch.func.jacrev