1
00:00:00,540 --> 00:00:06,660
Hello, everyone, and welcome to this new section in which we'll look at different ways of creating

2
00:00:06,660 --> 00:00:12,510
models other than the sequential API which we've seen so far in the section.

3
00:00:12,510 --> 00:00:20,340
We'll look at the functional API, we'll look at building callable models, we'll look at building model

4
00:00:20,340 --> 00:00:26,640
of your subclasses, and we'll also look at building our own custom layers.

5
00:00:26,670 --> 00:00:32,610
Previously in this class we said that there are three ways in which models are built in TensorFlow,

6
00:00:32,790 --> 00:00:39,150
that is the sequential API using the functional API.

7
00:00:39,150 --> 00:00:42,590
And then finally model subclasses.

8
00:00:42,810 --> 00:00:47,430
As of this point, we have been using the sequential API.

9
00:00:47,460 --> 00:00:48,930
As you could see right here.

10
00:00:49,620 --> 00:00:58,320
Now you may ask yourself why do we need to use a different method in creating TensorFlow models when

11
00:00:58,320 --> 00:01:06,360
so far we've achieved close to 99% train accuracy and around 95% accuracy.

12
00:01:06,870 --> 00:01:14,190
Now, as you may have noticed so far, all the models we've been building have taken up this kind of

13
00:01:14,190 --> 00:01:16,800
structure where we have an input.

14
00:01:16,800 --> 00:01:24,270
We have the first layer, the next layer, which has been stacked in this sequential manner right up

15
00:01:24,270 --> 00:01:26,460
to this very last layer here.

16
00:01:26,460 --> 00:01:28,230
And then we have the output.

17
00:01:28,620 --> 00:01:36,090
So the question we could ask ourselves is what if we have a model which takes in, say, two inputs

18
00:01:36,090 --> 00:01:38,760
and has three outputs?

19
00:01:39,810 --> 00:01:47,880
These kinds of models are very popular in deep learning and we shall look at them subsequently.

20
00:01:47,880 --> 00:01:54,360
But before getting there, you could just imagine a problem where instead of classifying whether we

21
00:01:54,360 --> 00:01:59,430
have a non parasitic or a parasitic cell, we want to.

22
00:02:01,350 --> 00:02:10,470
Know the exact position of that parasitic cell or in general that cell in the image, you would find

23
00:02:10,470 --> 00:02:16,830
that you would have one output which classifies whether it's a parasitic or not.

24
00:02:16,830 --> 00:02:23,820
So we have this first output, parasitic or uninfected, and then this other output, which gives us

25
00:02:23,820 --> 00:02:30,870
the position of the cell or the exact position of the cell in the image.

26
00:02:30,870 --> 00:02:35,490
So here we see already how we could get to outputs from this.

27
00:02:35,490 --> 00:02:37,710
Let's take this third output out.

28
00:02:37,710 --> 00:02:41,190
So here we see we could have let's let's even take this one.

29
00:02:41,190 --> 00:02:45,810
So here we have this one output to output model.

30
00:02:45,810 --> 00:02:50,160
And with a sequential API, we can't really do this.

31
00:02:50,160 --> 00:02:54,480
So that's why working with a functional API is very important.

32
00:02:55,650 --> 00:03:03,450
The next point is we'll be able to create more complex models with a functional API.

33
00:03:03,450 --> 00:03:11,130
So there is this model known as the rest net, which is very popular in deep learning computer or deep

34
00:03:11,130 --> 00:03:12,560
learning for computer vision.

35
00:03:12,570 --> 00:03:16,020
Now a rest not like structure will look like this.

36
00:03:16,020 --> 00:03:23,940
We have this this layer of outputs I've been passing to this next layer and then we have.

37
00:03:25,270 --> 00:03:31,450
The outputs of this, which are going to be concatenated with these outputs.

38
00:03:32,230 --> 00:03:38,160
And then after this concatenation, we are going to pass this to the next layer right here.

39
00:03:38,170 --> 00:03:42,370
So if we want to add this layer, we could have a layer here and have that.

