﻿1
00:00:01,020 --> 00:00:07,500
‫In the last couple of lectures, we learned how to train and create our model

2
00:00:09,090 --> 00:00:17,370
‫One thing you have noticed is each time we opened the jupyter notebook, we started creating and

3
00:00:17,370 --> 00:00:20,100
‫training our models from scratch.

4
00:00:21,500 --> 00:00:23,480
‫But we don't have to do that.

5
00:00:24,540 --> 00:00:29,410
‫That is a very simple way to save your model after you have trained it.

6
00:00:30,810 --> 00:00:38,760
‫Keras will save both the model architecture, including the every layer put parameters and the value

7
00:00:38,760 --> 00:00:45,000
‫of all the model parameters for each layer that is biases and weights

8
00:00:45,040 --> 00:00:45,420
‫Value

9
00:00:47,880 --> 00:00:54,690
‫So instead  of training the model each time you open your jupyter notebook or initiate a new session.

10
00:00:55,260 --> 00:01:02,890
‫You can just load the model that you have previously saved to get all the details of that.

11
00:01:02,910 --> 00:01:03,270
‫Model

12
00:01:05,440 --> 00:01:07,630
‫Now, saving model is very easy.

13
00:01:07,990 --> 00:01:10,070
‫You just have to write, you are more name.

14
00:01:11,370 --> 00:01:18,360
‫So in the last lecture, we created a model with functional api and object name was model.

15
00:01:18,630 --> 00:01:24,320
‫So I'm saving that same object so I can write model dot save.

16
00:01:24,390 --> 00:01:25,370
‫Save is the keyword.

17
00:01:26,100 --> 00:01:31,800
‫And in the parenthesis, you have to write the name that you want to give to that file.

18
00:01:33,180 --> 00:01:40,710
‫All this models are saved in hd f5 format. the extension of that is dot h5

19
00:01:42,660 --> 00:01:50,590
‫So if you just run this, you have saved your model in your present working directory

20
00:01:52,590 --> 00:02:03,140
‫So if I open my directory, this is this and you can see my model is here.

21
00:02:03,570 --> 00:02:10,860
‫And the name of model is my_func underscore model is the same as I have mentioned.

22
00:02:12,480 --> 00:02:15,360
‫You can see your present working directory with this.

23
00:02:15,360 --> 00:02:15,750
‫Command

24
00:02:16,220 --> 00:02:16,750
‫Just have to write

25
00:02:16,770 --> 00:02:17,020
‫.

26
00:02:18,700 --> 00:02:20,150
‫Percentage symbol and

27
00:02:20,450 --> 00:02:24,030
‫Pwd it stands for present working directory.

28
00:02:24,440 --> 00:02:27,600
‫So if you run this, you will get the directory location.

29
00:02:28,500 --> 00:02:32,730
‫And in that directory you can find the model that you have saved.

30
00:02:34,440 --> 00:02:41,200
‫Now, in case you want to change your working directory, you can do it with this keyword.

31
00:02:41,820 --> 00:02:46,470
‫You have to write percentage cd and then the new directory name.

32
00:02:48,180 --> 00:02:52,740
‫So as we have seen, I have this model present in my folder.

33
00:02:53,550 --> 00:02:57,000
‫Now let's just delete the model variable.

34
00:03:00,430 --> 00:03:02,930
‫So I have deleted the model

35
00:03:03,620 --> 00:03:05,870
‫I'm also clearing the session of keras.

36
00:03:08,860 --> 00:03:15,100
‫Now, again, I am loading this mortal with Lord underscore model method

37
00:03:16,450 --> 00:03:19,390
‫And here also you have to just provide the name of the file.

38
00:03:20,530 --> 00:03:24,640
‫And I'm saving this and another object known as model

39
00:03:25,680 --> 00:03:29,620
‫Let's just track loading the model that we have recently saved.

40
00:03:32,100 --> 00:03:33,850
‫Now we can call the summary method.

41
00:03:34,120 --> 00:03:41,840
‫This should give me the summary that we saw earlier while creating regression neural network with functional

42
00:03:41,840 --> 00:03:42,260
‫Api.

43
00:03:42,690 --> 00:03:44,990
‫Just run this.

44
00:03:47,110 --> 00:03:50,770
‫You can see that we have the same summary.

45
00:03:52,360 --> 00:03:54,170
‫Remember, we deleted our model.

46
00:03:55,060 --> 00:03:58,450
‫And then we loaded it using the save version.

47
00:03:59,980 --> 00:04:02,820
‫Now summary is just the architecture of that model.

