In a PyTorch training loop, what is the primary role of calling the backward() method on a loss tensor?
Select an answer to reveal the explanation.
Short Explanation
When you train a network in PyTorch, the forward pass gives you a loss, but the model learns by adjusting weights. Calling backward() on that loss tensor kicks off autograd, calculates gradients with the chain rule, and stores them in each parameter's .grad attribute. Without it, your model won't learn.
Full Explanation
In PyTorch, the backward() method is the gateway to automatic differentiation via torch.autograd. When a model processes input data to generate predictions and compute a loss, PyTorch dynamically builds a directed acyclic graph of the operations performed on the tensors. When you call loss.backward(), PyTorch traverses this graph in reverse order, applying the mathematical chain rule to calculate the derivative (gradient) of the loss with respect to every leaf node tensor (such as model weights and biases) that has requires_grad=True. These calculated gradients are accumulated and stored in each tensor's .grad attribute. An optimizer (like SGD or Adam) then uses these stored gradients during the optimizer.step() call to update the model's weights. Let's review the other choices: the NumPy conversion choice describes the .numpy() method, which converts a PyTorch tensor to a NumPy array. The normal-distribution initialization choice refers to parameter initialization methods (such as torch.nn.init functions), which set up the starting weights. The forward-pass choice refers to the forward() method of a torch.nn.Module subclass, which defines the forward propagation steps. For the exam, make sure you connect the .backward() call on the loss tensor with gradient calculation via automatic differentiation.