Greetings! In today's lesson, we will learn how to use a trained PyTorch model to make predictions. In previous lessons, we've already learned how to define and train a neural network using the PyTorch library. Now, it's time to utilize our trained model and make it useful by producing predictions. To make the lesson as hands-on as possible, we'll be using a trained model to predict if a team is likely to win based on average goals scored by the team and average goals conceded by the opponent.
Before we dive into making predictions, let's briefly recap how to train a binary classification model. Here's the code snippet we will use for training:
Python1import torch 2import torch.nn as nn 3import torch.optim as optim 4 5# Input features [Average Goals Scored, Average Goals Conceded by Opponent] 6X = torch.tensor([ 7 [3.0, 0.5], [1.0, 1.0], [0.5, 2.0], [2.0, 1.5], 8 [3.5, 3.0], [2.0, 2.5], [1.5, 1.0], [0.5, 0.5], 9 [2.5, 0.8], [2.1, 2.0], [1.2, 0.5], [0.7, 1.5] 10], dtype=torch.float32) 11 12# Target outputs [1 if the team is likely to win, 0 otherwise] 13y = torch.tensor([[1], [0], [0], [1], [1], [0], [1], [0], [1], [0], [1], [0]], dtype=torch.float32) 14 15# Define the model using nn.Sequential 16model = nn.Sequential( 17 nn.Linear(2, 10), 18 nn.ReLU(), 19 nn.Linear(10, 1), 20 nn.Sigmoid() 21) 22 23# Define loss function and optimizer 24criterion = nn.BCELoss() 25optimizer = optim.Adam(model.parameters(), lr=0.01) 26 27# Train the model for 50 epochs 28for epoch in range(50): 29 model.train() # Set the model to training mode 30 optimizer.zero_grad() # Zero the gradients for iteration 31 outputs = model(X) # Compute predictions 32 loss = criterion(outputs, y) # Compute the loss 33 loss.backward() # Compute the gradient of the loss 34 optimizer.step() # Optimize the model parameters
The first crucial step after we've trained our model is to put it in evaluation mode using model.eval()
. But why do we need to do that? Models can behave differently during training and evaluation phases. For example, many components or layers of the model may have certain behaviors that only need to occur during training, like adjusting internal parameters based on the provided data.
Putting the model in evaluation mode ensures that these components function correctly for making predictions.
Let's go ahead and put our model in evaluation mode:
Python1# Set the model to evaluation mode 2model.eval()
Do keep in mind that once we finish predicting, if we want to do further training, we will need to set the model back to the training mode using model.train()
before starting the training phase.
When making predictions, we only need to feed input data into the model and get the outputs. We don't need to compute gradients because we're not updating the model's weights. As a good practice, we can disable gradient calculation to save memory and computation. PyTorch allows us to do this by wrapping the prediction code block in with torch.no_grad()
:
Here’s the code to create an input tensor, disable gradient calculation, and make a prediction:
Python1# Create a new input tensor 2new_input = torch.tensor([[4.0, 5.0]], dtype=torch.float32) 3 4# Set the model to evaluation mode 5model.eval() 6 7# Disable gradient calculation for inference 8with torch.no_grad(): 9 # Make a prediction for the new input 10 prediction = model(new_input)
With our model set to the evaluation mode and no gradients, it's time to feed our model some input data and ask it to make forward pass predictions. The input data should be of the same type and shape that the model was trained with. Here, prediction
is the output from the forward pass of the model.
Following the forward pass, the model will output raw values. These values are dependent on the architecture and final activation function of your model.
In our example, the output value will be in range (0,1), representing the probability of the class being 1 because our simplified model has a single output node with a Sigmoid activation function.
Let's convert this probability to a binary class label using a threshold, which is usually 0.5:
Python1# Print the raw output from the model 2print("Raw output:", prediction) 3 4# Convert the probability to a binary class label 5print("Prediction:", (prediction > 0.5).int().item())
The output of the above code will be:
Plain text1Raw output: tensor([[0.1766]]) 2Prediction: 0
This example demonstrates that the model predicted a probability lower than the threshold, ultimately classifying the input as class 0
. In our scenario, this means that the model determined the team is not likely to win the match based on the average goals scored by the team and the average goals conceded by the opponent, showcasing how the output probability is mapped to a class label based on the specified threshold.
Congratulations! You've just learned how to make predictions using a trained PyTorch model. We walked through the steps of transitioning the model to evaluation mode, disabling gradient computation while using the model to make predictions, and interpreting the output of the model.
By making your trained model provide predictions, you can now see the results of all your hard work during the training phase. Now you will get the chance to apply these concepts in different exercises, which will help you reinforce what you've learned today. The more you practice, the better you get! So, let's move forward and keep practicing.