48
00:04:03,910 --> 00:04:12,320
‫You can also predict using this model because it already have the optimized weights and biases value

49
00:04:13,630 --> 00:04:16,020
‫So you can use it for prediction also.

50
00:04:16,020 --> 00:04:17,320
‫So you can see.

51
00:04:17,380 --> 00:04:21,400
‫This is the same prediction that we were getting earlier.

52
00:04:21,490 --> 00:04:22,780
‫With functional api.

53
00:04:25,240 --> 00:04:30,940
‫But you can save a model only after the completion of its training.

54
00:04:33,110 --> 00:04:43,490
‫But usually we have a very large dataset which can take up to eight to 10 hours just to train the model

55
00:04:44,120 --> 00:04:45,500
‫for that scenario.

56
00:04:45,890 --> 00:04:47,420
‫We can use callbacks.

57
00:04:50,330 --> 00:04:58,820
‫You can think of callbacks as check points, which will allow you to save your model after each epoch.

58
00:05:00,530 --> 00:05:03,380
‫So you don't have to wait for the complete training.

59
00:05:04,130 --> 00:05:07,850
‫You can save your model after each epoch as well.

60
00:05:09,800 --> 00:05:14,020
‫Let's just again delete this model and.

61
00:05:16,010 --> 00:05:18,350
‫Clear the session

62
00:05:18,580 --> 00:05:22,750
‫I'm just taking a small example of a sequential model with two dense layer

63
00:05:23,230 --> 00:05:24,550
‫And 1 output layer

64
00:05:24,790 --> 00:05:26,260
‫I will using the same data.

65
00:05:27,280 --> 00:05:28,850
‫Let's just compile it

66
00:05:29,260 --> 00:05:31,860
‫We are using the same parameters as earlier.

67
00:05:34,040 --> 00:05:40,010
‫Now, you can provide this callback parameter while training the model.

68
00:05:40,510 --> 00:05:45,460
‫So earlier we were only providing training, data, epochs and validation dataset.

69
00:05:46,420 --> 00:05:50,590
‫Now, along with this, we can also provide callbacks.

70
00:05:51,550 --> 00:05:58,750
‫So you have to write callbacks and then here you can provide the list of callbacks that you have created.

71
00:06:01,150 --> 00:06:02,860
‫This is how you create a callback.

72
00:06:03,370 --> 00:06:05,350
‫So this is our variable name that is

73
00:06:05,430 --> 00:06:06,730
‫Check point callback.

74
00:06:07,890 --> 00:06:12,820
‫And you have to write keras. callbacks DOT model checkpoint.

75
00:06:14,770 --> 00:06:16,590
‫Here I can write the file name.

76
00:06:18,310 --> 00:06:19,060
‫But since.

77
00:06:20,350 --> 00:06:22,970
‫We are running this model for 10 epochs.

78
00:06:23,150 --> 00:06:27,500
‫That means we are generating 10 files for each epoch.

79
00:06:30,170 --> 00:06:38,210
‫So if I provide a constant file names such as my kera model it will just override the file for

80
00:06:38,240 --> 00:06:39,890
‫each of these 10 epochs.

81
00:06:40,960 --> 00:06:46,600
‫So, at the end i would be only getting a single file of my 10 epoch

82
00:06:49,210 --> 00:06:50,980
‫now to resolve this problem.

83
00:06:51,670 --> 00:06:53,860
‫We can use these variables.

84
00:06:54,550 --> 00:07:05,460
‫So for one epoch, I want to save my file as model dash zero one dot h5 for the second epoch.

85
00:07:05,890 --> 00:07:08,150
‫I want to save it as Mordred dash

86
00:07:08,230 --> 00:07:08,920
‫0 to

87
00:07:09,190 --> 00:07:09,710
‫Dot h5

88
00:07:10,360 --> 00:07:11,140
‫And so on.

89
00:07:11,220 --> 00:07:12,310
‫Till the 10th epoch

90
00:07:13,150 --> 00:07:17,380
‫So you can use such variable names in your file name also.

91
00:07:18,040 --> 00:07:21,850
‫So you don't have to manually write each file name.

92
00:07:24,870 --> 00:07:28,590
‫So here model is the name that I have given.

93
00:07:28,770 --> 00:07:36,160
‫And this is the variable name which is kept in the curly bracket, so I can write epoch and I want

94
00:07:36,160 --> 00:07:38,070
‫the two digits of each epoch.

95
00:07:38,310 --> 00:07:40,170
‫So for one, I want zero one.

