Is there any way of saving and loading a whole model for loading without initialization? #6780
Unanswered
JLiu-1
asked this question in
Lightning Trainer API: Trainer, LightningModule, LightningDataModule
Replies: 1 comment
-
the problem with saving the whole model and it's "structure" is that it needs to also save all the source code basically. That's how the pickling works in Python. But not everything can be pickled always. In your case, since you mention inference, I recommend using torchscript. Here is the method and also some example: Also have a look at the PyTorch docs for torchscript |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've already known that the most recommended way of saving and loading a model is saving and loading the state dict, such as:
However, this load method needs to initialize a model having the same structure and configuration as the saved one. In some circumstances, I may just need the model for inference, and would not like to first initialize the model (for convenience, or sometimes the information of the loaded model is missing). Therefore, I'd like to know that:
In pyTorch, the torch.save may meet my need, but I'm not sure how to use it in PL. For example, in the following codes:
on which can I use the torch.save? Can I just use torch.save on the trainer object? If so, can this way works for my need? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions