-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add weights_only=True to torch.load #3012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,14 @@ | |
########################## | ||
# To load model weights, you need to create an instance of the same model first, and then load the parameters | ||
# using ``load_state_dict()`` method. | ||
# | ||
# In the code below, we set ``weights_only=True`` to limit the | ||
# functions executed during unpickling to only those necessary for | ||
# loading weights. Using ``weights_only=True`` is considered | ||
# a best practice when loading weights. | ||
|
||
model = models.vgg16() # we do not specify ``weights``, i.e. create untrained model | ||
model.load_state_dict(torch.load('model_weights.pth')) | ||
model.load_state_dict(torch.load('model_weights.pth', weights_only=True)) | ||
model.eval() | ||
|
||
########################### | ||
|
@@ -50,9 +55,14 @@ | |
torch.save(model, 'model.pth') | ||
|
||
######################## | ||
# We can then load the model like this: | ||
# We can then load the model as demonstrated below. | ||
# | ||
# As described in `Saving and loading torch.nn.Modules <pytorch.org/docs/main/notes/serialization.html#saving-and-loading-torch-nn-modules>`__, | ||
# saving ``state_dict``s is considered the best practice. However, | ||
# below we use ``weights_only=False`` because this involves loading the | ||
# model, which is a legacy use case for ``torch.save``. | ||
|
||
model = torch.load('model.pth') | ||
model = torch.load('model.pth', weights_only=False), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I would add a comment here that as noted in https://pytorch.org/docs/main/notes/serialization.html#saving-and-loading-torch-nn-modules, saving |
||
|
||
######################## | ||
# .. note:: This approach uses Python `pickle <https://docs.python.org/3/library/pickle.html>`_ module when serializing the model, thus it relies on the actual class definition to be available when loading the model. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: since this is the main landing page for save load, a comment along these lines would be great
"Setting
weights_only=True
restricts the functions that can be executed during unpickling to those required to load weights, using this flag is a best practice for loading weights."