96
00:07:40,470 --> 00:07:44,750
‫So that's why 02 d if I wanted three digits.

97
00:07:44,790 --> 00:07:50,880
‫So suppose if I was running for 300 epochs I would want three digits.

98
00:07:50,910 --> 00:07:55,750
‫Zero zero one or a hundred or a hundred and fifty to ninety nine.

99
00:07:56,280 --> 00:07:59,480
‫In that case I would have written zero 3D.

100
00:08:02,380 --> 00:08:06,740
‫So let's just cleared this checkpoint, call back again.

101
00:08:07,000 --> 00:08:12,210
‫Keras.callbacks Dot model checkpoint and then the variable name and inside variable name.

102
00:08:12,220 --> 00:08:14,410
‫Also, we can give variables.

103
00:08:15,560 --> 00:08:17,730
‫You mentioned different epoch values.

104
00:08:18,260 --> 00:08:18,520
‫Just

105
00:08:18,570 --> 00:08:19,200
‫Run this.

106
00:08:20,260 --> 00:08:22,180
‫And now just train the model.

107
00:08:22,500 --> 00:08:28,240
‫We are using the same method model. fit training epoch, validation data.

108
00:08:28,410 --> 00:08:30,090
‫And then the callback.

109
00:08:35,580 --> 00:08:40,890
‫So with each of this epoch, I'm also saving a model file for that epoch.

110
00:08:43,320 --> 00:08:50,310
‫So if we just go back to our directory, we should have 10 model files.

111
00:08:50,640 --> 00:08:54,570
‫Here you can see we have a model dash zero one more dash 02

112
00:08:54,570 --> 00:08:56,430
‫Zero to model.

113
00:08:56,720 --> 00:08:57,660
‫Dash 10

114
00:08:58,990 --> 00:09:00,150
‫This for First epoch.

115
00:09:00,260 --> 00:09:03,680
‫Second epoch third epoch and so on till the 10th epoch.

116
00:09:04,660 --> 00:09:13,330
‫If I would ever use the single file name, such as model, then at the end I would be getting only

117
00:09:13,330 --> 00:09:15,710
‫single file because after each epoch

118
00:09:15,760 --> 00:09:17,420
‫That file was overwrited

119
00:09:17,450 --> 00:09:19,030
‫By the next epoch value

120
00:09:20,530 --> 00:09:25,760
‫So remember to use the variable names of Epoch while creating a model file for Epoch.

121
00:09:27,400 --> 00:09:34,210
‫Now, I can load this files similar to the previous time.

122
00:09:34,640 --> 00:09:35,620
‫We just have to.

123
00:09:35,620 --> 00:09:36,020
‫Write

124
00:09:36,440 --> 00:09:37,680
‫Keras.model.loadmodel

125
00:09:37,790 --> 00:09:38,250
‫.

126
00:09:38,590 --> 00:09:41,290
‫And the file, which we want to load.

127
00:09:43,120 --> 00:09:50,740
‫So just to demonstrate that, let's delete the model and clear the session.

128
00:09:50,890 --> 00:09:57,620
‫So suppose if I want five of my 10th epoch, so model.10

129
00:10:02,290 --> 00:10:04,780
‫So this is the model I want to export.

130
00:10:09,330 --> 00:10:11,850
‫You can see i have imported that model.

131
00:10:12,990 --> 00:10:17,700
‫So in future, also, if I'm opening this, you buy a notebook of that three or four days.

132
00:10:18,000 --> 00:10:19,920
‫I can just write this model.

133
00:10:20,400 --> 00:10:22,440
‫Keras.model.loadmodel

134
00:10:22,590 --> 00:10:25,230
‫And then the model nale to load that model

135
00:10:25,620 --> 00:10:32,580
‫There is absolutely no need of creating, compiling and tarining the model once again.

136
00:10:33,690 --> 00:10:35,470
‫We can also use evaluate method.

137
00:10:36,390 --> 00:10:39,660
‫You can see this model object is working.

138
00:10:41,250 --> 00:10:45,330
‫Now there is no need of saving the model.

139
00:10:45,630 --> 00:10:55,200
‫At each epoch, if you are using the regulations set during the training, you can set the safe best

140
00:10:55,230 --> 00:10:58,980
‫only equal to true value, creating a checkpoint called the back.

141
00:11:01,770 --> 00:11:07,450
‫It will only save your more than when its performance on the validation set is best.

142
00:11:10,210 --> 00:11:13,870
‫So suppose out of this 10 epoch values.

