Saving and loading a PyTorch model
One of the important aspects of working on neural network models is to save and load back a model after training. Think of a scenario where you have to make inferences from an already-trained model. You would load the trained model instead of training it again.
The following code can be found in the save_and_load_pytorch_model.ipynb
file in the Chapter02
folder of this book’s GitHub repository at https://bit.ly/mcvp-2e.
Before going through the relevant commands to do that, taking the preceding example as our case, let’s understand what the important components that completely define a neural network are. We need the following:
- A unique name (key) for each tensor (parameter)
- The logic to connect every tensor in the network with one or the other
- The values (weight/bias values) of each tensor
While the first point is taken care of during the __init__
phase of a definition, the second...