‫In the last couple of lectures, we learned how to train and create our model
‫One thing you have noticed is each time we opened the jupyter notebook, we started creating and
‫training our models from scratch.
‫But we don't have to do that.
‫That is a very simple way to save your model after you have trained it.
‫Keras will save both the model architecture, including the every layer put parameters and the value
‫of all the model parameters for each layer that is biases and weights
‫Value
‫So instead of training the model each time you open your jupyter notebook or initiate a new session.
‫You can just load the model that you have previously saved to get all the details of that.
‫Model
‫Now, saving model is very easy.
‫You just have to write, you are more name.
‫So in the last lecture, we created a model with functional api and object name was model.
‫So I'm saving that same object so I can write model dot save.
‫Save is the keyword.
‫And in the parenthesis, you have to write the name that you want to give to that file.
‫All this models are saved in hd f5 format. the extension of that is dot h5
‫So if you just run this, you have saved your model in your present working directory
‫So if I open my directory, this is this and you can see my model is here.
‫And the name of model is my_func underscore model is the same as I have mentioned.
‫You can see your present working directory with this.
‫Command
‫Just have to write
‫.
‫Percentage symbol and
‫Pwd it stands for present working directory.
‫So if you run this, you will get the directory location.
‫And in that directory you can find the model that you have saved.
‫Now, in case you want to change your working directory, you can do it with this keyword.
‫You have to write percentage cd and then the new directory name.
‫So as we have seen, I have this model present in my folder.
‫Now let's just delete the model variable.
‫So I have deleted the model
‫I'm also clearing the session of keras.
‫Now, again, I am loading this mortal with Lord underscore model method
‫And here also you have to just provide the name of the file.
‫And I'm saving this and another object known as model
‫Let's just track loading the model that we have recently saved.
‫Now we can call the summary method.
‫This should give me the summary that we saw earlier while creating regression neural network with functional
‫Api.
‫Just run this.
‫You can see that we have the same summary.
‫Remember, we deleted our model.
‫And then we loaded it using the save version.
‫Now summary is just the architecture of that model.
‫You can also predict using this model because it already have the optimized weights and biases value
‫So you can use it for prediction also.
‫So you can see.
‫This is the same prediction that we were getting earlier.
‫With functional api.
‫But you can save a model only after the completion of its training.
‫But usually we have a very large dataset which can take up to eight to 10 hours just to train the model
‫for that scenario.
‫We can use callbacks.
‫You can think of callbacks as check points, which will allow you to save your model after each epoch.
‫So you don't have to wait for the complete training.
‫You can save your model after each epoch as well.
‫Let's just again delete this model and.
‫Clear the session
‫I'm just taking a small example of a sequential model with two dense layer
‫And 1 output layer
‫I will using the same data.
‫Let's just compile it
‫We are using the same parameters as earlier.
‫Now, you can provide this callback parameter while training the model.
‫So earlier we were only providing training, data, epochs and validation dataset.
‫Now, along with this, we can also provide callbacks.
‫So you have to write callbacks and then here you can provide the list of callbacks that you have created.
‫This is how you create a callback.
‫So this is our variable name that is
‫Check point callback.
‫And you have to write keras. callbacks DOT model checkpoint.
‫Here I can write the file name.
‫But since.
‫We are running this model for 10 epochs.
‫That means we are generating 10 files for each epoch.
‫So if I provide a constant file names such as my kera model it will just override the file for
‫each of these 10 epochs.
‫So, at the end i would be only getting a single file of my 10 epoch
‫now to resolve this problem.
‫We can use these variables.
‫So for one epoch, I want to save my file as model dash zero one dot h5 for the second epoch.
‫I want to save it as Mordred dash
‫0 to
‫Dot h5
‫And so on.
‫Till the 10th epoch
‫So you can use such variable names in your file name also.
‫So you don't have to manually write each file name.
‫So here model is the name that I have given.
‫And this is the variable name which is kept in the curly bracket, so I can write epoch and I want
‫the two digits of each epoch.
‫So for one, I want zero one.
‫So that's why 02 d if I wanted three digits.
‫So suppose if I was running for 300 epochs I would want three digits.
‫Zero zero one or a hundred or a hundred and fifty to ninety nine.
‫In that case I would have written zero 3D.
‫So let's just cleared this checkpoint, call back again.
‫Keras.callbacks Dot model checkpoint and then the variable name and inside variable name.
‫Also, we can give variables.
‫You mentioned different epoch values.
‫Just
‫Run this.
‫And now just train the model.
‫We are using the same method model. fit training epoch, validation data.
‫And then the callback.
‫So with each of this epoch, I'm also saving a model file for that epoch.
‫So if we just go back to our directory, we should have 10 model files.
‫Here you can see we have a model dash zero one more dash 02
‫Zero to model.
‫Dash 10
‫This for First epoch.
‫Second epoch third epoch and so on till the 10th epoch.
‫If I would ever use the single file name, such as model, then at the end I would be getting only
‫single file because after each epoch
‫That file was overwrited
‫By the next epoch value
‫So remember to use the variable names of Epoch while creating a model file for Epoch.
‫Now, I can load this files similar to the previous time.
‫We just have to.
‫Write
‫Keras.model.loadmodel
‫.
‫And the file, which we want to load.
‫So just to demonstrate that, let's delete the model and clear the session.
‫So suppose if I want five of my 10th epoch, so model.10
‫So this is the model I want to export.
‫You can see i have imported that model.
‫So in future, also, if I'm opening this, you buy a notebook of that three or four days.
‫I can just write this model.
‫Keras.model.loadmodel
‫And then the model nale to load that model
‫There is absolutely no need of creating, compiling and tarining the model once again.
‫We can also use evaluate method.
‫You can see this model object is working.
‫Now there is no need of saving the model.
‫At each epoch, if you are using the regulations set during the training, you can set the safe best
‫only equal to true value, creating a checkpoint called the back.
‫It will only save your more than when its performance on the validation set is best.
‫So suppose out of this 10 epoch values.
‫We were getting the best validation is score at suppose, epoch 8
‫Then in the final 5, we will be getting the model corresponding to Epoch eight.
‫There is no need to create a separate model for all this epoch values.
‫Because generally, we want the model corresponding to the best score only
‫So let us delete the model and creating a structure.
‫And here, while creating the callback, I am also setting the parameter value of save best only
‫equal to true
‫At the end I would be getting only a single model with the name
‫Best model.
‫The model, which is corresponding to the best value of validation score.
‫So let's just create this checkpoint.
‫And then we are training the mofel, again, x train epochs validation set and then the callbacks.
‫Now, to get the best model, we can just import the best model that we have saved.
‫So if you see in the directory also
‫So we have saved this best model.h5 .
‫We are just loading and using that model.
‫To evaluate the performance on test set
‫Now one of the best method to train your model is to use early stopping.
‫What I'm referring by early stopping, is that you run your model for a very large number of epoch
‫values.
‫Suppose here I am using two hundred epochs.
‫And then I will keep an eye on my validation, set score .
‫So suppose if after 60 epochs, my validation said accuracy is not improving, then I will stop the
‫training at that point and I will be using the model with the best validation score, so far.
‫So this is an ideal situation.
‫We don't have to worry about how many epochs we want to run.
‫We just set a very high value of epoch.
‫We monitor the valuation set score and then select the best model that we have got so far.
‫So to achieve that, we will be needing and another callback, which we generally call early stopping
‫callback.
‫And we will use this callback along with our normal callback of save best only model.
‫So in early stopping callback, we have to provide the patients value.
‫Patients value is the number of epochs after which our training will be interrupted if there is no progress
‫on the validation set score.
‫So suppose after 53 epochs, we are not getting any improvement on our validation set scores.
‫Then keras will stop the training at 63 epochs
‫So the difference between this 63 and 53 is 10
‫This is the patience.
‫Thus the number of epochs keras us will wait for any improvement in validation score.
‫So earlier you saw while creating our functional model.
‫We have we have to look at our valuation score graph to check out whether our model is converging or
‫not.
‫Now, we don't have to worry about that.
‫We can early stopping along with other callbacks, which will automatically save the best model
‫that is available.
‫And it will also train our model for optimal amount of time needed to train that model to its best
‫accuracy score.
‫So let's just run this.
‫We are first creating this structure now, along with early stopping callback.
‫We have to use our the normal callbacks also to save the model.
‫And here we are also using save best only parameter as well.
‫So at the end of all this training, we would be getting on the single model file on which we would
‫be getting the best validation set score.
‫Now our second callback is early stopping callback.
‫Here you have to provide patience parameters for the number of epochs.
‫Run this.
‫Now we have to train
‫Our model, we will be passing training dataset, epochs, values.
‫Remember to increase epoch values.
‫Then the validation data and then callbacks.
‫Again, we are passing callbacks in the form of list.
‫We are first passing out checkpoint callback and then we are passing our early stopping callback.
‫Just run this.
‫So the training has stopped after 87 epoch.
‫This means that there is no need to train it, till 200 epoch values.
‫Our model has already converged and there is no way to further decrease the validation loss.
‫You can see the best value we are getting is at 77th epoch, and it waited for 10 more epochs to get
‫the better value than zero point three two.
‫And you can see up to 10 epoch values.
‫The value is still hovering over zero point three three.
‫That's why it is stopped at 87 epoch value.
‫Let's just look at our directory.
‫We should have a model with file name early stop model here
‫You can see keras has already saved the best model here with the name of early stop model.
‫So now the training has stop.
‫We can use this early stop model to predict and evaluate our future values.
‫You can see the loss on tests set we are getting is zero point two three one as compared to our previous
‫loss values of zero point four three seven, eight and so on.
‫So using early stop callback is a very good way to optimize performance of your model.
‫You don't have to worry about the number of epochs.
‫So let's have a quick recap of what we learned in this lecture.
‫First, we learn how to save and load our model tan we used callbacks to save our model at each
‫epoch value.
‫Then we learned about a parameter that is saved best only, which will save the model for which we are
‫getting the best accuracy score or lowest validation loss.
‫And after that, we saw how to use early stop callback to terminate the training if no progress on
‫validation loss is occurring.
‫So that's all for this lecture.
‫Thank you.