‫In this video we are going to learn about how to save and restore a model we have created several models
‫in the past few lectures.
‫Now if we want to save the detail of our model that can be saved in a separate file on our system and
‫whenever we want to retrieve or restored our model using that same information we can do it by reading
‫that file model information basically consists of three things.
‫One is model architecture.
‫Second is model configuration.
‫And thirdly the weight 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 to a file or it's fine.
‫Both of these things stand for the same thing.
‫When you save a fight you can save it with its DFA format also or its file format.
‫Also then you save a model like this this model will have the entire information of architecture configuration
‫and the weight of this model.
‫So in the last video when we fitted our model using functional API we then this line of code and this
‫model func is now our train model.
‫If you want to store all the information that is in this model you can run this save model underscored
‫its data fine.
‫Come on and give a 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 the files that here you will see this written here.
‫This is the working directory for your session.
‫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 station.
‫So I have set this location as my working directory.
‫When I run this line of code it will create a new file I.
‫My model dot.
‫For you.
‫And this has the information of my entire neural network.
‫Now 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 city of five function.
‫So my new model will have the exactly same information that was saved in the model function.
‫So if you look at the somebody of this function
‫it has import layer to the layers when concatenation layer and one output layer.
‫Similarly a new model also has input layer to the 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 seen
‫another thing we will cover here is use of callbacks we saw when we were putting our model.
‫We gave a parameter called epochs epochs 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 it corresponding to which there
‫is a particular value of accuracy and loss.
‫Now sometimes when we have a lot of epochs our model over fits on the training data so we may want to
‫store the information at the end of all the epochs and Lauda model from in-between the epochs.
‫So for example we can run the model for 50 bucks and probably lower the weight of the model from the
‫30th epoch or the 20th 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 checkpoint.
‫In this working directory.
‫So when I run this command this has created a new variable called Checkpoint did 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 part variable is containing the information of
‫where the files will be stored and what will be the name of defined.
‫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 defined.
‫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 college bracket is basically suggesting that it is a variable containing the number of epoch and
‫the number should be indeed two digit format.
‫That is the first epoch will be the DNS epoch hyphen 0 1.
‫The second will be written as epoch hyphen 0 2 and so on.
‫Now with the part variable ready we create a new variable called CPM therefore callback which uses this
‫function callback model check.
‫And it has one important parameter.
‫This is mandatory that is the file but file part should have both things.
‫One is the directory and what is the name.
‫So that information we have stored and defined by the variable already.
‫So we'll run these lines of code also filed but well is created and CPE in the script callback variable
‫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 section that is running in 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 weights that
‫we have been previously trained with so to clear the history.
‫We remove the previously trained variable using the item function and clear decision using key underscore
‫clear underscore section function.
‫This would 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 underscores callback.
‫First we defined the architecture using gave as underscored model function.
‫Then we configured our model using the combine function
‫then we train our model using different function but when we are training our model we have to include
‫one new parameter this parameter 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 check point is what each epoch and at each epoch a file will be saved at this file.
‫But when I run a different function
‫at the end of each epoch a new file is being saved.
‫Let us go to the files tab open the check point and you can see that we have the 30 files corresponding
‫to each book.
‫Each of these files contains the information of the neural network at the end of that particular epoch.
‫So 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 forty one nine debates at this particular point are stored in the epoch
‫file 1
‫You can also check the list of files stored in the territory using the list stored files function.
‫It will list down all the files that you have in this territory and if you want to load the date in
‫a particular model.
‫So if I want to load debate in the 10th model into a new model that I'm calling as bank model only you
‫can use Load model as DFA function as we saw earlier also.
‫And we can specify the flight path where it is located.
‫So the epoch hyphen then file is located at the check point battery.
‫And when we run this a tent model variable is created and it is a model containing the information of
‫the tenth epoch.
‫So whatever the veer to it at this particular point are now assigned to the endpoint.
‫You can see these somebody of this 10th model also 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 callback had will be the same architecture assigned to paint 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 see that whatever model has the least validation
‫laws save the information of only that model and do not save the information of any of the model.
‫So when we are defining art callback with evil using callback underscore model and is called Checkpoint
‫function we can add two more parameters fine part is mandatory first parameter to be added 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 same.
‫Best only is equal to two.
‫This means that only the model with best validation loss that is the least validation loss will be seen
‫in this file with the title Best and the score model dot for you to recreate this variable first delete
‫the previous models
‫and start building a new model with defined the architecture configured it and train it
‫so it will again done for all the 30 epochs but we do it.
‫Epoch has the least validation loss which is shown by this green graph.
‫Only that will be saved.
‫So if we go back to our working directory here's our best understood model not 5.
‫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 past data since this dataset
‫has only five six observations out of which we have one or two observations at this data.
‫Instead of creating a separate validation set.
‫I'm just using the best data set as the validation dataset here.
‫If you have larger data I would recommend that you keep small amounts of data separately as a validation
‫dataset which you can use here to monitor the validation loss.
‫So whichever it 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 really 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 Dad that I need four hundred books 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 am creating that callback variable in that variable I'm adding two part one part
‫is the same model take point 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 called back at least topping.
‫In this part we tell what parameters to be monitored and how much patient 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 we will top
‫our model training then only to basically earlier when we ran our model.
‫You can see here that the validation lost is not improving much.
‫Only the training losses decreasing.
‫But even that improvement is not significant.
‫This is suggesting that we are just over on our model to prevent this awful thing.
‫We'll be using at least topping of training our model so we run this good.
‫This has created our callbacks list very well.
‫We believe the previous model we clear dictate our station.
‫Then we define the architecture of a new model 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 is no significant improvement in the validation loss parameter.
‫So again in the validation dataset I have given the test data center only callbacks is equal to callbacks
‫list within this.
‫It starts training but it stops at the fifth epoch only because after the first epoch there was some
‫improvement in the validation lost from 28 became nineteen point five.
‫But after that in the next three consecutive epochs the validation lost did not decrease.
‫So we stopped training our model.
‫I did 50 POC only 2 even though we specified that we will run this model for hundred epochs.
‫We will save the processing pain and or training of of our model because we used at least hoping this
‫model in the fifth epoch is stored in this best model at least topping out at five.
‫You can load that model using the Load model is of.
‫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 help the same model at each epoch.
‫Then we saw how to see.
‫Then we saw how to use saved best only parameter to save only one file.
‫Instead of saving separate files for each epoch.
‫And lastly we saw the use of at least copying functionality so that we can prevent what fitting and
‫excessive processing time to play in our model.
‫Thanks.