1
00:00:11,690 --> 00:00:15,590
In this lecture we are going to answer one of the most common questions I get.

2
00:00:15,830 --> 00:00:19,910
That is once we've trained the model how do we save it for later use.

3
00:00:19,910 --> 00:00:25,850
For example after creating a model in Google collab I might want to copy that model into my production

4
00:00:25,850 --> 00:00:32,020
server so that other services in my company can make use of that model that I just trained.

5
00:00:32,120 --> 00:00:37,540
This lecture will show you how to save a model to a file and then load it back at a later time.

6
00:00:37,550 --> 00:00:41,630
In addition we'll see how to download the saved model from Google Colette

7
00:00:47,190 --> 00:00:47,760
first.

8
00:00:47,760 --> 00:00:51,970
Note that this is a continuation of a notebook we were previously looking at.

9
00:00:52,050 --> 00:00:57,690
As usual you can determine what notebook we are currently looking at using the title of the notebook.

10
00:00:58,050 --> 00:01:01,560
So if you left this notebook previously you may want to reopen it.

11
00:01:01,560 --> 00:01:05,660
At this point so you can follow along.

12
00:01:05,830 --> 00:01:08,700
Let's start with the central objects that we need for this process.

13
00:01:08,710 --> 00:01:11,790
The state dict as you can tell by its name.

14
00:01:11,830 --> 00:01:13,240
It's a dictionary.

15
00:01:13,570 --> 00:01:18,940
If you look at what we get when we call model that state dict we can see that it's not just any dictionary

16
00:01:19,090 --> 00:01:21,260
but rather an ordered dictionary.

17
00:01:21,400 --> 00:01:25,780
That kind of makes sense since if you're iterating through the dictionary to populate the weights of

18
00:01:25,780 --> 00:01:32,380
a neural network you would want to do that in the order that your model expects as you can see it's

19
00:01:32,380 --> 00:01:37,300
nothing but the parameters of the model since we just have one layer in our model.

20
00:01:37,300 --> 00:01:43,650
The first element is the weight matrix and the second element is the bias vector.

21
00:01:43,810 --> 00:01:45,430
The next step is to save the model

22
00:01:50,420 --> 00:01:56,120
we can accomplish that in just one line of code torch dot save for the first argument we pass in the

23
00:01:56,120 --> 00:02:02,590
model's state dict and for the second argument we pass in a file path I've just passed in my model dot

24
00:02:02,710 --> 00:02:09,470
Petey so the file my model that Petey will be saved to the current directory you can see that after

25
00:02:09,470 --> 00:02:17,570
saving the model when I do an L S the file I just saved appears as expected the next step is to load

26
00:02:17,600 --> 00:02:20,600
and evaluate the model to ensure that we saved it correctly

27
00:02:25,110 --> 00:02:30,480
in order to do this we first have to recreate the model the same way it was created originally.

28
00:02:30,480 --> 00:02:34,500
That might seem strange since we still have to replicate the code we had earlier.

29
00:02:34,800 --> 00:02:36,700
This will probably make more sense to you.

30
00:02:36,840 --> 00:02:42,390
Once you've seen more complex models which will involve defining our own classes in those cases you

31
00:02:42,390 --> 00:02:47,730
would just instantiate an object of that class which is one line of code which would be more compact

32
00:02:47,730 --> 00:02:48,820
than this.

33
00:02:48,900 --> 00:02:54,400
In any case passing code around is very lightweight because code files are just text anyway.

34
00:02:54,410 --> 00:02:59,050
We'll call this model too so as not to interfere with our original model.

35
00:02:59,070 --> 00:03:06,250
Next we call model to load state debt and parse in the state dict that we just saved we get that by

36
00:03:06,250 --> 00:03:11,960
calling torture out load and passing in the file path where our model file is located.

37
00:03:12,010 --> 00:03:16,360
You can see that if your load is successful it will say all keys match successfully

38
00:03:21,410 --> 00:03:22,070
at this point.

39
00:03:22,070 --> 00:03:26,960
We would like to know is our reloaded model the same as the model we saved.

40
00:03:26,960 --> 00:03:31,040
Luckily this is an easy check and we already know how to write this code.

41
00:03:31,040 --> 00:03:36,500
We just take our code from earlier to compute the accuracy of the model on the train set and test sets.

42
00:03:36,560 --> 00:03:40,750
But this time we'll use a model 2 instead of the original model.

43
00:03:41,000 --> 00:03:45,070
As you can see we get precisely the same accuracy as we did earlier.

44
00:03:53,070 --> 00:03:54,810
At this point you might be wondering.

45
00:03:54,810 --> 00:03:56,380
So I just saved my model.

46
00:03:56,400 --> 00:04:02,430
It's in my local directory but it's not my local directory it's my Google drives local directory.

47
00:04:02,460 --> 00:04:04,540
What if my teammate asked me for this model.

48
00:04:04,560 --> 00:04:05,400
How can I share it.

49
00:04:06,060 --> 00:04:08,990
And that is the next question we are going to discuss.

50
00:04:09,060 --> 00:04:13,040
In fact this is quite easy and it's another reason I really like collab.

51
00:04:13,380 --> 00:04:14,950
So over in left here.

52
00:04:15,000 --> 00:04:18,180
You may have noticed this sidebar with some icons.

53
00:04:18,420 --> 00:04:24,120
If you click the folder icon you'll see all the files in your current directory including my model that

54
00:04:24,130 --> 00:04:32,890
Petey the model file we just saved so let's right click that and now you can see that there's this option

55
00:04:32,890 --> 00:04:38,020
to download and of course that's how you can download this file to your local harddrive

56
00:04:45,680 --> 00:04:49,650
another option is to use Python code to download the file.

57
00:04:49,700 --> 00:04:54,020
Now I've heard that this only works for Chrome but of course that may change in the future.

58
00:04:54,350 --> 00:05:00,300
We can use the same module we used earlier for uploading files so we import files from the Google Docs

59
00:05:00,300 --> 00:05:01,460
code that module.

60
00:05:01,670 --> 00:05:06,940
Then we say files that download and pass in the file path when we run the cell.

61
00:05:06,950 --> 00:05:09,260
The file is downloaded by our browser as usual

62
00:05:14,720 --> 00:05:15,030
all right.

63
00:05:15,040 --> 00:05:16,440
So that works as expected.
