In this lecture we are going to answer one of the most common questions I get.
That is once we've trained the model how do we save it for later use.
For example after creating a model in Google collab I might want to copy that model into my production
server so that other services in my company can make use of that model that I just trained.
This lecture will show you how to save a model to a file and then load it back at a later time.
In addition we'll see how to download the saved model from Google Colette
first.
Note that this is a continuation of a notebook we were previously looking at.
As usual you can determine what notebook we are currently looking at using the title of the notebook.
So if you left this notebook previously you may want to reopen it.
At this point so you can follow along.
Let's start with the central objects that we need for this process.
The state dict as you can tell by its name.
It's a dictionary.
If you look at what we get when we call model that state dict we can see that it's not just any dictionary
but rather an ordered dictionary.
That kind of makes sense since if you're iterating through the dictionary to populate the weights of
a neural network you would want to do that in the order that your model expects as you can see it's
nothing but the parameters of the model since we just have one layer in our model.
The first element is the weight matrix and the second element is the bias vector.
The next step is to save the model
we can accomplish that in just one line of code torch dot save for the first argument we pass in the
model's state dict and for the second argument we pass in a file path I've just passed in my model dot
Petey so the file my model that Petey will be saved to the current directory you can see that after
saving the model when I do an L S the file I just saved appears as expected the next step is to load
and evaluate the model to ensure that we saved it correctly
in order to do this we first have to recreate the model the same way it was created originally.
That might seem strange since we still have to replicate the code we had earlier.
This will probably make more sense to you.
Once you've seen more complex models which will involve defining our own classes in those cases you
would just instantiate an object of that class which is one line of code which would be more compact
than this.
In any case passing code around is very lightweight because code files are just text anyway.
We'll call this model too so as not to interfere with our original model.
Next we call model to load state debt and parse in the state dict that we just saved we get that by
calling torture out load and passing in the file path where our model file is located.
You can see that if your load is successful it will say all keys match successfully
at this point.
We would like to know is our reloaded model the same as the model we saved.
Luckily this is an easy check and we already know how to write this code.
We just take our code from earlier to compute the accuracy of the model on the train set and test sets.
But this time we'll use a model 2 instead of the original model.
As you can see we get precisely the same accuracy as we did earlier.
At this point you might be wondering.
So I just saved my model.
It's in my local directory but it's not my local directory it's my Google drives local directory.
What if my teammate asked me for this model.
How can I share it.
And that is the next question we are going to discuss.
In fact this is quite easy and it's another reason I really like collab.
So over in left here.
You may have noticed this sidebar with some icons.
If you click the folder icon you'll see all the files in your current directory including my model that
Petey the model file we just saved so let's right click that and now you can see that there's this option
to download and of course that's how you can download this file to your local harddrive
another option is to use Python code to download the file.
Now I've heard that this only works for Chrome but of course that may change in the future.
We can use the same module we used earlier for uploading files so we import files from the Google Docs
code that module.
Then we say files that download and pass in the file path when we run the cell.
The file is downloaded by our browser as usual
all right.
So that works as expected.