-: Hello and welcome to this tutorial.
All right, so today we're gonna make a function
that will save our model.
That is that will save the brain of the core
so that we can reuse it whenever we quit the application.
Thanks to this save function,
we'll be able to save the model,
then quit the application,
and then when we go back to the application,
thanks to another function that is the load function,
which we'll make after this one,
we will be learning the last version of our model
that was trained.
So that will be very practical
and therefore, let's make these two functions,
the save function and the load function.
So let's start with the save function in this tutorial.
So here comes the new dev, then save.
And then it's gonna take one argument
that is going to be self.
And the reason is that the thing we are going
to save is not the whole model here
but our neural network self.model
and our optimizer, self.optimizer
because what we wanna save
is just the last waits
that were updated at the last iteration
because whenever we want to reuse our saved model later,
we want it to predict the action to play
with the waits that were already trained.
So we need to take this last version of the wait
and also, we need to take the last version of the optimizer
because it is connected to these waits.
So let's do this.
We have our self, so we'll be able to take our self.model
and our self.optimizer,
and we will be saving these two objects
in a Python dictionary.
And to save these two objects,
we're gonna use the save function from the Torch module,
so I'm starting here with torch.save.
And then parenthesis,
we are going to input that dictionary.
Brackets, and a dictionary in Python works like that.
You have a key, which is your identifier.
So that's unique.
And for each key, you have the value you want
to give to that key.
So it's like a mapping function from unique identifiers
to a value you want to give these identifiers.
If you take a simple dictionary book,
well, the keys would be the words,
and the values would be the definitions of the words.
Well, here that's the same.
We're gonna make two keys.
One key for the first object we wanna save,
which is self.model.
And one second key for the second thing we want to save,
that is our self.optimizer.
And therefore, let's start with the first key.
So we have to give a name to that key,
and I'm gonna call it state_dict
because then you're gonna see
that I'm gonna use the function state_dict
to save our model in the dictionary.
So that's our first key.
Then to give the value we want to attribute
for that first key, well, as you can see,
I added a little colon here
and here I'm gonna add the object,
the object I wanna save.
So the first object I wanna save is self.model.
So we can just copy this.
Self.model and paste it as the value of our first key.
Self.model.
Then we add .state_dict.
Here we go, the first one.
And then we add some parenthesis.
And that will save the parameters
of your model in this first key state_dict.
And now let's save our optimizer.
So we're gonna add a second key in the dictionary.
And to do this, we add here a comma,
then press Enter and there we go with our second key.
So the second key we're gonna call it,
well, we can call it optimizer.
Then colon, and then we just need
to add the name of the object we wanna save
and that is self.
That is our optimizer.
So we add here self.optimizer.
And then again to save the parameters
of this optimizer, we have here again .state_dict.
And there we go.
We have our model saved
with all the wait saved,
and our optimizer saved.
Perfect.
And then we will save all this into a file
and to do this, I'm going to add a second argument
to the save function,
which is going to be the name of this file
where we want to have our model
and our optimizer saved.
So remember, I showed you a quick demo
in the first section of this first module self-driving car.
That was the demo where we just had some random actions.
So that was not yet a self-driving car
but then remember, I clicked on the save button
to save the model,
and this created the last_brain.pth file,
which is the file that contains the saved version
of your model.
So I'm going to add here last_brain.pth
so that your model and your optimizer
will be saved into this created file last_brain.pth.
So you don't have it yet
but as soon as you save your model in the application,
this val will be created thanks to this code we just added.
And so now perfect,
we have a save function that will save your model,
save the brain of your core
by saving the waits and the optimizer
of the neural network that is in fact the brain of the car.
So perfect, we now have only one function to create left.
That's the load function.
And that's because the save function never goes
without a load function.
There's no purpose saving your model
if you cannot load what you save afterwards.
So that's the last step in our journey
before the exciting demo,
and we will make this load function in the last tutorial
of this section.
So I'll see you in this next tutorial
and until then, enjoy AI.