﻿1
00:00:01,050 --> 00:00:09,570
‫In this video we are going to learn about how to save and restore a model we have created several models

2
00:00:09,810 --> 00:00:12,460
‫in the past few lectures.

3
00:00:12,470 --> 00:00:20,220
‫Now if we want to save the detail of our model that can be saved in a separate file on our system and

4
00:00:20,220 --> 00:00:27,810
‫whenever we want to retrieve or restored our model using that same information we can do it by reading

5
00:00:27,810 --> 00:00:34,240
‫that file model information basically consists of three things.

6
00:00:34,320 --> 00:00:36,450
‫One is model architecture.

7
00:00:36,450 --> 00:00:38,390
‫Second is model configuration.

8
00:00:38,460 --> 00:00:41,430
‫And thirdly the weight of the train model.

9
00:00:44,340 --> 00:00:51,720
‫So when we save a model all this information will be stored in a file.

10
00:00:51,720 --> 00:00:56,980
‫The file has a format called to a file or it's fine.

11
00:00:57,910 --> 00:01:00,720
‫Both of these things stand for the same thing.

12
00:01:00,720 --> 00:01:07,410
‫When you save a fight you can save it with its DFA format also or its file format.

13
00:01:07,410 --> 00:01:16,020
‫Also then you save a model like this this model will have the entire information of architecture configuration

14
00:01:16,200 --> 00:01:18,360
‫and the weight of this model.

15
00:01:20,550 --> 00:01:31,440
‫So in the last video when we fitted our model using functional API we then this line of code and this

16
00:01:31,500 --> 00:01:34,100
‫model func is now our train model.

17
00:01:35,190 --> 00:01:43,430
‫If you want to store all the information that is in this model you can run this save model underscored

18
00:01:43,540 --> 00:01:44,630
‫its data fine.

19
00:01:44,630 --> 00:01:47,570
‫Come on and give a name to the fight.

20
00:01:47,910 --> 00:01:54,890
‫This will be the name of the file with which it will be stored in your working directory.

21
00:01:55,050 --> 00:02:01,910
‫So if you go to the files that here you will see this written here.

22
00:02:01,950 --> 00:02:05,470
‫This is the working directory for your session.

23
00:02:05,760 --> 00:02:13,920
‫If you want to change the working directory you just click here select wherever you want to go open

24
00:02:13,920 --> 00:02:22,730
‫that particular location and then you go into the more options and click on settings working directory.

25
00:02:22,770 --> 00:02:27,440
‫This will set that particular address as your working directory for our station.

26
00:02:30,450 --> 00:02:35,570
‫So I have set this location as my working directory.

27
00:02:36,600 --> 00:02:42,780
‫When I run this line of code it will create a new file I.

28
00:02:42,930 --> 00:02:44,150
‫My model dot.

29
00:02:44,220 --> 00:02:44,630
‫For you.

30
00:02:45,420 --> 00:02:53,490
‫And this has the information of my entire neural network.

31
00:02:53,490 --> 00:03:00,930
‫Now whenever I want to create a new model containing the same architecture and the same weight I can

32
00:03:00,930 --> 00:03:08,580
‫load all the information from this file using the Lord underscore model underscored city of five function.

33
00:03:08,610 --> 00:03:16,320
‫So my new model will have the exactly same information that was saved in the model function.

34
00:03:17,460 --> 00:03:20,790
‫So if you look at the somebody of this function

35
00:03:25,550 --> 00:03:31,730
‫it has import layer to the layers when concatenation layer and one output layer.

36
00:03:33,110 --> 00:03:44,360
‫Similarly a new model also has input layer to the layers when concatenation layer and one output layer.

37
00:03:44,750 --> 00:03:50,030
‫If you check the performance of both of these model on new data it will also be exactly seen

38
00:03:54,080 --> 00:04:01,990
‫another thing we will cover here is use of callbacks we saw when we were putting our model.

39
00:04:02,210 --> 00:04:09,890
‫We gave a parameter called epochs epochs suggested the number of times our model went through the entire

40
00:04:09,890 --> 00:04:12,290
‫training dataset.

