PyTorch Load Model + Examples

In this  Python tutorial, we will learn about the PyTorch load model and we will also cover different examples related to the PyTorch load model. And we will cover these topics.

  • PyTorch load model
  • PyTorch load model example
  • PyTorch load model checkpoint
  • PyTorch load model to GPU
  • PyTorch load model for inference
  • PyTorch load model continue training
  • PyTorch load model to the device
  • PyTorch load model from bin file
  • PyTorch load model from pth path
  • PyTorch load model without class

PyTorch load model

In this section, we will learn about how we can load the PyTorch model in python.

  • PyTorch load model is defined as a process of loading the model after saving the data.
  • The torch.load() function is used to load the data it is the unpacking facility but handle storage which underline tensors.

Syntax:

In this syntax, we will load the data of the model.

torch.load(f,map_location=None,pickle_module,pickle_load_args)

Parameters:

  • f is a file-like object has implement read(),tell(), etc or string.In Path-like object hold a file name.
  • map_location is a function or string which identifies how to edit storage locations.
  • pickle_module is used for unpacking metadata.
  • pickle_load_args is used as an optional keyword argument which proceeds over to pickle_module.load().

Read: PyTorch Batch Normalization

PyTorch load model example

In this section, we will learn about how we can load the PyTorch model with the help of examples in python.

Torch.load() function is used to load the model it is an unpicking facility but it handles the storage which underlines tensors.

Code:

In the following code, we import some libraries from which we can load our model.

  • nn.MaxPool2d(2, 2) is used to apply on the input signal collecting from several input planes.
  • nn.Linear() is used to make feed-forward networks.
  • optimizers = optim.SGD(models.parameters(), lr=0.001, momentum=0.9) is used to initialize the model.
  • print(param_tensor, “\t”, models.state_dict()[param_tensor].size()) is used to print the model.
  • torch.save() is used to save the model.
  • models.load_state_dict() is used to load the model.
