WEBVTT

00:00.510 --> 00:01.500
Speaker: Hello and welcome

00:01.500 --> 00:03.630
to the final tutorial of this section.

00:03.630 --> 00:07.410
We are gonna make the last function of this DQN class,

00:07.410 --> 00:10.140
which is going to be, of course, the load function.

00:10.140 --> 00:12.540
That naturally comes after the save function.

00:12.540 --> 00:14.670
You save your model and then you want to be able

00:14.670 --> 00:17.460
to load it whenever you go back to the application.

00:17.460 --> 00:18.900
So let's do this.

00:18.900 --> 00:22.590
We're gonna make deaf, then load.

00:22.590 --> 00:24.480
We call this last function load.

00:24.480 --> 00:28.470
Again, this load function will take as argument self

00:28.470 --> 00:31.140
and you probably guess what this self will be for.

00:31.140 --> 00:33.840
It'll be exactly to load what was saved

00:33.840 --> 00:35.190
in the save function.

00:35.190 --> 00:37.200
So we will take self dot model

00:37.200 --> 00:39.510
and, of course, self dot optimizer.

00:39.510 --> 00:42.990
So this self here will be for the model and the optimizer.

00:42.990 --> 00:46.890
So then column, and now let's load the model.

00:46.890 --> 00:50.580
So since the model is in the last brain dot PTH file,

00:50.580 --> 00:52.710
we want to make sure this file exists

00:52.710 --> 00:54.630
and therefore that's what we're gonna start with.

00:54.630 --> 00:57.090
We're gonna make an if condition to make sure

00:57.090 --> 00:59.490
that this file exists and if it exists

00:59.490 --> 01:02.610
we will be loading what we have in the dictionary

01:02.610 --> 01:05.490
which is in this last brain PTH file.

01:05.490 --> 01:07.200
So we start with an if.

01:07.200 --> 01:10.290
Then we're gonna take our operating system

01:10.290 --> 01:15.150
and the path leading to this last brain dot PTH file.

01:15.150 --> 01:18.180
So OS dot path is exactly the path that leads

01:18.180 --> 01:19.800
to the working directory folder.

01:19.800 --> 01:23.610
So as far as I'm concerned, that's this path, desktop,

01:23.610 --> 01:26.310
then my artificial intelligence A to Z folder,

01:26.310 --> 01:29.490
then model one self driving car, and then the module one

01:29.490 --> 01:30.960
self-driving car folder.

01:30.960 --> 01:34.170
That is this folder here with the last brain dot PTH file.

01:34.170 --> 01:38.100
And then we're gonna add dot is file.

01:38.100 --> 01:39.840
Is file, this one.

01:39.840 --> 01:41.040
So that's the function.

01:41.040 --> 01:42.810
So I'm gonna add some parenthesis

01:42.810 --> 01:45.150
and inside the parenthesis I'm going to input

01:45.150 --> 01:46.560
the name of the file.

01:46.560 --> 01:48.780
The name of the file that contains the model,

01:48.780 --> 01:50.850
that is last brain dot PTH.

01:50.850 --> 01:53.850
So we have to input it in quotes.

01:53.850 --> 01:58.830
And so, I'm entering last brain dot PTH.

01:58.830 --> 02:03.690
And so, if file last brain dot PTH will return true,

02:03.690 --> 02:06.660
if the file last brain dot PTH exists

02:06.660 --> 02:08.640
and false if it doesn't exist.

02:08.640 --> 02:10.980
And therefore, this if condition means,

02:10.980 --> 02:14.220
if we have the last brain PTH file

02:14.220 --> 02:16.350
in our working directory folder,

02:16.350 --> 02:19.890
then let's code what's gonna happen in that case.

02:19.890 --> 02:22.620
In that case, if this file exists

02:22.620 --> 02:26.280
well first we're gonna print something to say that,

02:26.280 --> 02:27.510
you know, we are loading the model.

02:27.510 --> 02:30.360
So for example, we can say a little arrow

02:30.360 --> 02:35.360
and then loading checkpoint with three little dots.

02:35.970 --> 02:38.580
All right, that's just to say we're loading the model.

02:38.580 --> 02:41.070
And then, of course, we're going to load the model,

02:41.070 --> 02:43.500
so the model and the optimizer.

02:43.500 --> 02:45.090
And we are gonna put what we load

02:45.090 --> 02:49.830
in a folder that I'm going to call checkpoint equals.

02:49.830 --> 02:52.080
And that's where we're gonna use the load function

02:52.080 --> 02:55.140
to load what was saved in the safe function.

02:55.140 --> 02:59.220
So, of course, this is a function from the torch library.

02:59.220 --> 03:02.520
So, torch dot and the name of this load function

03:02.520 --> 03:04.680
is simply load,

03:04.680 --> 03:07.680
parenthesis and inside the parenthesis, according to you

03:07.680 --> 03:09.150
what do we need to input?

03:09.150 --> 03:10.440
Well, very simply,

03:10.440 --> 03:14.340
we need to input the file that contains our saved model

03:14.340 --> 03:16.170
and our saved optimizer.

03:16.170 --> 03:18.510
So we simply need to input the name of the file,

03:18.510 --> 03:23.073
which is last brain dot PTH.

03:24.360 --> 03:27.810
Last brain dot PTH and we load this file only

03:27.810 --> 03:30.270
in the condition that this file exists.

03:30.270 --> 03:33.870
So that's why we had to code this if condition here.

03:33.870 --> 03:36.690
Okay, so now that we loaded the model and the optimizer,