41
00:04:12,410 --> 00:04:22,160
‫Now at the end of each epoch we saw that our model has a set value of it corresponding to which there

42
00:04:22,160 --> 00:04:28,460
‫is a particular value of accuracy and loss.

43
00:04:28,460 --> 00:04:38,180
‫Now sometimes when we have a lot of epochs our model over fits on the training data so we may want to

44
00:04:38,350 --> 00:04:45,650
‫store the information at the end of all the epochs and Lauda model from in-between the epochs.

45
00:04:45,650 --> 00:04:53,120
‫So for example we can run the model for 50 bucks and probably lower the weight of the model from the

46
00:04:53,120 --> 00:04:57,710
‫30th epoch or the 20th epoch.

47
00:04:57,710 --> 00:05:02,830
‫This can be achieved using callbacks.

48
00:05:02,960 --> 00:05:10,670
‫Let me show you how in these first few lines I am creating a directory where this file will be saved.

49
00:05:10,670 --> 00:05:15,230
‫So basically I am creating a new folder called checkpoint.

50
00:05:15,560 --> 00:05:18,130
‫In this working directory.

51
00:05:18,320 --> 00:05:27,560
‫So when I run this command this has created a new variable called Checkpoint did containing the name

52
00:05:27,620 --> 00:05:28,770
‫checkpoint.

53
00:05:29,210 --> 00:05:37,730
‫And when I run directory create command it will create a new folder in my working directly with the

54
00:05:37,730 --> 00:05:39,980
‫name checkpoint.

55
00:05:39,980 --> 00:05:48,170
‫Now I am creating a variable called file but this file part variable is containing the information of

56
00:05:48,620 --> 00:05:54,600
‫where the files will be stored and what will be the name of defined.

57
00:05:54,920 --> 00:05:58,300
‫So file but function has two parameters.

58
00:05:58,370 --> 00:06:04,930
‫The first parameter is the location of the folder we want to store defined.

59
00:06:05,000 --> 00:06:08,320
‫And the second is the name of defined.

60
00:06:08,660 --> 00:06:19,430
‫Now since we will be running the model for multiple epochs such as 30 or 50 bucks each file should have

61
00:06:19,430 --> 00:06:20,380
‫a different name.

62
00:06:20,410 --> 00:06:28,970
‫Otherwise the same file will be overwritten multiple times to have a different name for each file we

63
00:06:28,970 --> 00:06:33,210
‫can use something like this.

64
00:06:33,320 --> 00:06:42,460
‫So the name of the file that I am suggesting is name will be epoch hyphen and this part which is in

65
00:06:42,460 --> 00:06:50,630
‫the college bracket is basically suggesting that it is a variable containing the number of epoch and

66
00:06:50,630 --> 00:06:53,990
‫the number should be indeed two digit format.

67
00:06:53,990 --> 00:07:00,200
‫That is the first epoch will be the DNS epoch hyphen 0 1.

68
00:07:00,200 --> 00:07:05,550
‫The second will be written as epoch hyphen 0 2 and so on.

69
00:07:05,580 --> 00:07:13,430
‫Now with the part variable ready we create a new variable called CPM therefore callback which uses this

70
00:07:13,430 --> 00:07:16,790
‫function callback model check.

71
00:07:17,210 --> 00:07:20,340
‫And it has one important parameter.

72
00:07:20,480 --> 00:07:26,420
‫This is mandatory that is the file but file part should have both things.

73
00:07:26,420 --> 00:07:29,470
‫One is the directory and what is the name.

74
00:07:29,480 --> 00:07:34,830
‫So that information we have stored and defined by the variable already.

75
00:07:34,850 --> 00:07:43,250
‫So we'll run these lines of code also filed but well is created and CPE in the script callback variable

76
00:07:43,250 --> 00:07:45,160
‫is also created.

77
00:07:45,290 --> 00:07:54,710
‫Now before we create a new model on the same data because the model information is stored in model func

78
00:07:54,710 --> 00:08:02,420
‫variable plus all the information is also stored in the data section that is running in the background.

