Hello and welcome to the final tutorial of the section we're going to make the last function of this
D2 in class which is going to be of course the load function that naturally comes after the same 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 going to make def then load.
We call the class function load again this load function will take as arguments self and you probably
guess where this self will be for it will be exactly to load what was saved in the same function.
So we will take self-direct morrow.
And of course solve that optimizer.
So the self-heal will be for the model and the optimizer.
So then Cullin And now let's slow the model.
So since Dymo is in the last brindled ph file we want to make sure this file exist and therefore that's
what we're going to start with.
We're going to 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 print PDA.
So we start with an if then we're going to take our operating system and the path leading to this last
brain that PVH found.
So that path is exactly the path that leads to the working directory folder.
So as far as I'm concerned that's this fast desktop than my artificial intelligence it is a folder then
model one self-driving car and then the module one self-driving car folder there is this folder here
with the last print that you found.
And then we're going to add to what is a file file this one.
So that's a function.
So I'm going to add some parenthesis.
And inside the parenthesis I'm going to input the name of the file name of the file that contains the
model that is last brain de-th.
So we have to put it in quotes.
And so I'm entering last brain that de-th and so is file last brain that page will return true if the
file that's friend that age exists and falls if it doesn't exist and therefore this if condition means
if we have the last render.
Well now working directory folder then let's go.
What's going to happen in that case.
In that case if this file exists.
Well first we're going to print something to say that you know we're loading the morals of for example
you can say a little arrow and then loading checkpoints with three little dots.
All right.
That's just to say we're loading tomorrow.
And then of course we're going to load the model.
So the moral and the optimizer and we're going to put what we load in a will that I'm going to call
checkpoint equals and that's where we're going to use the load function to load what was saved in the
same function.
So of course this is a function from the torch library to torch dot and the name of the Sloat function
is simply that 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 tomorrow and are saved optimizing
optimizer so we simply need to put the name of the file which is last bring dot de-th.
Let's bring that BGH and we load this file only in the condition that this file exists.
So that's why we had to call this condition here.
OK so now that we loaded the model and the optimizer.
Well what we're going to 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 cell block model and our existing
optimizer solved that optimizer with the parameters with the weights that are in this last Brender.
So we simply need to make these two update separately and to do this we're going to use a method from
the torture modules.
So there's going to be inheritance which will allow us to use this method that is called Load state
dict and the slowed static method will allow us to date all the parameters of our model and our optimizer.
So let's do this and let's start by updating our models.
So we take our model which is self taught model since the self-taught 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 are going to update all the parameters of the model that is all the way.
And so what we need to put in this state Dick method is our checkpoint very well.
That is the result of the load function.
So checkpoints then brackets and now we need to enter the name of the key that corresponds to our model
that is that corresponds to subduct model state date.
And that is state.
So in checkpoints and the brackets we enter in quote States underscore tickets and this line of code
will update your model.
That is a tool that awaits 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 pasted below.
And so this time we're going to date not tomorrow but the optimizer that optimizer.
Then again we use to load state Dick method that inherits from the torch module methods and we apply
this function to checkpoint of state dict.
But the key that corresponds to the optimizer and that is optimizer.
So here we just replace a date by up to Mizer.
There we go.
Here we update the weights of the.
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 lot of Pythia trial and so we just need to add an else than Colin and Cindy
we're just going to say that there is no such file.
Let's bring the PTA age.
So we're just going to print something like No check point out and three little that if you want.
All right.
And that gives us a functional load function and mostly functional.
Did you in class and now huge congratulations because our artificial intelligence is ready you can probably
hear by the sound of my voice and I'm getting very excited because now it's time for the demo.
We just made a brain and we're 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 wherever 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 I.