40
00:03:42,370 --> 00:03:48,640
So as we're saying, we take this output and then concatenate it with this next output before passing

41
00:03:48,640 --> 00:03:49,900
to this next layer.

42
00:03:49,900 --> 00:03:56,740
And so these kinds of structures or those kinds of models could not be built with the sequential API

43
00:03:56,740 --> 00:03:59,750
and hence the need for the functional API.

44
00:03:59,770 --> 00:04:08,140
And then the last reason why we are going to be using the functional API is the fact that we could use

45
00:04:08,140 --> 00:04:11,380
shared layers with shared layers.

46
00:04:11,380 --> 00:04:19,840
We could have a layer or a particular layer in our model, which has already a predefined way of encoding

47
00:04:19,840 --> 00:04:20,770
information.

48
00:04:20,770 --> 00:04:26,560
So when we pass information, let's say we have this input, let's say input one when we pass this input

49
00:04:26,560 --> 00:04:35,740
one, this layer right here or this encoder produces an output which is going to be different from when

50
00:04:35,740 --> 00:04:39,310
we pass in another input I to.

51
00:04:41,500 --> 00:04:45,850
But the way it produces outputs, it's in a very thoughtful manner.

52
00:04:45,850 --> 00:04:52,300
So we could have either one eye to eye three, which all share this layer.

53
00:04:53,260 --> 00:04:58,300
And then we have all the layers of the model which follow on that set.

54
00:04:58,300 --> 00:05:01,950
We'll look at how to create the functional API.

55
00:05:01,960 --> 00:05:08,380
So here we have the sequential and then just below we are going to create this functional API.

56
00:05:09,010 --> 00:05:13,210
Before starting with the creation, we are going to impart some classes.

57
00:05:13,210 --> 00:05:18,130
So start by importing the input class right here, which is a layer.

58
00:05:18,130 --> 00:05:27,280
We impart input and then we have from TensorFlow cross layer or rather models.

59
00:05:27,280 --> 00:05:30,610
We're going to import model.

60
00:05:30,610 --> 00:05:35,530
So we import the model right here and we impart the input.

61
00:05:35,530 --> 00:05:36,820
We run this.

62
00:05:36,820 --> 00:05:37,990
That should be fine.

63
00:05:39,220 --> 00:05:44,050
We now have this func input since we use the functional API.

64
00:05:44,080 --> 00:05:49,960
Just a way of calling that we have the input and then we have input which we just called and this takes

65
00:05:49,960 --> 00:05:50,950
in the shape.

66
00:05:50,950 --> 00:05:55,840
So here we are going to copy this exact shape we use in the sequential API.

67
00:05:55,840 --> 00:06:02,530
We have the shape right here, There we go, we copy that shape and then we reuse it.

68
00:06:02,530 --> 00:06:05,290
You're creating this input layer.

69
00:06:05,290 --> 00:06:08,980
So here we have that and then we have your shape.

70
00:06:08,980 --> 00:06:10,810
So we have the shape.

71
00:06:10,810 --> 00:06:18,310
This point, you could start stacking up all these different layers we had stacked up in the sequential

72
00:06:18,310 --> 00:06:24,730
or with this when we're using the sequential API we started with this year, this comes to DX right

73
00:06:24,730 --> 00:06:25,960
up to this dance layer.

74
00:06:25,960 --> 00:06:27,010
So there we go.

75
00:06:27,010 --> 00:06:33,980
We're going to make use of this, so we copy that and then we are going to paste this out right here.

76
00:06:34,000 --> 00:06:37,450
Now, first, since first we have an output.

77
00:06:37,450 --> 00:06:44,140
So first, since we have this layer that we have this convert to DX, which we've defined, and then

78
00:06:44,140 --> 00:06:48,580
we pass in the output from this input layer.

79
00:06:48,580 --> 00:06:57,130
So here we have this func input, we copy that and then we pass this into this, into this conf layer

80
00:06:57,130 --> 00:06:57,830
right here.