79
00:08:02,420 --> 00:08:04,700
‫We can clear that information.

80
00:08:04,700 --> 00:08:07,490
‫It will have two helpful impact.

81
00:08:07,490 --> 00:08:11,690
‫One is it will clear the memory that is being used in our system.

82
00:08:11,780 --> 00:08:13,280
‫So our system will perform better.

83
00:08:14,300 --> 00:08:20,270
‫Secondly when we train our new model it will start afresh and it will not start with the weights that

84
00:08:20,390 --> 00:08:26,010
‫we have been previously trained with so to clear the history.

85
00:08:26,090 --> 00:08:34,240
‫We remove the previously trained variable using the item function and clear decision using key underscore

86
00:08:34,520 --> 00:08:36,850
‫clear underscore section function.

87
00:08:36,920 --> 00:08:45,170
‫This would clear all the background information that we have stored in the get us library.

88
00:08:46,700 --> 00:08:48,680
‫Now we'll be training a new model.

89
00:08:48,680 --> 00:08:53,850
‫This model I'm calling as model underscores callback.

90
00:08:54,540 --> 00:08:58,920
‫First we defined the architecture using gave as underscored model function.

91
00:08:58,920 --> 00:09:02,160
‫Then we configured our model using the combine function

92
00:09:05,140 --> 00:09:11,860
‫then we train our model using different function but when we are training our model we have to include

93
00:09:12,070 --> 00:09:16,640
‫one new parameter this parameter is called callbacks.

94
00:09:16,630 --> 00:09:24,160
‫And in this we have to specify the callback variable which contains the information of when that file

95
00:09:24,160 --> 00:09:26,590
‫is to be created and where it is to be created.

96
00:09:28,420 --> 00:09:36,050
‫So this check point is what each epoch and at each epoch a file will be saved at this file.

97
00:09:36,130 --> 00:09:39,850
‫But when I run a different function

98
00:09:49,990 --> 00:09:58,090
‫at the end of each epoch a new file is being saved.

99
00:09:58,090 --> 00:10:07,000
‫Let us go to the files tab open the check point and you can see that we have the 30 files corresponding

100
00:10:07,000 --> 00:10:07,990
‫to each book.

101
00:10:09,070 --> 00:10:17,130
‫Each of these files contains the information of the neural network at the end of that particular epoch.

102
00:10:17,180 --> 00:10:26,980
‫So at the end of first epoch our model was giving a loss of twenty three point forty five with a mean

103
00:10:26,980 --> 00:10:34,180
‫absolute error of three point forty one nine debates at this particular point are stored in the epoch

104
00:10:34,180 --> 00:10:34,690
‫file 1

105
00:10:39,120 --> 00:10:44,700
‫You can also check the list of files stored in the territory using the list stored files function.

106
00:10:45,270 --> 00:10:53,310
‫It will list down all the files that you have in this territory and if you want to load the date in

107
00:10:53,310 --> 00:10:54,460
‫a particular model.

108
00:10:54,810 --> 00:11:03,870
‫So if I want to load debate in the 10th model into a new model that I'm calling as bank model only you

109
00:11:03,870 --> 00:11:10,170
‫can use Load model as DFA function as we saw earlier also.

110
00:11:10,400 --> 00:11:13,500
‫And we can specify the flight path where it is located.

111
00:11:14,310 --> 00:11:20,960
‫So the epoch hyphen then file is located at the check point battery.

112
00:11:21,660 --> 00:11:29,940
‫And when we run this a tent model variable is created and it is a model containing the information of

113
00:11:29,940 --> 00:11:31,540
‫the tenth epoch.

114
00:11:31,590 --> 00:11:38,460
‫So whatever the veer to it at this particular point are now assigned to the endpoint.

115
00:11:39,120 --> 00:11:44,600
‫You can see these somebody of this 10th model also the architecture will be automatically assigned as

116
00:11:44,610 --> 00:11:51,750
‫the same architecture that we define for the model on which we were fitting the training data to whatever

117
00:11:51,780 --> 00:12:00,410
‫architecture that model callback had will be the same architecture assigned to paint model.

