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

PyTorch · Module

tensor.requires_grad_

Changes a tensor’s requires_grad setting in place. By default, it enables autograd recording for subsequent operations involving that tensor.

How it works

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.

Syntax

Tensor.requires_grad_(requires_grad=True)

Parameters

NameTypeDescription
requires_gradboolWhether autograd should record operations on this tensor; defaults to True.

Returns

The same tensor, after its requires_grad setting is changed.

Key points

  • Use the method to control whether autograd records later operations involving the tensor.
  • The configured tensor itself is returned, allowing the call to participate in an expression or assignment.
  • A common use is enabling autograd on an already-created tensor before subsequent computation.
  • It is especially useful when the tensor currently has gradient recording disabled.

Common mistakes

  • The Boolean argument controls whether autograd records operations involving the tensor.

Example: Enable gradients on an existing tensor

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.

When to use it

Enable autograd on existing data

An existing tensor was loaded or initialized without gradient tracking and must participate in later differentiation.

Start tracking after preprocessing

A preprocessing pipeline produces a tensor that should become the starting point for differentiable computation.

Disable recording

A tensor should stop participating in gradient recording for later operations.

Interview questions

QWhat does 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.

QWhat parameter does the method accept, and what is its default?

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.

QWhat does the method return, and why does in-place behavior matter?

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_grad
  • torch.Tensor.is_leaf
  • torch.Tensor.grad
  • torch.Tensor.backward
  • torch.nn.Module.requires_grad_

Source: torch.Tensor.requires_grad_ — PyTorch 2.13 documentation. Checked 2026-10-16.