81
00:06:57,850 --> 00:07:04,870
Now once we pass this into this conf layer, we have an output and that output is this x and then you

82
00:07:04,870 --> 00:07:05,980
should get that right.

83
00:07:05,980 --> 00:07:10,090
We pass this X into this back norm layer.

84
00:07:10,090 --> 00:07:17,290
So here we have X as you could see, and then we have an output of X, there we go from here we pass

85
00:07:17,290 --> 00:07:22,720
in the X into the max, pull max pull to the layer.

86
00:07:22,720 --> 00:07:27,940
So we have this, we cut that and then we have this X right here.

87
00:07:27,940 --> 00:07:31,900
So we'll just repeat this same process right up to the end.

88
00:07:31,900 --> 00:07:33,190
And there we go.

89
00:07:33,220 --> 00:07:39,790
You see that we haven't done much changes or as compared with the sequential API.

90
00:07:39,790 --> 00:07:40,660
So that's it.

91
00:07:40,660 --> 00:07:44,140
We pass in this input right here.

92
00:07:44,140 --> 00:07:49,830
We have X, we pass it in, we have this, we pass in and right up to this end.

93
00:07:49,840 --> 00:07:55,390
Now, once we get to the end, we are now going to create the net model from this.

94
00:07:55,390 --> 00:08:02,110
So we have learned model, equal model which will imported and then we have the func input.

95
00:08:02,830 --> 00:08:11,680
Now let's say we have func output, so we pass this last and then our last output is from output.

96
00:08:11,680 --> 00:08:15,220
So we have the input and then we have the output.

97
00:08:15,400 --> 00:08:16,390
So there we go.

98
00:08:16,390 --> 00:08:22,150
We can now give it a name, we have name new net model.

99
00:08:23,110 --> 00:08:26,980
If you look up we had Mr. Right within this right here.

100
00:08:26,980 --> 00:08:29,530
So this would be the input image here.

101
00:08:29,530 --> 00:08:34,630
We have our input image and then we've created our model, learn that model.

102
00:08:34,630 --> 00:08:40,040
And then from here you could simply do net model dot summary.

103
00:08:40,060 --> 00:08:48,690
Now you will notice that we should have exactly the same summary as we had with the sequential API.

104
00:08:48,700 --> 00:08:50,980
So let's run that and see what we get.

105
00:08:52,240 --> 00:08:54,880
Yeah, we have how many parameters.

106
00:08:54,880 --> 00:08:55,480
We have

107
00:08:56,950 --> 00:09:01,330
4,668,297 parameters.

108
00:09:01,870 --> 00:09:02,590
There we go.

109
00:09:02,590 --> 00:09:07,750
We see we have exactly the same number of parameters, the same number of trainable and non trainable

110
00:09:07,750 --> 00:09:08,620
parameters.

111
00:09:08,620 --> 00:09:14,920
So basically what we've done here is we've we created this model we created with a sequential API.

112
00:09:15,550 --> 00:09:17,140
Now we've gotten this.

113
00:09:17,140 --> 00:09:23,050
We'll see that we have to change absolutely nothing from our code.

114
00:09:23,050 --> 00:09:28,720
So here we are just going to compile our model without changing anything.

115
00:09:28,720 --> 00:09:30,670
We have the same net model.

116
00:09:30,670 --> 00:09:31,030
Now.

117
00:09:31,030 --> 00:09:37,240
We could also change this let's say net func So you see clearly that we are actually using this functional

118
00:09:37,240 --> 00:09:38,530
model right here.

119
00:09:38,530 --> 00:09:40,440
So we have this func.

120
00:09:40,900 --> 00:09:41,950
There we go.

121
00:09:43,000 --> 00:09:43,510
Funk.

122
00:09:43,510 --> 00:09:44,200
And that's it.

123
00:09:44,230 --> 00:09:49,440
We could run that, and then we recompile right here.

124
00:09:49,450 --> 00:09:51,750
So we are not changing any parts of this.

125
00:09:51,790 --> 00:09:56,470
We recompile that and then we train the model.

126
00:09:57,100 --> 00:10:01,690
We are getting this error because of the way we named this model right here.

127
00:10:01,690 --> 00:10:04,870
So let's have this learn that model.

128
00:10:05,050 --> 00:10:06,040
That's fine.

129
00:10:07,690 --> 00:10:11,830
We recompile and then we run.

130
00:10:12,190 --> 00:10:13,150
So that's it.

131
00:10:13,150 --> 00:10:17,860
We're training our model, and here is what we get is results.

132
00:10:18,550 --> 00:10:25,390
Now, coming back to our model, we'll see that we have this feature extraction unit right here.

133
00:10:25,390 --> 00:10:34,180
So this Conv layers are responsible for extracting useful features from the images, and then this last

134
00:10:34,180 --> 00:10:41,050
layers are responsible for correctly classifying whether the image is parasitic or not.

135
00:10:41,440 --> 00:10:46,990
That said, we could build a model known as feature extractor.

136
00:10:47,200 --> 00:10:49,570
And so your would add this.

137
00:10:49,990 --> 00:10:57,820
We have our model feature extractor which is going to be like similar in construction as what we've

138
00:10:57,820 --> 00:10:58,720
done so far.

139
00:10:58,720 --> 00:11:02,680
So we just have that copied and then we have this.

140
00:11:02,680 --> 00:11:08,410
But the difference is we are not going to include this other this final layers right here.

141
00:11:08,410 --> 00:11:15,760
We'll only end up this point and then we'll have this output this year.

142
00:11:15,760 --> 00:11:18,130
So this is our functional.

143
00:11:18,310 --> 00:11:20,040
Yeah, we'll call this extract all.

144
00:11:20,220 --> 00:11:25,390
Let's just say we have this output, so we have this output and there we go.

145
00:11:25,390 --> 00:11:33,790
So here we have our functional input and then we have this output and then here is the feature extractor.

146
00:11:33,790 --> 00:11:39,130
So we have our feature extractor model right here.

147
00:11:39,580 --> 00:11:48,640
We could do this feature extractor and then we summarize this.

148
00:11:48,640 --> 00:11:50,920
So let's run this and see what we get.

149
00:11:51,100 --> 00:11:51,940
So that's it.

150
00:11:51,940 --> 00:11:56,320
We have our input and then we have this output right here.

151
00:11:57,280 --> 00:12:03,850
At this point, instead of writing all this we wrote here, we're just going to call oh, let's, let's

152
00:12:03,850 --> 00:12:04,780
take from this point.

153
00:12:04,780 --> 00:12:07,030
So we have our feature.

154
00:12:07,420 --> 00:12:09,400
Let's look at the name we gave it.

155
00:12:09,400 --> 00:12:12,070
We gave it the name feature extractor model.

156
00:12:12,070 --> 00:12:17,320
So here we have feature extractor model.

157
00:12:17,320 --> 00:12:19,270
So that's our fixture extractor model.

158
00:12:19,270 --> 00:12:25,240
So we take all this off and then in here we pass in our input.

159
00:12:25,240 --> 00:12:30,580
So notice how we are making this model load like a function.

160
00:12:30,580 --> 00:12:37,060
So TensorFlow models are callable just like the layers.

161
00:12:37,420 --> 00:12:45,430
And as you could see here, this feature extractor model could be seen as a layer just like the dense

162
00:12:45,430 --> 00:12:48,370
layer, the batch nom layer and all other layers.

163
00:12:48,370 --> 00:12:53,680
So we've gotten this X from this input which has been passed in our model.

164
00:12:53,680 --> 00:12:58,440
And then from here you see we pass this X into this flatten and we have the rest.

165
00:12:58,450 --> 00:12:59,670
So that's it.

166
00:12:59,680 --> 00:13:01,930
Let's now rerun this again.

167
00:13:01,930 --> 00:13:04,420
So you could see what we get is output.

168
00:13:04,930 --> 00:13:08,550
And as you could see, we get exactly what we expected.

169
00:13:08,560 --> 00:13:16,120
We have the same number of parameters and there is this difference here where we have this feature extractor.

170
00:13:16,120 --> 00:13:26,230
So unlike before where we had the components like the conf 2d batch norm max pulling and the same like

171
00:13:26,230 --> 00:13:27,190
let's go up here.

172
00:13:27,190 --> 00:13:28,570
There's actually a feature extractor.

173
00:13:28,570 --> 00:13:36,460
So unlike before where we had this and then this now has been replaced with this feature extractor right

174
00:13:36,460 --> 00:13:44,710
here that said we've just built this model using the functional API, and then subsequent sections will

175
00:13:44,710 --> 00:13:51,430
build even more complex models using this functional API models where we're going to use shared layers,

176
00:13:51,430 --> 00:13:55,480
we're going to have multiple inputs, multiple outputs and models.

177
00:13:55,690 --> 00:13:59,920
We are going to have even more complicated model configurations.

178
00:13:59,920 --> 00:14:06,760
It's important to note that you could mix up the functional API model creation style with that of the

179
00:14:06,760 --> 00:14:08,320
sequential API.

180
00:14:08,800 --> 00:14:15,880
So you're instead of creating this, So instead of having this our feature extractor created like this,

181
00:14:15,880 --> 00:14:19,570
we are going to create it using the sequential API.

182
00:14:19,870 --> 00:14:28,300
Let's add that and then we copy out this from your copy of this full model with a sequential API with

183
00:14:28,300 --> 00:14:32,970
PCs out and we take all of the feature extraction part here.

184
00:14:32,980 --> 00:14:37,930
We see we take this off and then we'll have only with this feature extraction part.

185
00:14:38,080 --> 00:14:40,000
Now let's call this feature.

186
00:14:40,160 --> 00:14:42,200
Her extractor.

187
00:14:42,590 --> 00:14:47,560
So feature extractor, sequential model.

188
00:14:47,570 --> 00:14:48,680
There we go.

189
00:14:49,070 --> 00:14:55,100
We pay this out right here and we find so we have our feature extractor model.

190
00:14:55,670 --> 00:14:56,600
We run that.

191
00:14:56,600 --> 00:14:57,620
That's okay.

192
00:14:58,130 --> 00:15:06,050
Let's take this off and then we'll just make sure we put exactly the same here.

193
00:15:06,320 --> 00:15:07,250
So there we go.

194
00:15:07,250 --> 00:15:10,010
We piss it out and we, we run this.

195
00:15:10,550 --> 00:15:13,700
We see we should be able to get exactly the same output.

196
00:15:13,700 --> 00:15:15,830
See, we have exactly the same output.

197
00:15:15,830 --> 00:15:21,020
And you're instead of our feature extractor model we have here sequential layer.

198
00:15:21,350 --> 00:15:22,220
So that's it.

199
00:15:23,620 --> 00:15:29,560
This shows us that we could mix up these different ways of creating models.

200
00:15:30,130 --> 00:15:33,150
From this point, we'll look at the models of class.

201
00:15:33,490 --> 00:15:37,690
So right here we have our model subclass.

202
00:15:39,430 --> 00:15:40,570
There we go.

203
00:15:40,720 --> 00:15:49,900
It's important to note that models of class and permits us to create recursively composable layers and

204
00:15:49,900 --> 00:15:50,740
models.

205
00:15:51,220 --> 00:15:53,020
Now, what does that mean?

206
00:15:53,650 --> 00:16:05,380
This means I could create a layer where its attributes are auto layers, and this layer tracks the weights

207
00:16:05,380 --> 00:16:08,770
and biases of the sub layers.

208
00:16:09,490 --> 00:16:13,990
Before taking an example, let's make this in part.

209
00:16:13,990 --> 00:16:16,750
So we're going to import layer from layers.

210
00:16:16,750 --> 00:16:20,710
We have TensorFlow that Keras layers.

211
00:16:20,710 --> 00:16:31,630
We're going to import layer, we run that and then we move on to create our model using the model subclass.
