Making Predictions with a Trained PyTorch Model

Introduction

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.

Brief Recap of Training

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:

import torch
import torch.nn as nn
import torch.optim as optim

# Input features [Average Goals Scored, Average Goals Conceded by Opponent]
X = torch.tensor([
    [3.0, 0.5], [1.0, 1.0], [0.5, 2.0], [2.0, 1.5],
    [3.5, 3.0], [2.0, 2.5], [1.5, 1.0], [0.5, 0.5],
    [2.5, 0.8], [2.1, 2.0], [1.2, 0.5], [0.7, 1.5]
], dtype=torch.float32)

# Target outputs [1 if the team is likely to win, 0 otherwise]
y = torch.tensor([[1], [0], [0], [1], [1], [0], [1], [0], [1], [0], [1], [0]], dtype=torch.float32)

# Define the model using nn.Sequential
model = nn.Sequential(
    nn.Linear(2, 10),
    nn.ReLU(),
    nn.Linear(10, 1),
    nn.Sigmoid()
)

# Define loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)  

# Train the model for 50 epochs
for epoch in range(50):  
    model.train()  # Set the model to training mode
    optimizer.zero_grad()  # Zero the gradients for iteration
    outputs = model(X)  # Compute predictions
    loss = criterion(outputs, y)  # Compute the loss
    loss.backward()  # Compute the gradient of the loss
    optimizer.step()  # Optimize the model parameters

Switching the Model to Evaluation Mode

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:

Python
# Set the model to evaluation mode
model.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.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal