You have just finished defining the architecture of a custom deep neural network by subclassing torch.nn.Module in PyTorch. Before you can execute the training loop to feed data and update the model's weights, which of the following operations must you perform?
Select an answer to reveal the explanation.
Short Explanation
Alright, you've just built your neural network, but it still can't learn until you tell it how wrong it is and how to fix itself. You need to define the loss criterion and instantiate the optimizer with model.parameters() before the training loop starts. Once those are in place, you're ready to feed data, compute gradients, and update weights.
Full Explanation
In a PyTorch deep learning pipeline, the model architecture is defined by subclassing torch.nn.Module and specifying the forward pass. However, a model structure is simply a set of layers and mathematical transformations. For training to occur, two crucial components must be initialized: the loss function (or criterion) and the optimizer.
The loss function determines how the model's predictions are evaluated against the ground truth labels. It computes a scalar value representing the error. The optimizer (such as Stochastic Gradient Descent, Adam, or RMSprop) is responsible for updating the model's weights based on the computed gradients during backpropagation. When instantiating the optimizer, you must explicitly pass the model's parameters (via model.parameters()) so that the optimizer knows which tensors it is responsible for updating.
The other choices do not fit: - The choice about serializing the model using torch.save to initialize the weight tensor shapes is incorrect because saving the model using torch.save is a serialization step typically performed after training (or at checkpoints during training) to persist weights. It is not a prerequisite for running the training loop. - The choice about calling the forward() method directly on the model class to compile the training graph is incorrect because calling the forward() method directly is not how we pass data through the model; instead, we call the model instance as a callable (e.g., outputs = model(inputs)), which internally invokes hooks and then calls forward. Furthermore, calling this before initializing the optimizer/loss would not compile anything or set up the optimization parameters. - The choice about evaluating the model using the test dataset to establish a baseline accuracy is incorrect because evaluating the model on the test dataset before training is premature and yields random baseline results. While sometimes done to verify pipeline integrity, it is not a structural requirement for starting the training loop, nor does it prepare the model's parameters for optimization.