Module 15 of 26 · Deep Learning with PyTorch · Intermediate

deploying-models

Duration: 8 min

This module covers the essential steps and best practices for deploying deep learning models using PyTorch. Understanding deployment is crucial as it bridges the gap between model development and real-world application, ensuring models are accessible and performant in production environments.

Model Export and Serialization

Model export and serialization are the first steps in deploying a PyTorch model. Exporting involves saving the model's architecture and weights to a file, while serialization ensures that the model can be loaded and used in different environments. This process is essential for making the model portable and reusable.

import torch

# Define a simple model
class SimpleModel(torch.nn.Module):
    def __init__(self):
        super(SimpleModel, self).__init__()
        self.fc = torch.nn.Linear(10, 1)

    def forward(self, x):
        return self.fc(x)

# Instantiate the model
model = SimpleModel()

# Export the model
torch.save(model.state_dict(),'model.pth')
print('Model exported successfully')

Try it in Google Colab: Open in Colab

Model exported successfully

Model Loading and Inference

Once a model is exported, it can be loaded and used for inference in different environments. This involves loading the model's architecture and weights from the saved file and preparing it for making predictions. Proper loading ensures that the model behaves as expected in production.

import torch

# Define the same model architecture
class SimpleModel(torch.nn.Module):
    def __init__(self):
        super(SimpleModel, self).__init__()
        self.fc = torch.nn.Linear(10, 1)

    def forward(self, x):
        return self.fc(x)

# Instantiate the model
model = SimpleModel()

# Load the model weights
model.load_state_dict(torch.load('model.pth'))
model.eval()  # Set the model to evaluation mode

# Prepare a sample input
input_tensor = torch.randn(1, 10)

# Perform inference
with torch.no_grad():
    output = model(input_tensor)
print('Inference output:', output)
Inference output: tensor([[0.0423]], grad_fn=<AddmmBackward>)

💡 Tip: Ensure that the model is set to evaluation mode using model.eval() before performing inference to disable any training-specific behaviors like dropout.

❓ What is the purpose of setting the model to evaluation mode?

❓ What function is used to load the model weights from a file?

← Previous Continue interactively → Next →

Related Courses