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 and Infographic
Alright, check this out. You've built your shiny new neural network model in PyTorch—that's awesome. But if you try to kick off your training loop right now, you're going to crash and burn. Why? Because the model has no idea how to measure its mistakes, and it has no mechanism to update its weights. That's where your loss function and optimizer come into play. Think of the loss function as the coach telling you how far off target you are, and the optimizer as the steering wheel adjusting your parameters to get you closer. You've got to instantiate both of these—like nn.CrossEntropyLoss() and optim.Adam(model.parameters())—before that loop starts spinning. Get this right, and you're ready to roll!
Full explanation below image
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.
Let's look at why the other options do not fit: - Option A 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. - Option C 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. - Option D 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.