pytorch-saver
Simple helper to save and load PyTorch models.
repository: https://github.com/mathpn/pytorch-saver
Why use this package to save and load models?
PyTorch suggests two standard ways of saving and loading models: either saving their state_dict or saving the pickled model object itself.
Both methods have drawbacks:
- Saving the state_dict is very flexible, however we loose all the arguments used to create the model, the optimizer and (optionally) the scheduler;
- Saving a pickled snapshot solves this issue, but it's not flexible at all. Even minor changes to the model class can break the unpickling process and the arguments used to define the object are still obscured behind the objects themselves.
Therefore, the goal of this package is to provide a pratical way of creating models and associated objects, saving, and loading them without headaches. Also, any additional metadata should be included in the saved file.
Installing
Install with pip:
pip install pytorch-saver
Or clone the repository and go inside its folder:
cd pytorch-saver
and install from source:
pip install .
How to use it
Initializing objects
Import ModelContainer and create a new container instance.
from pytorch_saver import ModelContainer
container = ModelContainer()
This is the only part of the pipeline that breaks with Python conventions. Since we need to store all arguments used to create the objects as to recreate them, they are created through the initialize method.
Pass all the classes and dictionaries with all keyword arguments to initialize them to the initialize method.
model_objects = container.initialize(
Model,
model_kwargs,
torch.optim.Adam,
optim_kwargs
)
Model objects is a NamedTuple with three attributes: model, optimizer and scheduler. Access these objects (if created though initialize) and train your model.
Saving checkpoints
Use the save method to save checkpoints:
container.save("./", "tutorial")
This will save a checkpoint to "./tutorial_checkpoint_TIMESTAMP.zip", where TIMESTAMP is the current Unix timestamp in seconds.
Any additional keyword arguments provided will be saved as model metadata, as long as they are JSON-serializable.
container.save("./", "tutorial", loss=0.55, epoch=5)
If you only want to save the model (ignoring optimizer and scheduler), use the save_inference method.
Loading saved files
Use the load method to load checkpoints:
from pytorch_saver import ModelContainer
container = ModelContainer()
metadata, objs = container.load(file_path)
"metadata" is a dictionary with the arguments used to initialize all objects, the timestamp, and any additional arguments passed to the saved method when saving this file.
"objs" is a NamedTuple with the same structure as the one returned by the initialize method.
Metadata
Release files for pytorch-saver 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pytorch_saver-0.1.1.tar.gz | 7.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytorch_saver-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.2 kB
Release files / pytorch_saver-0.1.1.tar.gz
| Download URL | pytorch_saver-0.1.1.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2e9b24fd79e05c985840757d0bca4b043004d1b3859abad76c87e611ea9972a3
|
|
BLAKE2b-256 checksum How to use checksums |
b8cc0e4c29e976bcf0d835370f073dc2650a46b54a5914e2f6f7dcdb94567ad5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / pytorch_saver-0.1.1-py3-none-any.whl
| Download URL | pytorch_saver-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5932765f6f6aae5d508c63e5fd83f91893a00e2f156763df5b0cf8808244cab2
|
|
BLAKE2b-256 checksum How to use checksums |
086f752c2869a08df228c0a1db1f19aac7e1d34afeb885097a450feaf6c8ec3c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Security for pytorch-saver 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.