03:36.690 --> 03:41.310
well what we're gonna do is update separately our model

03:41.310 --> 03:42.540
and the optimizer.

03:42.540 --> 03:44.250
Because actually we loaded the parameters,

03:44.250 --> 03:48.150
we loaded the weights and the parameters of the optimizer.

03:48.150 --> 03:52.800
So now what we need to do is update our existing model,

03:52.800 --> 03:55.590
which is this one, self dot model.

03:55.590 --> 03:59.160
And our existing optimizer, self dot optimizer.

03:59.160 --> 04:02.190
With the parameters, with the weights that are

04:02.190 --> 04:05.160
in this last brain PTH file.

04:05.160 --> 04:08.160
So we simply need to make these two updates separately.

04:08.160 --> 04:08.993
And to do this,

04:08.993 --> 04:12.150
we're gonna use a method from the torch modules.

04:12.150 --> 04:14.760
So there's gonna be inheritance which will allow us

04:14.760 --> 04:18.810
to use this method that is called load state dict.

04:18.810 --> 04:20.940
And this load state dict method will allow us to

04:20.940 --> 04:25.470
update all the parameters of our model and our optimizer.

04:25.470 --> 04:28.650
So let's do this and let's start by updating our model.

04:28.650 --> 04:32.820
So we take our model, which is self dot model.

04:32.820 --> 04:35.820
Since self dot model inherits from the methods

04:35.820 --> 04:40.650
of the torch module, to use the load state dict method.

04:40.650 --> 04:43.350
So that's the method we're taking from the inheritance.

04:43.350 --> 04:44.490
And thanks to this method,

04:44.490 --> 04:46.770
we're going to update all the parameters

04:46.770 --> 04:48.780
of the model, that is all the weight.

04:48.780 --> 04:51.870
And so, what we need to input in this load state dict method

04:51.870 --> 04:54.930
is our checkpoint variable, that is the result

04:54.930 --> 04:56.280
of the load function.

04:56.280 --> 04:59.640
So checkpoint, then bracket.

04:59.640 --> 05:00.840
And now we need to enter the name

05:00.840 --> 05:04.440
of the key, that corresponds to our model.

05:04.440 --> 05:07.530
That is at corresponds to self dot model state dict

05:07.530 --> 05:10.170
and that is state dict.

05:10.170 --> 05:11.880
So in checkpoint and the bracket

05:11.880 --> 05:16.860
we enter, in quote, state underscore dict.

05:16.860 --> 05:20.190
And this line of code, will update your model.

05:20.190 --> 05:21.630
That is to update the weight,

05:21.630 --> 05:23.400
the parameters of your model.

05:23.400 --> 05:24.780
And now we need to do the same

05:24.780 --> 05:28.440
for the optimizer, and that's going to be almost the same.

05:28.440 --> 05:30.453
So I'm going to copy this line.

05:32.190 --> 05:33.420
Paste it below.

05:33.420 --> 05:36.390
And so this time we're going to update not the model

05:36.390 --> 05:40.620
but the optimizer, self dot optimizer.

05:40.620 --> 05:45.150
Then again, we use the load state dict method that inherits

05:45.150 --> 05:47.190
from the torch module method.

05:47.190 --> 05:50.220
And we apply this function to checkpoint

05:50.220 --> 05:53.970
of not state dict, but the key that corresponds

05:53.970 --> 05:57.150
to the optimizer and that is optimizer.

05:57.150 --> 06:00.807
So here we just replaced state dict by optimizer.

06:02.250 --> 06:03.150
There we go.

06:03.150 --> 06:04.890
Here we update the weights of the model

06:04.890 --> 06:08.340
and here we update the parameters of the optimizer.

06:08.340 --> 06:09.173
Perfect.

06:09.173 --> 06:11.160
And then just to finish,

06:11.160 --> 06:15.840
we can print a little done like that.

06:15.840 --> 06:18.810
And finally, we just need to specify what happens

06:18.810 --> 06:21.510
if this condition is not respected.

06:21.510 --> 06:24.720
That is, if there is not a last brain dot PTH file.

06:24.720 --> 06:28.083
And so we just need to add an L then column.

06:29.160 --> 06:30.720
And simply we're just gonna say

06:30.720 --> 06:34.170
that there is no such file last brain dot PTH.

06:34.170 --> 06:35.440
So we're just gonna print

06:36.480 --> 06:41.480
something like no checkpoint found

06:41.520 --> 06:43.270
and three little dots, if you want.

06:44.340 --> 06:45.210
All right.

06:45.210 --> 06:48.240
And that gives us a functional, load function

06:48.240 --> 06:51.960
and mostly a functional DQN class.

06:51.960 --> 06:54.150
And now huge congratulations

06:54.150 --> 06:57.120
because our artificial intelligence is ready.

06:57.120 --> 06:59.070
You can probably hear by the sound of my voice

06:59.070 --> 07:00.510
that I'm getting very excited

07:00.510 --> 07:02.310
because now it's time for the demo.

07:02.310 --> 07:03.870
We just made a brain

07:03.870 --> 07:06.720
and we are going to put this brain in the car.

07:06.720 --> 07:08.820
And we will see how it is clever enough

07:08.820 --> 07:10.290
to do these round trips

07:10.290 --> 07:13.500
between the airport and downtown, whatever the road is.

07:13.500 --> 07:15.390
So I can't wait to show you the demo.

07:15.390 --> 07:17.490
This is going to be in the next section.

07:17.490 --> 07:19.353
And until then, enjoy AI.
