Speaker: Hello and welcome
to the final tutorial of this section.
We are gonna make the last function of this DQN class,
which is going to be, of course, the load function.
That naturally comes after the save function.
You save your model and then you want to be able
to load it whenever you go back to the application.
So let's do this.
We're gonna make deaf, then load.
We call this last function load.
Again, this load function will take as argument self
and you probably guess what this self will be for.
It'll be exactly to load what was saved
in the save function.
So we will take self dot model
and, of course, self dot optimizer.
So this self here will be for the model and the optimizer.
So then column, and now let's load the model.
So since the model is in the last brain dot PTH file,
we want to make sure this file exists
and therefore that's what we're gonna start with.
We're gonna make an if condition to make sure
that this file exists and if it exists
we will be loading what we have in the dictionary
which is in this last brain PTH file.
So we start with an if.
Then we're gonna take our operating system
and the path leading to this last brain dot PTH file.
So OS dot path is exactly the path that leads
to the working directory folder.
So as far as I'm concerned, that's this path, desktop,
then my artificial intelligence A to Z folder,
then model one self driving car, and then the module one
self-driving car folder.
That is this folder here with the last brain dot PTH file.
And then we're gonna add dot is file.
Is file, this one.
So that's the function.
So I'm gonna add some parenthesis
and inside the parenthesis I'm going to input
the name of the file.
The name of the file that contains the model,
that is last brain dot PTH.
So we have to input it in quotes.
And so, I'm entering last brain dot PTH.
And so, if file last brain dot PTH will return true,
if the file last brain dot PTH exists
and false if it doesn't exist.
And therefore, this if condition means,
if we have the last brain PTH file
in our working directory folder,
then let's code what's gonna happen in that case.
In that case, if this file exists
well first we're gonna print something to say that,
you know, we are loading the model.
So for example, we can say a little arrow
and then loading checkpoint with three little dots.
All right, that's just to say we're loading the model.
And then, of course, we're going to load the model,
so the model and the optimizer.
And we are gonna put what we load
in a folder that I'm going to call checkpoint equals.
And that's where we're gonna use the load function
to load what was saved in the safe function.
So, of course, this is a function from the torch library.
So, torch dot and the name of this load function
is simply load,
parenthesis and inside the parenthesis, according to you
what do we need to input?
Well, very simply,
we need to input the file that contains our saved model
and our saved optimizer.
So we simply need to input the name of the file,
which is last brain dot PTH.
Last brain dot PTH and we load this file only
in the condition that this file exists.
So that's why we had to code this if condition here.
Okay, so now that we loaded the model and the optimizer,
well what we're gonna do is update separately our model
and the optimizer.
Because actually we loaded the parameters,
we loaded the weights and the parameters of the optimizer.
So now what we need to do is update our existing model,
which is this one, self dot model.
And our existing optimizer, self dot optimizer.
With the parameters, with the weights that are
in this last brain PTH file.
So we simply need to make these two updates separately.
And to do this,
we're gonna use a method from the torch modules.
So there's gonna be inheritance which will allow us
to use this method that is called load state dict.
And this load state dict method will allow us to
update all the parameters of our model and our optimizer.
So let's do this and let's start by updating our model.
So we take our model, which is self dot model.
Since self dot model inherits from the methods
of the torch module, to use the load state dict method.
So that's the method we're taking from the inheritance.
And thanks to this method,
we're going to update all the parameters
of the model, that is all the weight.
And so, what we need to input in this load state dict method
is our checkpoint variable, that is the result
of the load function.
So checkpoint, then bracket.
And now we need to enter the name
of the key, that corresponds to our model.
That is at corresponds to self dot model state dict
and that is state dict.
So in checkpoint and the bracket
we enter, in quote, state underscore dict.
And this line of code, will update your model.
That is to update the weight,
the parameters of your model.
And now we need to do the same
for the optimizer, and that's going to be almost the same.
So I'm going to copy this line.
Paste it below.
And so this time we're going to update not the model
but the optimizer, self dot optimizer.
Then again, we use the load state dict method that inherits
from the torch module method.
And we apply this function to checkpoint
of not state dict, but the key that corresponds
to the optimizer and that is optimizer.
So here we just replaced state dict by optimizer.
There we go.
Here we update the weights of the model
and here we update the parameters of the optimizer.
Perfect.
And then just to finish,
we can print a little done like that.
And finally, we just need to specify what happens
if this condition is not respected.
That is, if there is not a last brain dot PTH file.
And so we just need to add an L then column.
And simply we're just gonna say
that there is no such file last brain dot PTH.
So we're just gonna print
something like no checkpoint found
and three little dots, if you want.
All right.
And that gives us a functional, load function
and mostly a functional DQN class.
And now huge congratulations
because our artificial intelligence is ready.
You can probably hear by the sound of my voice
that I'm getting very excited
because now it's time for the demo.
We just made a brain
and we are going to put this brain in the car.
And we will see how it is clever enough
to do these round trips
between the airport and downtown, whatever the road is.
So I can't wait to show you the demo.
This is going to be in the next section.
And until then, enjoy AI.