Consider the following PyTorch code snippet. What will be the value of z.requires_grad after the operations?
import torch x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True) y = x * 2 y = y.detach() z = y + 1
Think about what detach() does to the computation graph and the requires_grad flag.
The detach() method creates a new tensor that shares data but does not require gradients. Therefore, y.requires_grad becomes False, and so does z.requires_grad because it is computed from y.
You want to train a simple linear model in PyTorch. Which tensor should have requires_grad=True to update its values during training?
Which tensors need gradients to update weights during backpropagation?
Model parameters need requires_grad=True so gradients are computed and weights updated. Input data and target labels do not require gradients.
What is the effect of setting requires_grad=False on some model parameters during training?
Think about what happens when gradients are not computed for some tensors.
Setting requires_grad=False tells PyTorch not to compute gradients for those tensors, reducing computation and speeding up training.
Examine the code below. Why does the backward call raise an error?
import torch x = torch.tensor([1.0, 2.0, 3.0]) y = x * 2 z = y.sum() z.backward()
Check if the tensor you want gradients for has requires_grad=True.
The tensor x was created without requires_grad=True, so PyTorch does not track operations for gradients. Calling backward() on z raises an error.
In PyTorch, if you perform an in-place operation on a tensor with requires_grad=True, what is the effect on the requires_grad flag and the computation graph?
Consider how in-place changes affect gradient tracking and graph integrity.
In-place operations keep requires_grad=True but can corrupt the computation graph, leading to runtime errors during backpropagation. PyTorch warns about this but does not automatically disable gradient tracking.