WEBVTT

00:00.420 --> 00:02.490
-: Hello and welcome to this tutorial.

00:02.490 --> 00:04.320
All right, so today we're gonna make a function

00:04.320 --> 00:05.940
that will save our model.

00:05.940 --> 00:07.890
That is that will save the brain of the core

00:07.890 --> 00:11.430
so that we can reuse it whenever we quit the application.

00:11.430 --> 00:12.690
Thanks to this save function,

00:12.690 --> 00:14.400
we'll be able to save the model,

00:14.400 --> 00:15.960
then quit the application,

00:15.960 --> 00:18.090
and then when we go back to the application,

00:18.090 --> 00:20.430
thanks to another function that is the load function,

00:20.430 --> 00:22.200
which we'll make after this one,

00:22.200 --> 00:24.750
we will be learning the last version of our model

00:24.750 --> 00:25.800
that was trained.

00:25.800 --> 00:27.450
So that will be very practical

00:27.450 --> 00:29.850
and therefore, let's make these two functions,

00:29.850 --> 00:32.160
the save function and the load function.

00:32.160 --> 00:35.490
So let's start with the save function in this tutorial.

00:35.490 --> 00:38.730
So here comes the new dev, then save.

00:38.730 --> 00:41.070
And then it's gonna take one argument

00:41.070 --> 00:43.080
that is going to be self.

00:43.080 --> 00:45.270
And the reason is that the thing we are going

00:45.270 --> 00:48.150
to save is not the whole model here

00:48.150 --> 00:51.390
but our neural network self.model

00:51.390 --> 00:54.690
and our optimizer, self.optimizer

00:54.690 --> 00:56.100
because what we wanna save

00:56.100 --> 00:58.350
is just the last waits

00:58.350 --> 01:00.510
that were updated at the last iteration

01:00.510 --> 01:03.960
because whenever we want to reuse our saved model later,

01:03.960 --> 01:06.090
we want it to predict the action to play

01:06.090 --> 01:09.120
with the waits that were already trained.

01:09.120 --> 01:11.490
So we need to take this last version of the wait

01:11.490 --> 01:14.160
and also, we need to take the last version of the optimizer

01:14.160 --> 01:16.830
because it is connected to these waits.

01:16.830 --> 01:17.940
So let's do this.

01:17.940 --> 01:21.417
We have our self, so we'll be able to take our self.model

01:21.417 --> 01:23.640
and our self.optimizer,

01:23.640 --> 01:25.680
and we will be saving these two objects

01:25.680 --> 01:27.180
in a Python dictionary.

01:27.180 --> 01:28.770
And to save these two objects,

01:28.770 --> 01:32.430
we're gonna use the save function from the Torch module,

01:32.430 --> 01:36.570
so I'm starting here with torch.save.

01:36.570 --> 01:37.830
And then parenthesis,

01:37.830 --> 01:39.960
we are going to input that dictionary.

01:39.960 --> 01:42.720
Brackets, and a dictionary in Python works like that.

01:42.720 --> 01:45.630
You have a key, which is your identifier.

01:45.630 --> 01:46.830
So that's unique.

01:46.830 --> 01:48.750
And for each key, you have the value you want

01:48.750 --> 01:49.860
to give to that key.

01:49.860 --> 01:53.520
So it's like a mapping function from unique identifiers

01:53.520 --> 01:56.880
to a value you want to give these identifiers.

01:56.880 --> 01:59.520
If you take a simple dictionary book,

01:59.520 --> 02:01.830
well, the keys would be the words,

02:01.830 --> 02:04.830
and the values would be the definitions of the words.

02:04.830 --> 02:06.240
Well, here that's the same.

02:06.240 --> 02:07.890
We're gonna make two keys.

02:07.890 --> 02:10.620
One key for the first object we wanna save,

02:10.620 --> 02:12.480
which is self.model.

02:12.480 --> 02:15.060
And one second key for the second thing we want to save,

02:15.060 --> 02:17.850
that is our self.optimizer.

02:17.850 --> 02:20.400
And therefore, let's start with the first key.

02:20.400 --> 02:22.290
So we have to give a name to that key,

02:22.290 --> 02:25.060
and I'm gonna call it state_dict

02:26.070 --> 02:26.903
because then you're gonna see

02:26.903 --> 02:29.520
that I'm gonna use the function state_dict

02:29.520 --> 02:31.710
to save our model in the dictionary.

02:31.710 --> 02:33.300
So that's our first key.

02:33.300 --> 02:36.510
Then to give the value we want to attribute

