File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change 32
32
##########################
33
33
# To load model weights, you need to create an instance of the same model first, and then load the parameters
34
34
# using ``load_state_dict()`` method.
35
+ #
36
+ # In the code below, we set ``weights_only=True`` to limit the
37
+ # functions executed during unpickling to only those necessary for
38
+ # loading weights. Using ``weights_only=True`` is considered
39
+ # a best practice when loading weights.
35
40
36
41
model = models .vgg16 () # we do not specify ``weights``, i.e. create untrained model
37
42
model .load_state_dict (torch .load ('model_weights.pth' , weights_only = True ))
50
55
torch .save (model , 'model.pth' )
51
56
52
57
########################
53
- # We can then load the model like this:
58
+ # We can then load the model as demonstrated below.
59
+ #
60
+ # As described in `Saving and loading torch.nn.Modules <pytorch.org/docs/main/notes/serialization.html#saving-and-loading-torch-nn-modules>`__,
61
+ # saving ``state_dict``s is considered the best practice. However,
62
+ # below we use ``weights_only=False`` because this involves loading the
63
+ # model, which is a legacy use case for ``torch.save``.
54
64
55
65
model = torch .load ('model.pth' , weights_only = False ),
56
66
You can’t perform that action at this time.
0 commit comments