118
00:12:00,540 --> 00:12:08,040
‫There is one important feature of this callback process that we can use instead of saving all the 30

119
00:12:08,040 --> 00:12:13,290
‫files we can save only one file which has the best model.

120
00:12:14,010 --> 00:12:20,730
‫And how do we judge that which is the best model we can see that whatever model has the least validation

121
00:12:20,730 --> 00:12:30,070
‫laws save the information of only that model and do not save the information of any of the model.

122
00:12:30,480 --> 00:12:38,010
‫So when we are defining art callback with evil using callback underscore model and is called Checkpoint

123
00:12:38,100 --> 00:12:50,610
‫function we can add two more parameters fine part is mandatory first parameter to be added is what values

124
00:12:50,670 --> 00:12:58,290
‫to be monitored since this is a regression problem we'll be looking at validation loss if it would have

125
00:12:58,290 --> 00:13:05,420
‫been a classification problem we would be monitoring the accuracy and the second parameter is same.

126
00:13:05,510 --> 00:13:08,900
‫Best only is equal to two.

127
00:13:09,120 --> 00:13:18,210
‫This means that only the model with best validation loss that is the least validation loss will be seen

128
00:13:18,820 --> 00:13:30,430
‫in this file with the title Best and the score model dot for you to recreate this variable first delete

129
00:13:30,460 --> 00:13:31,590
‫the previous models

130
00:13:35,190 --> 00:13:44,130
‫and start building a new model with defined the architecture configured it and train it

131
00:13:48,780 --> 00:13:54,250
‫so it will again done for all the 30 epochs but we do it.

132
00:13:54,270 --> 00:14:01,630
‫Epoch has the least validation loss which is shown by this green graph.

133
00:14:02,220 --> 00:14:03,630
‫Only that will be saved.

134
00:14:05,790 --> 00:14:14,260
‫So if we go back to our working directory here's our best understood model not 5.

135
00:14:14,430 --> 00:14:21,800
‫This contains the information of the model with least validation loss.

136
00:14:21,900 --> 00:14:28,740
‫You may also notice here that instead of validation data I'm just using past data since this dataset

137
00:14:28,770 --> 00:14:35,020
‫has only five six observations out of which we have one or two observations at this data.

138
00:14:35,310 --> 00:14:37,510
‫Instead of creating a separate validation set.

139
00:14:37,540 --> 00:14:43,290
‫I'm just using the best data set as the validation dataset here.

140
00:14:43,560 --> 00:14:49,650
‫If you have larger data I would recommend that you keep small amounts of data separately as a validation

141
00:14:49,690 --> 00:14:57,430
‫dataset which you can use here to monitor the validation loss.

142
00:14:58,260 --> 00:15:01,860
‫So whichever it is the best model is stored in this file.

143
00:15:01,950 --> 00:15:09,080
‫You can load all the information of this model and do your new best model variable.

144
00:15:09,570 --> 00:15:14,160
‫And here you have this model you can use this model for predicting in the future.

145
00:15:18,190 --> 00:15:25,450
‫The last thing I'm going to discuss in this lecture is the feature of really stopping in callbacks.

146
00:15:25,510 --> 00:15:27,960
‫It is a very important feature.

147
00:15:28,210 --> 00:15:36,700
‫The basic concept is instead of running the model for all the epochs we can specify a large value for

148
00:15:36,700 --> 00:15:41,510
‫the epochs Randy model and stop it.

149
00:15:41,590 --> 00:15:46,540
‫When we do not see much improvement in the accuracy.

150
00:15:46,900 --> 00:15:55,750
‫So basically instead of running it for only 30 we tell Dad that I need four hundred books but stop at

151
00:15:55,750 --> 00:16:02,170
‫that point of time when you stop seeing any improvement in a particular metric that we have specified.

152
00:16:02,350 --> 00:16:10,930
‫For example here I am creating that callback variable in that variable I'm adding two part one part

153
00:16:11,020 --> 00:16:16,780
‫is the same model take point part which contains the name of defined in which the information will be

