<jupyter_start><jupyter_text>Copyright 2019. Created by <NAME>.  Saving and Loading Trained ModelsRefer back to this notebook as a refresher on saving and loading models.  Saving a trained modelSave a trained model to a file in case you want to come back later and feed new data through it.To save a trained model called "model" to a file called "MyModel.pt":<jupyter_code>torch.save(model.state_dict(), 'MyModel.pt')<jupyter_output><empty_output><jupyter_text>To ensure the model has been trained before saving (assumes the variables "losses" and "epochs" have been defined):<jupyter_code>if len(losses) == epochs:
    torch.save(model.state_dict(), 'MyModel.pt')
else:
    print('Model has not been trained. Consider loading a trained model instead.')<jupyter_output><empty_output><jupyter_text>Loading a saved model (starting from scratch)We can load the trained weights and biases from a saved model. If we've just opened the notebook, we'll have to run standard imports and function definitions.  1. Perform standard importsThese will depend on the scope of the model, chosen displays, metrics, etc.<jupyter_code># Perform standard imports
import torch
import torch.nn as nn
import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>2. Run the model definitionWe'll introduce the model shown below in the next section.<jupyter_code>class MultilayerPerceptron(nn.Module):
    def __init__(self, in_sz=784, out_sz=10, layers=[120,84]):
        super().__init__()
        self.fc1 = nn.Linear(in_sz,layers[0])
        self.fc2 = nn.Linear(layers[0],layers[1])
        self.fc3 = nn.Linear(layers[1],out_sz)
    
    def forward(self,X):
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        X = self.fc3(X)
        return F.log_softmax(X, dim=1)<jupyter_output><empty_output><jupyter_text>3. Instantiate the model, load parametersFirst we instantiate the model, then we load the pre-trained weights & biases, and finally we set the model to "eval" mode to prevent any further backprops.<jupyter_code>model2 = MultilayerPerceptron()
model2.load_state_dict(torch.load('MyModel.pt'));
model2.eval() # be sure to run this step!<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09604005461993628, 'fraction_numerical': 0.014110150204824761, 'mean_word_length': 4.9245283018867925, 'pattern_counts': {'":': 1, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '224929', 'license': 'MIT', 'n_tokens_mistral': 705, 'n_tokens_neox': 682, 'n_words': 232, 'path': 'coursework/02-ANN-Artificial-Neural-Networks/07-Recap-Saving-and-Loading-Trained-Models-NO_VIDEO.ipynb', 'repo_name': 'saint1729/deep-learning-pytorch', 'size': '2212'}