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 and Infographic
Okay, let's dive in. When you're training a network in PyTorch, you run your data forward through the model to get a loss value—that's the forward pass. But how does the model actually learn? It has to adjust its weights. To do that, it needs to calculate gradients, which tell it which way to nudge those weights. Calling .backward() on your loss tensor kicks off the autograd engine, calculating the gradients using the chain rule and saving them in the .grad attribute of each parameter. Without this, your model would never learn a thing! That makes Option D the right answer here.
Full explanation below image
Full Explanation
In PyTorch, the backward() method is the gateway to automatic differentiation (via the torch.autograd engine). When a model processes input data to generate predictions and compute a loss, PyTorch dynamically builds a directed acyclic graph (DAG) of the operations performed on the tensors. When you call loss.backward(), PyTorch traverses this DAG 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 options: Option A describes the .numpy() method, which converts a PyTorch tensor to a NumPy array. Option B refers to parameter initialization methods (such as torch.nn.init functions), which set up the starting weights. Option C 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.