Model Configuration, Training, and Predictions for Classification
Explore how to configure a PyTorch binary classification model including defining the model, loss function, and optimizer. Learn step-by-step training processes, interpret logits, convert them into probabilities, and make class predictions using an appropriate threshold.
We'll cover the following...
Model configuration
In the chapter, Going Classy, we ended up with a lean model configuration part. We only need to define a model, an appropriate loss function, and an optimizer. Let us define a model that produces logits and uses BCEWithLogitsLoss as the loss function. Since we have two features, and we are producing logits instead of probabilities, our model has one layer and one layer alone: Linear(2, 1). We will keep using the SGD optimizer with a learning rate of 0.1 for now.
This is what the model configuration looks like for our classification problem:
Model training
Time to train our model! We can leverage the StepByStep class we built in the chapter, Going Classy and use pretty much the same code as before:
After training our model, if we try plotting our results, we get the following training and validation losses:
“Wait, there is something weird with this plot…”
Having cleared that, it is time to inspect the model’s trained parameters:
GPU users will get an output similar to the following:
Our model produced logits, right? So we can plug the weights above in the corresponding logit equation (equation 6.3), and we end up with:
...