02:36.510 --> 02:39.150
for that first key, well, as you can see,

02:39.150 --> 02:41.400
I added a little colon here

02:41.400 --> 02:43.440
and here I'm gonna add the object,

02:43.440 --> 02:44.970
the object I wanna save.

02:44.970 --> 02:48.690
So the first object I wanna save is self.model.

02:48.690 --> 02:50.850
So we can just copy this.

02:50.850 --> 02:55.620
Self.model and paste it as the value of our first key.

02:55.620 --> 02:56.850
Self.model.

02:56.850 --> 03:00.450
Then we add .state_dict.

03:00.450 --> 03:02.160
Here we go, the first one.

03:02.160 --> 03:04.140
And then we add some parenthesis.

03:04.140 --> 03:06.270
And that will save the parameters

03:06.270 --> 03:09.930
of your model in this first key state_dict.

03:09.930 --> 03:12.330
And now let's save our optimizer.

03:12.330 --> 03:15.480
So we're gonna add a second key in the dictionary.

03:15.480 --> 03:17.610
And to do this, we add here a comma,

03:17.610 --> 03:21.240
then press Enter and there we go with our second key.

03:21.240 --> 03:23.160
So the second key we're gonna call it,

03:23.160 --> 03:26.160
well, we can call it optimizer.

03:26.160 --> 03:28.500
Then colon, and then we just need

03:28.500 --> 03:31.230
to add the name of the object we wanna save

03:31.230 --> 03:33.360
and that is self.

03:33.360 --> 03:34.770
That is our optimizer.

03:34.770 --> 03:37.317
So we add here self.optimizer.

03:39.180 --> 03:40.680
And then again to save the parameters

03:40.680 --> 03:44.083
of this optimizer, we have here again .state_dict.

03:45.180 --> 03:46.013
And there we go.

03:46.013 --> 03:48.330
We have our model saved

03:48.330 --> 03:49.650
with all the wait saved,

03:49.650 --> 03:52.080
and our optimizer saved.

03:52.080 --> 03:53.160
Perfect.

03:53.160 --> 03:56.490
And then we will save all this into a file

03:56.490 --> 03:59.040
and to do this, I'm going to add a second argument

03:59.040 --> 04:00.660
to the save function,

04:00.660 --> 04:04.200
which is going to be the name of this file

04:04.200 --> 04:05.787
where we want to have our model

04:05.787 --> 04:07.650
and our optimizer saved.

04:07.650 --> 04:09.720
So remember, I showed you a quick demo

04:09.720 --> 04:13.380
in the first section of this first module self-driving car.

04:13.380 --> 04:16.590
That was the demo where we just had some random actions.

04:16.590 --> 04:18.750
So that was not yet a self-driving car

04:18.750 --> 04:20.850
but then remember, I clicked on the save button

04:20.850 --> 04:22.410
to save the model,

04:22.410 --> 04:26.160
and this created the last_brain.pth file,

04:26.160 --> 04:28.530
which is the file that contains the saved version

04:28.530 --> 04:29.520
of your model.

04:29.520 --> 04:34.520
So I'm going to add here last_brain.pth

04:36.180 --> 04:38.970
so that your model and your optimizer

04:38.970 --> 04:43.470
will be saved into this created file last_brain.pth.

04:43.470 --> 04:44.820
So you don't have it yet

04:44.820 --> 04:47.250
but as soon as you save your model in the application,

04:47.250 --> 04:51.303
this val will be created thanks to this code we just added.

04:52.230 --> 04:53.820
And so now perfect,

04:53.820 --> 04:56.550
we have a save function that will save your model,

04:56.550 --> 04:57.720
save the brain of your core

04:57.720 --> 05:00.510
by saving the waits and the optimizer

05:00.510 --> 05:03.930
of the neural network that is in fact the brain of the car.

05:03.930 --> 05:08.220
So perfect, we now have only one function to create left.

05:08.220 --> 05:09.870
That's the load function.

05:09.870 --> 05:11.850
And that's because the save function never goes

05:11.850 --> 05:13.560
without a load function.

05:13.560 --> 05:15.480
There's no purpose saving your model

05:15.480 --> 05:17.820
if you cannot load what you save afterwards.

05:17.820 --> 05:19.290
So that's the last step in our journey

05:19.290 --> 05:20.940
before the exciting demo,

05:20.940 --> 05:24.090
and we will make this load function in the last tutorial

05:24.090 --> 05:25.170
of this section.

05:25.170 --> 05:26.880
So I'll see you in this next tutorial

05:26.880 --> 05:28.713
and until then, enjoy AI.