143
00:11:15,070 --> 00:11:20,440
‫We were getting the best validation is score at suppose, epoch 8

144
00:11:21,400 --> 00:11:26,520
‫Then in the final 5, we will be getting the model corresponding to Epoch eight.

145
00:11:27,100 --> 00:11:32,260
‫There is no need to create a separate model for all this epoch values.

146
00:11:34,590 --> 00:11:39,150
‫Because generally, we want the model corresponding to the best score only

147
00:11:42,940 --> 00:11:47,030
‫So let us delete the model and creating a structure.

148
00:11:47,740 --> 00:11:56,440
‫And here, while creating the callback, I am also setting the parameter value of save best only

149
00:11:56,650 --> 00:11:57,560
‫equal to true

150
00:11:58,310 --> 00:12:03,640
‫At the end I would be getting only a single model  with the name

151
00:12:03,760 --> 00:12:04,480
‫Best model.

152
00:12:05,550 --> 00:12:09,900
‫The model, which is corresponding to the best value of validation score.

153
00:12:12,570 --> 00:12:15,760
‫So let's just create this checkpoint.

154
00:12:16,140 --> 00:12:24,360
‫And then we are training the mofel, again, x train epochs validation set and then the callbacks.

155
00:12:36,030 --> 00:12:41,160
‫Now, to get the best model, we can just import the best model that we have saved.

156
00:12:41,820 --> 00:12:43,770
‫So if you see in the directory also

157
00:12:43,800 --> 00:12:47,720
‫So we have saved this best model.h5 .

158
00:12:51,510 --> 00:12:53,950
‫We are just loading and using that model.

159
00:12:54,270 --> 00:12:56,550
‫To evaluate the performance on test set

160
00:13:00,360 --> 00:13:05,950
‫Now one of the best method to train your model is to use early stopping.

161
00:13:07,470 --> 00:13:14,280
‫What I'm referring by early stopping, is that you run your model for a very large number of epoch

162
00:13:14,280 --> 00:13:14,790
‫values.

163
00:13:15,240 --> 00:13:17,910
‫Suppose here I am using two hundred epochs.

164
00:13:19,190 --> 00:13:23,900
‫And then I will keep an eye on my validation, set score .

165
00:13:25,060 --> 00:13:33,820
‫So suppose if after 60 epochs, my validation said accuracy is not improving, then I will stop the

166
00:13:33,820 --> 00:13:40,760
‫training at that point and I will be using the model with the best validation score, so far.

167
00:13:41,620 --> 00:13:43,330
‫So this is an ideal situation.

168
00:13:43,570 --> 00:13:46,840
‫We don't have to worry about how many epochs we want to run.

169
00:13:47,500 --> 00:13:49,750
‫We just set a very high value of epoch.

170
00:13:50,320 --> 00:13:56,990
‫We monitor the valuation set score  and then select the best model that we have got so far.

171
00:13:59,360 --> 00:14:06,980
‫So to achieve that, we will be needing and another callback, which we generally call early  stopping

172
00:14:06,980 --> 00:14:07,440
‫callback.

173
00:14:08,390 --> 00:14:16,190
‫And we will use this callback along with our normal callback of save best only model.

174
00:14:18,170 --> 00:14:22,310
‫So in early stopping callback, we have to provide the patients value.

175
00:14:23,240 --> 00:14:31,730
‫Patients value is the number of epochs after which our training will be interrupted if there is no progress

176
00:14:31,760 --> 00:14:34,160
‫on the validation set score.

177
00:14:36,410 --> 00:14:44,210
‫So suppose after 53 epochs, we are not getting any improvement on our validation set scores.

178
00:14:44,870 --> 00:14:49,510
‫Then keras will stop the training at 63 epochs

179
00:14:51,020 --> 00:14:54,420
‫So the difference between this 63  and 53 is 10

180
00:14:54,930 --> 00:14:55,950
‫This is the patience.

181
00:14:56,450 --> 00:15:02,380
‫Thus the number of epochs keras us will wait for any improvement in validation score.

182
00:15:04,520 --> 00:15:08,150
‫So earlier you saw while creating our functional model.

183
00:15:08,180 --> 00:15:14,990
‫We have we have to look at our valuation score graph to check out whether our model is converging or

184
00:15:14,990 --> 00:15:15,380
‫not.

185
00:15:16,100 --> 00:15:18,070
‫Now, we don't have to worry about that.

186
00:15:18,500 --> 00:15:27,110
‫We can early stopping along with other callbacks, which will automatically save the best model

