mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-22 06:11:27 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/40288 Test Plan: Imported from OSS Differential Revision: D22140324 Pulled By: jamesr66a fbshipit-source-id: 01d7aa642ed2f4e4bdac4b7f3223bf4d7e62fd4d
41 lines
1.1 KiB
ReStructuredText
41 lines
1.1 KiB
ReStructuredText
|
|
Serialization semantics
|
|
=======================
|
|
|
|
Best practices
|
|
--------------
|
|
|
|
.. _recommend-saving-models:
|
|
|
|
Recommended approach for saving a model
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
There are two main approaches for serializing and restoring a model.
|
|
|
|
The first (recommended) saves and loads only the model parameters::
|
|
|
|
torch.save(the_model.state_dict(), PATH)
|
|
|
|
Then later::
|
|
|
|
the_model = TheModelClass(*args, **kwargs)
|
|
the_model.load_state_dict(torch.load(PATH))
|
|
|
|
The second saves and loads the entire model::
|
|
|
|
torch.save(the_model, PATH)
|
|
|
|
Then later::
|
|
|
|
the_model = torch.load(PATH)
|
|
|
|
However in this case, the serialized data is bound to the specific classes
|
|
and the exact directory structure used, so it can break in various ways when
|
|
used in other projects, or after some serious refactors.
|
|
|
|
.. note::
|
|
The 1.6 release of PyTorch switched ``torch.save`` to use a new
|
|
zipfile-based file format. ``torch.load`` still retains the ability to
|
|
load files in the old format. If for any reason you want ``torch.save``
|
|
to use the old format, pass the kwarg ``_use_new_zipfile_serialization=False``.
|