Skip to main content

Timeline for answer to How do I save a trained model in PyTorch? by dontloo

Current License: CC BY-SA 4.0

Post Revisions

23 events
when toggle format what by license comment
Sep 4, 2024 at 3:12 history edited Ynjxsjmh CC BY-SA 4.0
Add separator to split two methods for better reading
Nov 23, 2023 at 18:34 comment added Pam Cesar Does saving the model along with the model definition still work ?
Jul 17, 2022 at 12:39 history edited End genocide - save Gaza CC BY-SA 4.0
Formatting - made quoted portion clear.
Jul 11, 2022 at 7:45 history edited Mateen Ulhaq CC BY-SA 4.0
Reduce header.
Apr 17, 2022 at 13:56 comment added Jeff Bezos What file extension should you use with torch.save(model.state_dict(), filepath)?
Feb 15, 2022 at 11:27 comment added Antonio Sesto In the linked page, I see that the loading terminates with model.eval(): does it mean we cannot restart training ?
Feb 4, 2022 at 16:06 comment added Leo Note, that torch.save does NOT override an existing file and does so silently. Be aware when doing hyperparameter sweeps or such automated training runs ;)
Jan 31, 2022 at 0:59 history edited dontloo CC BY-SA 4.0
added 216 characters in body
Aug 9, 2021 at 15:35 comment added Franva Hi guys, could anyone tell me what is the extension for model dict file(.pth?) and the extension for the entire model file(.pkl)?? Am I correct?
Apr 24, 2021 at 20:07 comment added mah65 I saved the rnn model using second method. After loading, i want to do predictions and calculate metrics such as F1. However, prediction doesn't work. I get this error IndexError: index out of range in self when running this: predictions = model(batch.textt).squeeze(1)
Apr 10, 2021 at 23:14 comment added rayryeng @NarenBabuR that was just a dummy name. You are to replace it with the actual model class you created or whichever predefined model you used from PyTorch. The args and kwargs are whatever you used for defining the model. You can save both the weights and biases as well as the extra parameters required to load the model in. See the second answer below.
Jul 20, 2020 at 8:15 comment added Naren Babu R @dontloo the_model = TheModelClass(*args, **kwargs). Running this command says NameError: name 'TheModelClass' is not defined. How should I go about this
Jul 15, 2020 at 18:26 comment added dontloo @CharlieParker sorry no idea, haven’t worked on pytorch for too long
Jul 15, 2020 at 18:23 comment added Charlie Parker actually I only need to save a nn.Sequential model. Do you know how to do that? I don't have a model class definition.
Jun 20, 2020 at 9:12 history edited CommunityBot
Commonmark migration
Apr 9, 2020 at 14:40 comment added Mariano Kamp With that approach how do you keep track of the *args and **kwargs you need to pass in for the load case?
Dec 11, 2019 at 14:16 comment added Michael D Is there any option to save without a need for an access for model class?
Dec 16, 2018 at 14:29 comment added Gulzar the second method gives stackoverflow.com/questions/53798009/… error on windows 10. wasn't able to solve it
Jul 15, 2018 at 22:30 comment added WillZ According to @smth discuss.pytorch.org/t/saving-and-loading-a-model-in-pytorch/… model reloads to train model by default. so need to manually call the_model.eval() after loading, if you are loading it for inference, not resuming training.
Jul 19, 2017 at 5:58 history edited dontloo CC BY-SA 3.0
added 6 characters in body
May 10, 2017 at 4:30 vote accept Wasi Ahmad
May 6, 2017 at 11:00 history edited dontloo CC BY-SA 3.0
deleted 1 character in body
May 6, 2017 at 10:18 history answered dontloo CC BY-SA 3.0