154
00:16:16,780 --> 00:16:27,730
‫stored what parameter is to be monitored and whether to save only the best model or save all the models.

155
00:16:27,730 --> 00:16:31,710
‫The other part is called back at least topping.

156
00:16:32,650 --> 00:16:39,640
‫In this part we tell what parameters to be monitored and how much patient do we have.

157
00:16:39,640 --> 00:16:46,840
‫That is for how many epochs will we see whether there is an improvement or not.

158
00:16:46,840 --> 00:16:54,760
‫If there is no improvement for three consecutive epochs in the validation lost parameter we will top

159
00:16:54,850 --> 00:17:02,460
‫our model training then only to basically earlier when we ran our model.

160
00:17:02,470 --> 00:17:06,930
‫You can see here that the validation lost is not improving much.

161
00:17:07,090 --> 00:17:09,430
‫Only the training losses decreasing.

162
00:17:09,520 --> 00:17:14,330
‫But even that improvement is not significant.

163
00:17:14,350 --> 00:17:20,340
‫This is suggesting that we are just over on our model to prevent this awful thing.

164
00:17:20,340 --> 00:17:28,990
‫We'll be using at least topping of training our model so we run this good.

165
00:17:29,230 --> 00:17:33,450
‫This has created our callbacks list very well.

166
00:17:34,300 --> 00:17:38,320
‫We believe the previous model we clear dictate our station.

167
00:17:38,320 --> 00:17:48,150
‫Then we define the architecture of a new model configured it and now when we are training our model

168
00:17:49,180 --> 00:17:56,320
‫we specify a large number of epochs because we know that their training will automatically stop when

169
00:17:56,700 --> 00:18:01,840
‫there is no significant improvement in the validation loss parameter.

170
00:18:04,150 --> 00:18:09,970
‫So again in the validation dataset I have given the test data center only callbacks is equal to callbacks

171
00:18:09,970 --> 00:18:16,360
‫list within this.

172
00:18:16,990 --> 00:18:26,170
‫It starts training but it stops at the fifth epoch only because after the first epoch there was some

173
00:18:26,170 --> 00:18:31,640
‫improvement in the validation lost from 28 became nineteen point five.

174
00:18:32,730 --> 00:18:39,500
‫But after that in the next three consecutive epochs the validation lost did not decrease.

175
00:18:40,510 --> 00:18:43,750
‫So we stopped training our model.

176
00:18:43,750 --> 00:18:50,700
‫I did 50 POC only 2 even though we specified that we will run this model for hundred epochs.

177
00:18:50,950 --> 00:18:58,300
‫We will save the processing pain and or training of of our model because we used at least hoping this

178
00:18:58,300 --> 00:19:04,840
‫model in the fifth epoch is stored in this best model at least topping out at five.

179
00:19:05,860 --> 00:19:09,560
‫You can load that model using the Load model is of.

180
00:19:09,570 --> 00:19:10,000
‫Function

181
00:19:12,910 --> 00:19:23,470
‫well that's all in this lecture we saw how to see all the information of a model into a separate file.

182
00:19:23,470 --> 00:19:29,080
‫The benefit of this is you can share this file with your colleagues with your student or anyone in your

183
00:19:29,080 --> 00:19:37,390
‫office and whenever someone loads a model from that file that model will have the exact same architecture

184
00:19:37,510 --> 00:19:39,400
‫and debate that your model had.

185
00:19:42,070 --> 00:19:49,260
‫Then we saw the importance of using callbacks callbacks help the same model at each epoch.

186
00:19:49,600 --> 00:19:51,130
‫Then we saw how to see.

187
00:19:52,330 --> 00:19:58,540
‫Then we saw how to use saved best only parameter to save only one file.

188
00:19:58,690 --> 00:20:02,820
‫Instead of saving separate files for each epoch.

189
00:20:03,430 --> 00:20:10,690
‫And lastly we saw the use of at least copying functionality so that we can prevent what fitting and

190
00:20:10,840 --> 00:20:15,220
‫excessive processing time to play in our model.

191
00:20:15,220 --> 00:20:15,640
‫Thanks.

