.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "beginner/basics/saveloadrun_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_beginner_basics_saveloadrun_tutorial.py: `Learn the Basics `_ || `Quickstart `_ || `Tensors `_ || `Datasets & DataLoaders `_ || `Transforms `_ || `Build Model `_ || `Autograd `_ || `Optimization `_ || **Save & Load Model** Save and Load the Model ============================ In this section we will look at how to persist model state with saving, loading and running model predictions. .. GENERATED FROM PYTHON SOURCE LINES 17-22 .. code-block:: default import torch import torchvision.models as models .. GENERATED FROM PYTHON SOURCE LINES 23-28 Saving and Loading Model Weights -------------------------------- PyTorch models store the learned parameters in an internal state dictionary, called ``state_dict``. These can be persisted via the ``torch.save`` method: .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: default model = models.vgg16(weights='IMAGENET1K_V1') torch.save(model.state_dict(), 'model_weights.pth') .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading: "https://download.pytorch.org/models/vgg16-397923af.pth" to /var/lib/ci-user/.cache/torch/hub/checkpoints/vgg16-397923af.pth 0%| | 0.00/528M [00:00`_ module when serializing the model, thus it relies on the actual class definition to be available when loading the model. .. GENERATED FROM PYTHON SOURCE LINES 61-65 Related Tutorials ----------------- - `Saving and Loading a General Checkpoint in PyTorch `_ - `Tips for loading an nn.Module from a checkpoint `_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 8.725 seconds) .. _sphx_glr_download_beginner_basics_saveloadrun_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: saveloadrun_tutorial.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: saveloadrun_tutorial.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_