Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

autograd.grad

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.

How it works

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.

Syntax

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, ...]

Parameters

NameTypeDescription
outputsTensor or sequence of Tensor or GradientEdgeOutputs of the differentiated function.
inputsTensor or sequence of Tensor or GradientEdgeInputs for which gradients are returned rather than accumulated in .grad.
grad_outputsTensor or sequence of Tensor or None, optionalVector-Jacobian-product vectors corresponding to outputs; optional for suitable outputs.
retain_graphbool, optionalWhether to preserve the graph used by this call; defaults to the value of create_graph.
create_graphbool, optionalWhether to construct a graph for the derivative, allowing higher-order derivatives.
only_inputsbool, optionalLegacy option; the current API returns gradients only for inputs.
allow_unusedbool, optionalWhether to allow inputs unrelated to outputs; otherwise such inputs can raise an error.
is_grads_batchedbool, optionalWhether the first dimension of grad_outputs is a batch of vectors.
materialize_gradsbool, optionalWhether unused inputs should receive zero tensors instead of being treated as an error.

Returns

A tuple of tensors containing gradients with respect to inputs.

Key points

  • The function returns the summed derivatives of the requested outputs with respect to the requested inputs.
  • Each requested input receives a corresponding returned gradient; the input's .grad field is not populated by this call.
  • For non-scalar outputs, grad_outputs supplies the vector that weights the Jacobian in the vector-Jacobian product.
  • With is_grads_batched=True, the leading dimension of grad_outputs represents multiple vectors and enables batched vector-Jacobian products.
  • create_graph enables the returned derivative computation to remain differentiable for higher-order products.
  • retain_graph=False releases the graph after computing the requested gradients.

Common mistakes

  • The graph is normally released after the derivative is computed; set retain_graph=True when another derivative pass needs the same graph.
  • Set create_graph=True before requesting higher-order derivatives; otherwise the returned gradient is not recorded for differentiating again.
  • Use backward when the goal is to accumulate gradients into graph leaves rather than obtain returned gradients for selected inputs.

Example: Compute a gradient without accumulation

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.

When to use it

Selective differentiation

A model or mathematical expression produces selected tensors and gradients are needed for only particular inputs.

Non-accumulating gradient queries

A scalar loss or output must be differentiated while preserving the input tensors' .grad attributes.

Higher-order derivatives

Optimization, meta-learning, or sensitivity analysis requires derivatives of derivatives.

Batched vector-Jacobian products

Several vector-Jacobian products share a leading batch dimension.

Interview questions

QWhat does torch.autograd.grad return, and how does it differ from backward?

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.

QWhat is the purpose of grad_outputs?

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.

QWhen should create_graph and retain_graph be enabled?

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.backward
  • torch.autograd.functional.jacobian
  • torch.autograd.functional.hessian
  • torch.func.jacrev

Source: torch.autograd.grad — PyTorch 2.14 documentation. Checked 2026-10-16.