187
00:15:27,140 --> 00:15:28,110
‫that is available.

188
00:15:28,970 --> 00:15:36,950
‫And it will also train our model for optimal amount of time needed to train that model to its best

189
00:15:36,950 --> 00:15:37,880
‫accuracy score.

190
00:15:39,740 --> 00:15:42,680
‫So let's just run this.

191
00:15:42,860 --> 00:15:49,510
‫We are first creating this structure now, along with early stopping callback.

192
00:15:49,550 --> 00:15:52,940
‫We have to use our the normal callbacks also to save the model.

193
00:15:54,380 --> 00:15:59,960
‫And here we are also using save best only parameter as well.

194
00:16:01,760 --> 00:16:09,020
‫So at the end of all this training, we would be getting on the single model file on which we would

195
00:16:09,020 --> 00:16:11,850
‫be getting the best validation set score.

196
00:16:13,550 --> 00:16:16,960
‫Now our second callback is early stopping callback.

197
00:16:17,480 --> 00:16:23,740
‫Here you have to provide patience parameters for the number of epochs.

198
00:16:25,780 --> 00:16:26,360
‫Run this.

199
00:16:27,710 --> 00:16:28,610
‫Now we have to train

200
00:16:28,640 --> 00:16:33,020
‫Our model, we will be passing training dataset, epochs, values.

201
00:16:34,040 --> 00:16:36,710
‫Remember to increase epoch values.

202
00:16:38,630 --> 00:16:42,150
‫Then the validation data and then callbacks.

203
00:16:43,070 --> 00:16:46,100
‫Again, we are passing callbacks in the form of list.

204
00:16:46,280 --> 00:16:52,250
‫We are first passing out checkpoint callback and then we are passing our early stopping callback.

205
00:16:52,880 --> 00:16:53,900
‫Just run this.

206
00:17:21,250 --> 00:17:25,860
‫So the training has stopped after 87 epoch.

207
00:17:27,690 --> 00:17:33,150
‫This means that there is no need to train it, till 200 epoch values.

208
00:17:34,950 --> 00:17:40,890
‫Our model has already converged and there is no way to further decrease the validation loss.

209
00:17:41,970 --> 00:17:51,270
‫You can see the best value we are getting is at 77th epoch, and it waited for 10 more epochs to get

210
00:17:51,700 --> 00:17:53,840
‫the better value than zero point three two.

211
00:17:54,990 --> 00:17:57,250
‫And you can see up to 10 epoch values.

212
00:17:58,590 --> 00:18:02,550
‫The value is still hovering over zero point three three.

213
00:18:02,820 --> 00:18:06,290
‫That's why it is stopped at 87 epoch value.

214
00:18:08,150 --> 00:18:10,360
‫Let's just look at our directory.

215
00:18:11,330 --> 00:18:17,580
‫We should have a model with file name early stop model here

216
00:18:18,500 --> 00:18:26,990
‫You can see keras has already saved the best model here with the name of early stop model.

217
00:18:28,240 --> 00:18:29,860
‫So now the training has stop.

218
00:18:30,080 --> 00:18:35,900
‫We can use this early stop model to predict and evaluate our future values.

219
00:18:37,550 --> 00:18:45,710
‫You can see the loss on tests set we are getting is zero point two three one as compared to our previous

220
00:18:45,710 --> 00:18:49,820
‫loss values of zero point four three seven, eight and so on.

221
00:18:51,830 --> 00:18:58,820
‫So using early stop callback is a very good way to optimize performance of your model.

222
00:18:59,150 --> 00:19:02,570
‫You don't have to worry about the number of epochs.

223
00:19:05,330 --> 00:19:09,200
‫So let's have a quick recap of what we learned in this lecture.

224
00:19:09,680 --> 00:19:19,890
‫First, we learn how to save and load our model tan we used callbacks to save our model at each

225
00:19:19,890 --> 00:19:20,600
‫epoch value.

226
00:19:21,890 --> 00:19:28,970
‫Then we learned about a parameter that is saved best only, which will save the model for which we are

227
00:19:28,970 --> 00:19:32,870
‫getting the best accuracy score or lowest validation loss.

228
00:19:34,010 --> 00:19:43,550
‫And after that, we saw how to use early stop callback to terminate the training if no progress on

229
00:19:43,640 --> 00:19:46,070
‫validation loss is occurring.

230
00:19:46,940 --> 00:19:48,440
‫So that's all for this lecture.

231
00:19:48,860 --> 00:19:49,310
‫Thank you.

