In this video, we are going to learn about how to save and restore our model.
We have created several models in the past few lectures.
Now, if you want to see the detail for model that can be saved in a separate file on our system.
And whenever we want to retrieve or restore our model using that saved information, we can do it by
reading that filing model information.
Basically consists of three things.
One is model architecture.
Second model configuration.
And thirdly, DVDs of the train model.
So when we save a model, all this information will be stored in a file.
The file has a format called its D.F. File or its file.
Both of these things stand for the same thing.
When you say, well, fine, you can save it with its DFI format, Dolto or HFA format.
Also, then you save a model like this.
This model will have the entire information of architecture, configuration and Digweed of this model.
So in the last video, when we fitted our model using functional EPA, we then this line of code.
And this model func is now a train model.
If you want to store all the information that is in this model, you can run this save model and just
put its DFI.
Come on.
And given name to the fight.
This will be the name of the file with which it will be stored in your working directory.
So if you go to these files that here you will see this Driton here.
This is the working directory for your obsession.
If you want to change the working directory, you just click here, select wherever you want to go,
open that particular location.
And then you go into the more options and click on Settings Working Directory.
This will set that particular address as your working directory for our decision.
So I have said this location as my working directory
when I ran this line of code.
It will create a new file title.
My model dot.
It's for you.
And this has the information of my entire neural network.
No, whenever I want to create a new model containing the same architecture and the same weight, I
can load all the information from this file using the Lord underscore model underscored a deep faith
function.
So my new model will have the exactly same information that was saved in the model function.
Also, if you look at the summary of dysfunction.
It has input layer to the layers when concatenation layer and one output layer.
Similarly, a new model also has input led to hidden layers.
When concatenation layer and one output layer, if you check the performance of both of these model
on new data, it will also be exactly same.
Another thing we will cover here is use of callbacks we saw when we were putting our model.
We gave a barometer called Epochs Epoch suggested the number of times our model went through the entire
training dataset.
Now, at the end of each epoch, we saw that our model has a set value of offbeat corresponding to which
there is a particular value of accuracy and loss.
Now, sometimes when we have a lot of epochs, our model overfit on the training data.
So we may want to store the information at the end of all the epochs and Lawder model from in-between
the e-books.
So, for example, we can run the model for the P box and probably load the weight of the model from
the 30th book or the 28 epoch.
This can be achieved using callbacks.
Let me show you how in these first few lines I am creating a directory where this file will be saved.
So basically I am creating a new folder called Check Point in this working directory.
So when I run this command, this has created a new variable called Checkpoint Day containing the name
Checkpoint.
And when I run directory create command, it will create a new folder in my working directly with the
name Checkpoint.
Now I am creating a variable called file.
But this file but variable is containing the information of where the files will be stored and what
will be the name of the fight.
So file but function has two parameters.
The first parameter is the location of the folder.
We want to store defined and the second is the name of divides.
Now, since we will be running the model for multiple epochs such as 30 or 50 bucks, each file should
have a different name.
Otherwise, the same file will be overwritten multiple times to have a different name for each file.
We can use something like this.
So the name of the file that I am suggesting is name will be Epoch Hyphen.
And this part, which is in the curly bracket, is basically suggesting that it is a variable containing
the number of epoch and the number should be in the two digit format.
That is the first epoch will be the DNS epoch hyphen zero one.
The second will be written as Epoch hyphen zero two and so on.
Now, with the part variable ready, we create a new variable called S.P. Underscore Callback, which
uses dysfunction callback model checkpoint.
And it has one important parameter.
This is mandatory that is defined but but should have both things.
One is the directory and what is the name.
So that information we have stored in file but variable already.
So we run these lines of code.
Also filed by Terry Mills, creator and S.P. Underscore Callback Wytheville is also created.
Now before we create a new model on the same data, because the model information is stored in model
func variable plus all the information is also stored in the data session that is running the background.
We can clear that information.
It will have two helpful impact.
One is it will clear the memory that is being used in our system so our system will perform better.
Secondly, when we train our new model, it will start afresh and it will not start with the weight
that we have been previously trained with.
So to clear the history, we removed the previously trained variable using the item function and we
clear decision using gay underscore, clear and association function.
This will clear all the background information that we have stored in the Get US library.
Now we'll be training a new model.
This model I'm calling as model underscore callback.
First, we define the architecture, using it as underscored moral function, then we configured our
model using the combined function.
Then we train our model using different function.
But when we are training our model, we have to include one new parameter.
This barometer is called callbacks.
And in this, we have to specify the callback variable, which contains the information of when that
file is to be created and where it is to be created.
So this checkpoint is for each epoch and at each epoch a file will be saved at this file.
But when I run different function.
At the end of each epoch, a new file is being saved.
Let us go to the files tab, open the checkpoint and you can see that we have here 30 files corresponding
to each book.
Each of these files contains the information of the neural network.
At the end of that particular epoch to at the end of First Epoch, our model was giving a loss of twenty
three point forty five with a mean absolute error of three point forward, one nine debates.
At this particular point are stored in the Epoch File one.
You can also check the list of files stored in the territory using the list DOT files function, it
will list down all defiance that you have in this territory.
And if you want to load the lead in a particular model.
So if I want to load debate in the 10th model into a new model that I'm calling as 10th model, only
you can use load model as DFI function, as we saw earlier also.
And we can specify the flight path where it is located.
So the Epoch hyphen 10 file is located at the checkpoint barratry.
And when we done this tent model variable is created.
And it is a model containing the information of the 10th Epoch.
So whatever the were at this particular point are now assigned to the Pentagon.
You can see the summary of this 10th model, although the architecture will be automatically assigned
as the same architecture that we define for the model on which we were fitting the training data to
whatever architecture that model called back hard will be the same architecture assigned to any model.
There is one important feature of this callback process that we can use, instead of saving all the
30 files, we can save only one file, which has the best model.
And how do we judge that, which is the best model?
We can say that whichever model has the least validation laws saved the information of only that model
and do not save the information of any other model.
So when we are defining our backward evil using callback, just go model and record check point function,
we can add two more parameters.
Five out is mandatory first parameter.
Do we add it is what values to be monitored.
Since this is a regression problem, we'll be looking at validation loss.
If it would have been a classification problem, we would be monitoring the accuracy.
And the second parameter is still best only is equal to two.
This means that only the model with best validation loss.
That is the least validation loss will be saved in this file with the title best.
And just got a model dot edge for you to recreate this variable first.
Delete the previous models
and start building our new model.
We define the architecture, configure it and frame it.
So it will again done for all Dieter D-Box.
But whichever Epoch has the least validation loss which is shown by this green graph here.
Only that will be seen.
So if we go back to a working directory, here's our best underscored model dot HFA.
This contains the information of the model with least validation loss.
You may also notice here that instead of validation data, I'm just using best data.
Since this dataset has only 506 observations out of which we have 102 observations at test data instead
of creating a separate validation set.
I'm just using deep test dataset as the validation dataset here.
If you have larger data, I would recommend that you keep a small amount of data separately as a validation
dataset, which you can use here to monitor the validation loss.
So whichever is the best model is stored in this file.
You can load all the information of this model and do your new best model variable.
And here you have this model.
You can use this model for predicting in the future.
The last thing I'm going to discuss in this lecture is the feature of a release stopping in callbacks.
It is a very important feature.
The basic concept is instead of running the model for all the epochs, we can specify a large value
for the epochs randy model and stop it.
When we do not see much improvement in the accuracy.
So basically, instead of running it for only 30, we tell that one in four hundred D-Box.
But stop at that point of time when you stop seeing any improvement in a particular metric that we have
specified.
For example, here I'm creating that callback variable in that variable.
I'm adding two parts.
One part is the same model checkpoint part, which contains the name of defined in which the information
will be stored.
What parameter is to be monitored and whether to save only the best model or save all the models.
The other part is callback, at least stopping.
In this part, we tell what parameters to be monitored and how much patience do we have.
That is, for how many epochs will we see whether there is an improvement or not.
If there is no improvement for three consecutive epochs in the validation, lost parameter will top
our model training.
There are only two basically earlier when we ran our model.
You can see here that the validation loss is not improving much on liddick training losses decreasing.
But even that improvement is not significant.
This is suggesting that we are just overfitting on our model to prevent this.
What we're doing, we'll be using at least stopping of training our model.
So we run this code.
This has created our callbacks list variable.
We believed the previous model.
We cleared the gate gas station.
Then we redefined the architecture of our new model and we configured it.
And now when we are training our model, we specify a large number of epochs because we know that their
training will automatically stop when there's no significant improvement in the validation loss.
But I will do so again in the validation data set.
I have given the test data set.
Only callbacks is equal to callbacks list.
We've done this.
It starts trading, but it stops at the fifth epoch only because after the first epoch, there was some
improvement in the validation loss from twenty became nineteen point five.
But after that, in the next three consecutive epochs, deregulation laws did not decrease.
So we stopped blaming our model.
I did 50 Bocconi, too, even though we specified that we will run this model.
Four hundred epochs.
We were saved the processing pain and or draining all of our model because we used at least dumping
this model in the fifth epoch is stored.
And this best model of stopping or it's five, you can load that model using the load model is DFI function.
Well, that's all in this lecture.
We saw how to see all the information of a model into a separate file.
The benefit of this is you can share this file with your colleagues, with your student or anyone in
your office.
And whenever someone loads a model from that file, that model will have the exact same architecture
and debate that your model had.
Then we saw the importance of using callbacks.
Callbacks helped us save model at each epoch.
Then we saw how to save.
Then we saw how to use saved best only parameter to save only one file.
Instead of saving separate files for each book.
And lastly, we saw the use of at least orbing functionality so that we can prevent overfitting and
excessive processing time to bring our model.
Thanks.