Skip to main content

Model Evaluation and Inference

In this article, we are going to focus on two crucial steps in the machine learning pipeline: Model Evaluation and Inference. Once a model has been trained using a framework like PyTorch, it's essential to evaluate its performance and use it for predictions (inference).

So, let's get started!

Model Evaluation

Model evaluation is the process of determining how well your model performs on unseen data. The most common way to do this is by splitting your dataset into training and testing sets.

import torch
from sklearn.model_selection import train_test_split

# Assume X is your data and Y are your labels
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)

After training your model on the training set, you can evaluate its performance on the testing set.

Metrics

There are several metrics available for evaluating a model's performance, depending on the task at hand. For classification tasks, metrics include accuracy, precision, recall, and F1 score. For regression tasks, common metrics are Mean Absolute Error (MAE), Mean Squared Error (MSE), and R-squared.

from sklearn.metrics import accuracy_score

# Assume 'model' is your trained PyTorch model and 'device' is your PyTorch device
model.eval()

with torch.no_grad():
correct = 0
total = 0
for images, labels in X_test:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print('Test Accuracy of the model on the test images: {} %'.format((correct / total) * 100))

Model Inference

Once we are satisfied with our model's performance, we use it to make predictions on new, unseen data. This process is known as inference.

# Assume 'model' is your trained PyTorch model and 'device' is your PyTorch device
model.eval()

with torch.no_grad():
new_data = torch.tensor([new_data]).to(device)
outputs = model(new_data)
_, predicted = torch.max(outputs.data, 1)

print('Predicted label: ', predicted.item())

Saving and Loading Models

Another important aspect of model evaluation and inference is the ability to save your trained models for future use and load them when needed.

# Saving model
torch.save(model.state_dict(), 'model.ckpt')

# Loading model
model = TheModelClass(*args, **kwargs) # model class needs to be defined as was defined during training
model.load_state_dict(torch.load('model.ckpt'))

Conclusion

Model evaluation and inference are critical steps in the machine learning workflow. They help us determine how well our model will perform on unseen data and utilize the model to make predictions. PyTorch provides straightforward and flexible tools for both these steps, making it a popular choice for developing and deploying machine learning models.

Happy coding!