import torch
from torch import nn
import torch.optim as optim
import torch.nn.functional as f
# Define model
class TheModelClass(nn.Module):
    def __init__(self):
        super(TheModelClass, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(18 * 7 * 7, 140)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(F.relu(self.conv1(X)))
        X = self.pool(F.relu(self.conv2(X)))
        X = x.view(-1, 16 * 5 * 5)
        X = f.relu(self.fc1(X))
        X = f.relu(self.fc2(X))
        X = self.fc3(X)
        return X

models = TheModelClass()



# Initialize optimizer
optimizers = optim.SGD(models.parameters(), lr=0.001, momentum=0.9)


print("Models state_dict:")
for param_tensor in models.state_dict():
    print(param_tensor, "\t", models.state_dict()[param_tensor].size())


print("Optimizers state_dict:")
for var_name in optimizers.state_dict():
    print(var_name, "\t", optimizers.state_dict()[var_name])
torch.save(models.state_dict(), 'model_weights.pth')
models.load_state_dict(torch.load('model_weights.pth'))
models.eval()

Output:

After running the above code we get the following output in which we can see that the loading model data is printed on the screen.

PyTorch load model example
PyTorch load model example

Read: PyTorch Save Model – Complete Guide

PyTorch load model checkpoint

In this section, we will learn about the PyTorch load model checkpoint in Python.

PyTorch load model checkpoint is used to load the model. To load the model we can firstly be initializing the model and after that optimizer then load.

Code:

In the following code, we will import some libraries from which we can load the checkpoints.

  • optimizer = optimizer.SGD(net.parameters(), lr=0.001, momentum=0.9) is used to initialize the optimizer.
  • torch.save() is used to save the model.
  • checkpoint = torch.load() is used to load the model.
import torch
import torch.nn as nn
import torch.optim as optim
class net(nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(18 * 5 * 5, 140)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(f.relu(self.conv1(X)))
        X = self.pool(f.relu(self.conv2(X)))
        X = X.view(-1, 16 * 5 * 5)
        X = f.relu(self.fc1(X))
        X = f.relu(self.fc2(X))
        X = self.fc3(X)
        return X

netout = net()
print(netout)
optimizers = optim.SGD(netout.parameters(), lr=0.001, momentum=0.9)
# Additional information
epoch = 7
path = "model.pt"
loss = 0.6

torch.save({
            'epoch': epoch,
            'model_state_dict': netout.state_dict(),
            'optimizer_state_dict': optimizers.state_dict(),
            'loss': loss,
            }, path)
models = net()
optimizers = optim.SGD(netout.parameters(), lr=0.001, momentum=0.9)

CheckPoint = torch.load(path)
models.load_state_dict(CheckPoint['model_state_dict'])
optimizers.load_state_dict(CheckPoint['optimizer_state_dict'])
Epoch = CheckPoint['epoch']
Loss = CheckPoint['loss']

models.eval()

Output:

READ:  How to Create a Snake game in Python using Turtle

After running the above code, we get the following output in which we can see that the model checkpoint is loaded on the screen.

Pytorch load model checkpoint
Pytorch load model checkpoint

Read: What is Scikit Learn in Python

PyTorch load model to GPU

In this section, we will learn about how to load the model to GPU in Python.

PyTorch load model to GPU is used to load model the model to GPU. Firstly we will initialize the data after initializing save the data and after that, we will load the model to GPU.

Code:

In the following code, we will import some libraries for loading our model to GPU.

  • Path = “model.pt” is used to give the path where our model saves or after saving we can load out a model to this path.
  • torch.save(net.state_dict(), Path) is used to save the model.
  • torch.device(“cuda”) is used to set up and run the Cuda operation.
  • model.load_state_dict(torch.load(Path)) is used to load the model.
import torch
from torch import nn
from torch import optim
import torch.nn.functional as f
class model(nn.Module):
    def __init__(self):
        super(model, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(f.relu(self.conv1(X)))
        X = self.pool(f.relu(self.conv2(X)))
        X = x.view(-1, 16 * 5 * 5)
        X = f.relu(self.fc1(X))
        X = f.relu(self.fc2(X))
        X = self.fc3(X)
        return X

netout = model()
print(netout)
Path = "model.pt"
# save
torch.save(netout.state_dict(), Path)

# load
device = torch.device("cuda")
model = model()
model.load_state_dict(torch.load(Path))
model.to(device)

Output:

In the following code, we initialize the model after initializing save our model after saving the model we can again load the model and the load model to GPUdata is printed on the screen.

Pytorch load model to gpu
Pytorch load model to gpu

Read: Tensorflow in Python

PyTorch load model for inference

In this section, we will learn about the PyTorch load model for inference in Python.

PyTorch load model for inference is defined as a conclusion that arrived at the evidence and reasoning.

Code:

In the following code, we will import some libraries from which we can load our model.

  • optimizer = optimizer.SGD(net.parameters(), lr=0.001, momentum=0.9) is used to initialize the optimizer.
  • Path = “state_dict_model.pt” is used to give the path to our model.
  • torch.save(net.state_dict(), Path) is used to save the model.
  • model.load_state_dict(torch.load(Path)) is used to load the model.
import torch
from torch import nn
import torch.optim as optimizer
import torch.nn.functional as f
class net(nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(f.relu(self.conv1(x)))
        X = self.pool(f.relu(self.conv2(x)))
        X = x.view(-1, 16 * 5 * 5)
        X = f.relu(self.fc1(X))
        X = f.relu(self.fc2(X))
        X = self.fc3(X)
        return X_test

netoutput = net()
print(netoutput)
optimizer = optimizer.SGD(netoutput.parameters(), lr=0.001, momentum=0.9)

Path = "state_dict_model.pt"


torch.save(netoutput.state_dict(), Path)


model = net()
model.load_state_dict(torch.load(Path))
model.eval()

Output:

After running the above code, we get the following output in which we can see that the PyTorch load model inference data is printed on the screen.

Pytorch load model inference
Pytorch load model inference

Read: TensorFlow get shape

PyTorch load model continue training

In this section, we will learn about the PyTorch load model continue training in python.

PyTorch load model continues training is defined as a process of continuous training the model and loading the model with the help of a torch.load() function.

READ:  Python Pandas Write to Excel + Examples

Code:

In the following code, we will import some libraries from which we can load the continued training model.

  • optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) is used to initialize the optimizer.
  • torch.save() is used to save the model.
  • optimizer.load_state_dict() is used to load the continue training model.
  • model.eval() is used to evaluate the model.
import torch
import torch.nn as nn
import torch.optim as optim
class train_model(nn.Module):
    def __init__(self):
        super(train_model, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(F.relu(self.conv1(X)))
        X = self.pool(F.relu(self.conv2(X)))
        X = X.view(-1, 16 * 5 * 5)
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        x = self.fc3(X)
        return X
netoutput = train_model()
#print(netoutput)
optimizers = optim.SGD(netoutput.parameters(), lr=0.001, momentum=0.9)
epoch = 7
path = "model.pt"
loss = 0.6
torch.save({
            'epoch': epoch,
            'model_state_dict': netoutput.state_dict(),
            'optimizers_state_dict': optimizers.state_dict(),
            'loss': loss,
            }, path)

models = train_model()
optimizers = optim.SGD(models.parameters(), lr=0.001, momentum=0.9)

CheckPoint = torch.load(path)
models.load_state_dict(CheckPoint['model_state_dict'])
optimizers.load_state_dict(CheckPoint['optimizers_state_dict'])
Epoch = CheckPoint['epoch']
Loss = CheckPoint['loss']

models.eval()

models.train()

Output:

After running the above code, we get the following output in which we can see that the continued training model is loaded on the screen.

PyTorch load model continue training
PyTorch load model continue training

Read: PyTorch Tensor to Numpy

PyTorch load model to the device

In this section, we will learn about how to load the PyTorch model to the device in python.

PyTorch load model to the device is used to load the multiple components to the device with the help of a torch.load() function.

Code:

In the following code, we will import some libraries from which we can load the model to the device.

  • optimizer = optimizer.SGD(netoutput.parameters(), lr=0.001, momentum=0.9) is used to initialize the optimizer.
  • Path = “state_dict_model.pt” is used to specify the path.
  • torch.save(netoutput.state_dict(), Path) is used to save the model to cpu.
  • models.load_state_dict(torch.load(Path)) is used to load the model.
  • models.to(device) is used to load the model to device.

import torch
from torch import nn
import torch.optim as optimizer
import torch.nn.functional as f
class Netmodel(nn.Module):
    def __init__(self):
        super(Netmodel, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(f.relu(self.conv1(x)))
        X = self.pool(f.relu(self.conv2(x)))
        X = x.view(-1, 16 * 5 * 5)
        X = f.relu(self.fc1(X))
        X = f.relu(self.fc2(X))
        X = self.fc3(X)
        return X_test

netoutput = Netmodel()
print(netoutput)
optimizer = optimizer.SGD(netoutput.parameters(), lr=0.001, momentum=0.9)

Path = "state_dict_model.pt"


torch.save(netoutput.state_dict(), Path)


device = torch.device("cuda")
models = Netmodel()
models.load_state_dict(torch.load(Path))
models.to(device)

Output:

In the following output, we can see that firstly train model data is printed after training the model save the model and after that, we load the model, and the loading data to the device is printed on the screen.

PyTorch load model to device
PyTorch load model to the device

Read PyTorch Binary Cross Entropy

PyTorch load model from bin file

In this section, we will learn about how to load a PyTorch model from a bin file in python.

  • Before moving forward we should have a piece of knowledge about the bin file.Bin file is a compressed form of a binary file.
  • The binary file is defined as a file the content written inside the file is only explained by the programmer and hardware.
  • PyTorch load model from bin file is defined as to load the model from the bin with the help of a torch.load() function.

Code:

In the following code, we will import some libraries from which we can load the model from the bin file.

  • nn.Linear() is used to make the feed-forward network.
  • File = “model.pth” is used to give the path.
  • torch.save(model.state_dict(), File) is used to save the model.
  • loadmodel.load_state_dict() is used to load the model.
import torch
import torch.nn as nn

class Model(nn.Module):
    def __init__(self, ninput_features):
        super(Model, self).__init__()
        self.linear = nn.Linear(ninput_features, 1)

    def forward(self, X):
        ypred = torch.sigmoid(self.linear(X))
        return ypred

model = Model(ninput_features=8)



for param in model.parameters():
    print(param)

File = "model.pth"
torch.save(model.state_dict(), File)

print(model.state_dict())
loadmodel = Model(ninput_features=8)
loadmodel.load_state_dict(torch.load(File)) # it takes the loaded dictionary, not the path file itself
loadmodel.eval()

print(loadmodel.state_dict())

Output:

READ:  Python Tkinter to Display Data in Textboxes

In the following output, we can see that the PyTorch model data is loaded from the bin and printed on the screen.

PyTorch load model from bin file
PyTorch load model from the bin file

Read PyTorch Logistic Regression

PyTorch load model from pth path

In this section, we will learn about how to load the PyTorch model from the pth path in python.

  • PyTorch load model from the pth path is defined as a process from which we can load our model with the help of a torch.load() function.
  • The PyTorch regular convention is used to save the model using the .pth file extension.

Code:

In the following code, we will import some libraries from which we can load the model from the pth path.

  • model = ModelClass() is used to initialize the model.
  • optimizers = optimizer.SGD(model.parameters(), lr=0.001, momentum=0.9) is used to initialize the optimizer.
  • print(param_tensor, “\t”, model.state_dict()[param_tensor].size()) is used to print the model state_dict.
  • torch.save(model.state_dict(), ‘model_weights.pth’) is used to save the model.
  • model.load_state_dict(torch.load(‘model_weights.pth’)) is used to load the model.
  • model.eval() is used to evaluate the model.
import torch
from torch import nn
import torch.optim as optimizer
import torch.nn.functional as f
# Define model
class ModelClass(nn.Module):
    def __init__(self):
        super(ModelClass, self).__init__()
        self.conv1 = nn.Conv2d(5, 8, 7)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(8, 18, 7)
        self.fc1 = nn.Linear(18 * 7 * 7, 140)
        self.fc2 = nn.Linear(140, 86)
        self.fc3 = nn.Linear(86, 12)

    def forward(self, X):
        X = self.pool(f.relu(self.conv1(X)))
        X = self.pool(f.relu(self.conv2(X)))
        X = x.view(-1, 16 * 5 * 5)
        X = f.relu(self.fc1(X))
        X = f.relu(self.fc2(X))
        X = self.fc3(X)
        return X


model = ModelClass()


optimizers = optimizer.SGD(model.parameters(), lr=0.001, momentum=0.9)


print("Model state_dict:")
for param_tensor in model.state_dict():
    print(param_tensor, "\t", model.state_dict()[param_tensor].size())



torch.save(model.state_dict(), 'model_weights.pth')
model.load_state_dict(torch.load('model_weights.pth'))
model.eval()

Output:

After running the above code, we get the following output in which we can see that the model is loaded on the screen from the pth path.

Pytorch load model to the device
Pytorch load model to the device

PyTorch load model without class

In this section, we will learn about the PyTorch load model without class in python.

  • As we know we can load our PyTorch load model without class with the help of a function torch.load().
  • After saving the model we can load the model and the load model is unpicking facility and handle the storage.

Code:

In the following code, we will import the torch module from which we can load the model without class.

  • model = trainmodel(inputfeatures=4) is used to initialize the model.
  • File = “model.pth” is used to give the path to the file.
  • torch.save(model, File) is used to save the model.
  • load_model = torch.load(File) is used to load the model without using class.
  • load_model.eval() is used to evaluate the loaded model.
  • print(param) is used to print the parameters of the loaded model.
import torch
import torch.nn as nn
class trainmodel(nn.Module):
    def __init__(self, inputfeatures):
        super(trainmodel, self).__init__()
        self.linear = nn.Linear(inputfeatures, 1)

    def forward(self, X):
        y_pred = torch.sigmoid(self.linear(X))
        return y_pred

model = trainmodel(inputfeatures=4)
File = "model.pth"
torch.save(model, File)

load_model = torch.load(File)
load_model.eval()

for param in load_model.parameters():
    print(param)

Output:

After running the above code, we get the following output in which we can see that the PyTorch load model without class data is printed on the screen.

PyTorch load model without class
PyTorch load model without class

So, in this tutorial, we discussed the PyTorch load model and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • PyTorch load model
  • PyTorch load model example
  • PyTorch load model checkpoint
  • PyTorch load model to GPU
  • PyTorch load model for inference
  • PyTorch load model continue training
  • PyTorch load model to the device
  • PyTorch load model from bin file
  • PyTorch load model from pth path
  • PyTorch load model without class