Skip to main content

A simple PyTorch checkpoint manager

Project description

PyTorch Checkpoint Manager

A custom PyTorch checkpoint manager inspired by TensorFlow's CheckpointManager. Specify the necessary arguments in the constructor and then use the CheckpointManager.save() and CheckpointManager.load() methods to save/load models. Functionality is similar to that of torch.save() and torch.load().

Example usage

The following is a simple convolutional network for demonstrating the checkpoint manager's functionality.

Imports:

# Neural network source: https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_a_general_checkpoint.html

import torch
import torch.nn as nn
from ckpt_manager import CheckpointManager

Create the neural network and its optimizer:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    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

net = Net()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

Create the CheckpointManager:

manager = CheckpointManager(
    assets={
        'model' : net.state_dict(),
        'optimizer' : optimizer.state_dict()
    },
    directory='training_checkpoints',
    file_name='model',
    maximum=3,
    file_format='pt'
)

Save the states to the directory specified in the constructor:

manager.save()

Load the states from the directory:

load_data = manager.load()

net.load_state_dict(load_data['model'])
optimizer.load_state_dict(load_data['optimizer'])

If there is nothing to load, net and optimizer won't be altered.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytorch-ckpt-manager-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

pytorch_ckpt_manager-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file pytorch-ckpt-manager-0.1.0.tar.gz.

File metadata

  • Download URL: pytorch-ckpt-manager-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pytorch-ckpt-manager-0.1.0.tar.gz
Algorithm Hash digest
SHA256 56409b6cf65c4b90bc1c049520451e3928e92695c637029c7e1dfb8d099ee66c
MD5 fe22db57f839eedab80a6a23134679f8
BLAKE2b-256 edbcbd09fa52b4e32167298add3d95efc18b6a39c802e6d67c96da16b120d213

See more details on using hashes here.

File details

Details for the file pytorch_ckpt_manager-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytorch_ckpt_manager-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pytorch_ckpt_manager-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29d19b252129a0933e199f286a406d47a99339d2c7f61bef789f4f8e3d3b2b6e
MD5 2bd22eaa41bf59d66fe16bdd1fd14437
BLAKE2b-256 632f122b36e62d0b5b81c438afbab71413d5ea723c865e90882c85cb13655376

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page