What is requires_grad in PyTorch: Explanation and Usage
requires_grad is a property of PyTorch tensors that tells the system whether to track operations on the tensor for gradient calculation during backpropagation. If requires_grad=True, PyTorch records all operations on the tensor so it can compute gradients for optimization.How It Works
Imagine you are baking a cake and want to remember every step you took so you can adjust the recipe later. In PyTorch, requires_grad works like a notebook that records every operation done on a tensor. When set to True, PyTorch tracks all changes to that tensor so it can calculate how to adjust it to improve a model.
This tracking is essential for training machine learning models because it allows PyTorch to automatically compute gradients, which tell the model how to update its parameters to reduce errors. If requires_grad is False, PyTorch skips this tracking to save memory and computation when gradients are not needed.
Example
This example shows how setting requires_grad=True enables gradient calculation for a tensor during a simple operation.
import torch # Create a tensor with requires_grad=True to track computation x = torch.tensor([2.0, 3.0], requires_grad=True) # Perform a simple operation y = x * x + 3 # Compute the sum to get a scalar output z = y.sum() # Backpropagate to compute gradients z.backward() # Print gradients of x gradients = x.grad print(gradients)
When to Use
Use requires_grad=True when you want PyTorch to calculate gradients for a tensor, which is necessary during training of neural networks or any model that learns by adjusting parameters. For example, model weights usually have requires_grad=True so they can be updated by optimizers.
On the other hand, use requires_grad=False for tensors that hold fixed data or intermediate results where you don't want to waste resources tracking gradients. This is common during evaluation or inference when you only want predictions without training.
Key Points
- requires_grad=True enables gradient tracking for tensors.
- Gradients are needed for training models using backpropagation.
- Disabling gradient tracking saves memory and speeds up inference.
- Model parameters usually have
requires_grad=True. - You can toggle
requires_gradanytime to control tracking.