1
00:00:00,170 --> 00:00:01,070
At this point.

2
00:00:01,100 --> 00:00:09,530
One question you may ask yourself is we are having this, um, outputs which are being generated from

3
00:00:09,530 --> 00:00:13,520
the making use of this Z vector right here.

4
00:00:14,450 --> 00:00:20,300
But how does the Z vector this Z vector here?

5
00:00:21,260 --> 00:00:27,860
Of the different images vary with the particular digits in those images.

6
00:00:27,860 --> 00:00:36,320
So how does a vector in say let's take this for example in six vary with that of zero uh, with two,

7
00:00:36,530 --> 00:00:39,080
uh, and all other digits.

8
00:00:40,480 --> 00:00:50,620
To experiment with this would copy out this code we had already and then get back here and now try to

9
00:00:50,620 --> 00:00:58,000
pass in the different digits into our encoder and then plot out the positions of.

10
00:00:59,840 --> 00:01:10,220
Z vectors and then see whether there is some correlation with the Z vector position and the input we

11
00:01:10,220 --> 00:01:11,010
have here.

12
00:01:11,030 --> 00:01:13,490
Write your other specific input digit.

13
00:01:14,510 --> 00:01:17,840
That said, we're going to now make use of this.

14
00:01:17,840 --> 00:01:19,460
So we have that.

15
00:01:19,460 --> 00:01:23,000
And then here we're going to have our Y train.

16
00:01:23,000 --> 00:01:30,050
So this time around we'll be needing the Y train because we need to know exactly what digit we're dealing

17
00:01:30,050 --> 00:01:33,950
with, unlike here, where we just needed to have the input images.

18
00:01:34,160 --> 00:01:35,380
Okay, so that's it.

19
00:01:35,390 --> 00:01:43,400
We have X train, y train, we're going to reload this and then we do the usual pre-processing.

20
00:01:43,550 --> 00:01:51,110
From here, we run the cell and then we move straight away to pass in this images in our encoder.

21
00:01:52,320 --> 00:01:54,480
Now similar to what we had here.

22
00:01:54,510 --> 00:02:01,080
Now we're going to be making use of our layers and then picking out the first index.

23
00:02:01,080 --> 00:02:04,650
So here we have our output.

24
00:02:04,680 --> 00:02:12,270
We could call this or basically we have Z, then we have the mean and the variance and then this is

25
00:02:12,270 --> 00:02:13,070
equal.

26
00:02:13,080 --> 00:02:14,790
Let's copy this here.

27
00:02:15,420 --> 00:02:17,400
Um, there we go.

28
00:02:17,430 --> 00:02:18,860
We have that.

29
00:02:18,870 --> 00:02:20,630
Then this is now one.

30
00:02:20,640 --> 00:02:21,270
See that?

31
00:02:21,270 --> 00:02:26,310
And then from here we pass in this x train.

32
00:02:26,730 --> 00:02:27,660
So that's it.

33
00:02:27,690 --> 00:02:34,320
We pass in the X train and then we could go ahead to start with the plotting, uh, plot figure.

34
00:02:34,350 --> 00:02:36,720
Specify the figure size.

35
00:02:37,020 --> 00:02:41,220
Figure size equals 12 by 12.

36
00:02:41,550 --> 00:02:42,600
There we go.

37
00:02:42,600 --> 00:02:45,300
And then we do a scatter plot.

38
00:02:45,330 --> 00:02:51,450
Now, the scatter plot will show us the different positions of the values of Z.

39
00:02:51,480 --> 00:02:53,020
That's the.

40
00:02:55,810 --> 00:02:59,530
Z points in a two dimensional space.

41
00:02:59,680 --> 00:03:00,740
Let's take this off.

42
00:03:00,760 --> 00:03:02,860
This different Z points.

43
00:03:02,860 --> 00:03:07,090
And then also we're going to color this such that.

44
00:03:08,200 --> 00:03:10,690
Digits belonging to the same level.

45
00:03:10,690 --> 00:03:12,430
Have the same color.

46
00:03:13,720 --> 00:03:14,620
There we go.

47
00:03:14,620 --> 00:03:20,500
We're going to plot this as our X and then our Y.

48
00:03:20,740 --> 00:03:23,110
So we have that.

49
00:03:23,110 --> 00:03:28,990
And then now we specify this Y train as our labels.

50
00:03:28,990 --> 00:03:30,100
So that's it.

51
00:03:30,100 --> 00:03:37,090
And then we can now go ahead to plot out this color bar and show.

52
00:03:37,660 --> 00:03:38,770
Let's run that.

53
00:03:39,460 --> 00:03:41,470
And here's what we get.

54
00:03:41,710 --> 00:03:42,970
As you could see.

55
00:03:44,210 --> 00:03:49,250
The labels here go from zero right up to nine.

56
00:03:49,520 --> 00:03:53,990
And then you could notice this different clusters we have in here.

57
00:03:54,170 --> 00:03:56,030
See these different clusters?

58
00:03:56,270 --> 00:03:57,080
See that?

59
00:03:57,830 --> 00:03:59,030
This shows that.

60
00:04:00,510 --> 00:04:10,140
The encoder part of our variational auto encoder has been trained such that now it's able to generate

61
00:04:10,140 --> 00:04:12,990
values of Z where.

62
00:04:13,880 --> 00:04:15,260
This point.

63
00:04:17,660 --> 00:04:27,830
Is output at such that two inputs belonging to the same digit will be closer to each other as compared

64
00:04:27,830 --> 00:04:31,850
to two inputs belonging to two different digits.

65
00:04:31,880 --> 00:04:40,940
So what we've been doing so far is we've been training the variational autoencoder model with our own

66
00:04:40,940 --> 00:04:43,130
custom training block.

67
00:04:43,160 --> 00:04:51,310
You see right here, we make use of this gradient tape and we carry out the gradient descent manually

68
00:04:51,320 --> 00:04:58,100
as compared to when you just have, say, model fit and then you pass in your train data set.

69
00:04:58,100 --> 00:05:03,250
And basically, uh, this takes care of doing the training.

70
00:05:03,260 --> 00:05:11,510
Now the advantage of working with this fit method is that it's quite simple to use, but uh, compared

71
00:05:11,510 --> 00:05:17,160
to this custom training loops, it doesn't give you that much freedom.

72
00:05:17,160 --> 00:05:25,380
And that's why we're making use of this, um, gradient tape here to train our model.

73
00:05:25,650 --> 00:05:33,570
Now, with TensorFlow, it's possible to actually get the best of both worlds.

74
00:05:33,600 --> 00:05:43,110
Now, this means that we can be able to train or get this custom training right here and then still

75
00:05:43,110 --> 00:05:45,660
make use of the fit method.

76
00:05:45,780 --> 00:05:55,470
The way this is done is by overriding the trainstep method of our model, which we shall define.

77
00:05:55,770 --> 00:06:01,950
So that said, let's get right here and define this VA model.

78
00:06:01,980 --> 00:06:09,180
Then we go ahead and define this model as we would do with a usual Keras model.

79
00:06:09,720 --> 00:06:18,060
But just as we had done right here, that's in this model we had built here, we need to take into consideration

80
00:06:18,230 --> 00:06:25,810
in our take us argument here the encoder model and the decoder model.

81
00:06:25,820 --> 00:06:26,750
So that's it.

82
00:06:26,780 --> 00:06:35,960
Now, once we have this two models we have here our encoder, which is our encoder model, and we do

83
00:06:35,960 --> 00:06:37,610
the same for the decoder.

84
00:06:38,000 --> 00:06:42,050
Now, once we're done with this decoder, let's type this out.

85
00:06:42,080 --> 00:06:48,710
We have your decoder, Uh, as we're saying, once we're done with this decoder, the next thing we

86
00:06:48,710 --> 00:06:51,740
want to do is define some loss.

87
00:06:51,740 --> 00:06:54,080
So here we have our loss.

88
00:06:54,230 --> 00:06:56,830
Um, it's actually a loss tracker.

89
00:06:56,840 --> 00:07:00,710
Now, we'll get to understand better why we're calling this a loss tracker.

90
00:07:00,710 --> 00:07:05,780
So here we have the average or the mean of the different losses which you shall get.

91
00:07:06,890 --> 00:07:13,460
And then from here, we'll go ahead to this main section where we shall override the Trainstep method.

92
00:07:13,460 --> 00:07:20,570
So, uh, just like, let's add this here, just like we used to working with methods like model, like

93
00:07:20,570 --> 00:07:24,050
the compile method, like the fit method.

94
00:07:24,050 --> 00:07:32,180
Here we have this Trainstep method, which is called each time we call on this fit method right here.

95
00:07:33,120 --> 00:07:40,200
But since we are now going to make use of the default, um, fit or the default trainstep method, we'll

96
00:07:40,200 --> 00:07:48,000
have to override this and write out our own training block, which is basically this block we have here.

97
00:07:48,000 --> 00:07:55,350
So what we could do here is copy this out so we could simply copy this out and then paste it out here.

98
00:07:55,380 --> 00:07:57,890
Now let's just copy all this.

99
00:07:57,900 --> 00:07:58,890
There we go.

100
00:07:58,890 --> 00:08:01,770
And, uh, that's it.

101
00:08:01,800 --> 00:08:02,880
We paste this out.

102
00:08:02,880 --> 00:08:04,860
So here we have our training block.

103
00:08:04,860 --> 00:08:07,650
We'll call this, um, trainstep.

104
00:08:07,650 --> 00:08:14,250
So we override this Trainstep method, which is a keras model method.

105
00:08:14,430 --> 00:08:16,890
So here we have our trainstep.

106
00:08:16,890 --> 00:08:19,170
We're taking in our batch.

107
00:08:19,470 --> 00:08:20,760
Uh, there we go.

108
00:08:20,760 --> 00:08:21,930
And then.

109
00:08:23,130 --> 00:08:26,670
Is going to be similar to what we've had already in the training block.

110
00:08:26,670 --> 00:08:33,180
So you see here we have our Z mean log VAR, which is gotten from the encoder model.

111
00:08:33,180 --> 00:08:37,710
And from here we pass Z and then we obtain Y pred.

112
00:08:37,740 --> 00:08:38,820
We have our Y.

113
00:08:38,850 --> 00:08:40,500
True, we obtain our loss.

114
00:08:40,500 --> 00:08:44,970
We've already defined our custom loss method and that's it.

115
00:08:45,920 --> 00:08:48,920
Then from here, we're going to update our loss metric.

116
00:08:48,920 --> 00:08:54,730
So here we have, um, loss tracker, which we defined already here.

117
00:08:54,740 --> 00:08:56,690
We're going to update its state.

118
00:08:56,690 --> 00:09:02,630
So we update our loss state with the value of this loss.

119
00:09:02,960 --> 00:09:03,830
So that's it.

120
00:09:03,980 --> 00:09:07,970
Now we are going to return the result.

121
00:09:07,970 --> 00:09:17,450
So we have this dictionary, we have loss and then we have our loss tracker dot result.

122
00:09:17,450 --> 00:09:18,170
See that?

123
00:09:18,170 --> 00:09:19,040
We have that.

124
00:09:19,040 --> 00:09:20,780
So there we go.

125
00:09:20,780 --> 00:09:24,350
We have updated our loss, we output the loss.

126
00:09:25,220 --> 00:09:31,640
But one important method we need to call again is the metrics method.

127
00:09:31,640 --> 00:09:33,770
So here we have metrics.

128
00:09:34,280 --> 00:09:44,720
You could define several metrics, but here we just have this loss and then we return our loss tracker.

129
00:09:44,720 --> 00:09:49,140
So we have loss tracker returned.

130
00:09:49,170 --> 00:09:52,050
Now notice how this is actually a list.

131
00:09:52,050 --> 00:09:58,170
So you see, this is a list because we could have, um, say different losses which we could return.

132
00:09:58,170 --> 00:10:04,560
And then we have the property decorator which will place right here.

133
00:10:04,560 --> 00:10:12,930
So once we have this set, you see, we do not need anymore to make use of this or to write this as,

134
00:10:13,080 --> 00:10:14,130
as it was reading.

135
00:10:14,130 --> 00:10:20,910
And so this means that we, we are not going to be writing out, for example, training start for epoch.

136
00:10:20,940 --> 00:10:24,330
This, uh, the training loss is this and all of that.

137
00:10:24,330 --> 00:10:28,740
Now all we need to do is just call on our model.fit method.

138
00:10:28,740 --> 00:10:33,480
So right here, let's do, uh, let's run this first.

139
00:10:33,480 --> 00:10:35,700
Let's run this right here.

140
00:10:36,300 --> 00:10:37,320
There we go.

141
00:10:37,320 --> 00:10:39,240
And then we go ahead.

142
00:10:39,240 --> 00:10:46,100
We're getting an error Now from here we define our var.

143
00:10:46,100 --> 00:10:51,500
So it's quite similar to to what we had already seen here.

144
00:10:51,680 --> 00:10:52,370
See that?

145
00:10:52,370 --> 00:10:57,530
Now we have this Var, which is now this which we've created here.

146
00:10:57,530 --> 00:11:07,940
So let's have that equal var E, and then it takes in the encoder model, encoder model and the decoder

147
00:11:07,940 --> 00:11:08,780
model.

148
00:11:08,780 --> 00:11:09,500
That's it.

149
00:11:09,500 --> 00:11:13,010
So once we have this now we could do just as we are used to doing.

150
00:11:13,010 --> 00:11:15,710
Let's, let's even say model, let's just say model.

151
00:11:15,710 --> 00:11:23,360
And then here we have model.compile as usual, and then here we're going to pass in our optimizer.

152
00:11:23,360 --> 00:11:26,000
So here we have Optimizer.

153
00:11:26,240 --> 00:11:29,300
Uh, guess we define this optimizer already.

154
00:11:29,300 --> 00:11:30,590
So let's check here.

155
00:11:30,590 --> 00:11:32,150
Yeah, we have defined this optimizer.

156
00:11:32,150 --> 00:11:41,540
So here we just have optimizer, um, equal Optimizer, which we've defined already, and then we do

157
00:11:41,690 --> 00:11:43,580
our model.fit.

158
00:11:43,610 --> 00:11:44,390
See that?

159
00:11:44,390 --> 00:11:45,350
So that's all.

160
00:11:45,350 --> 00:11:49,590
We just need to, um, now make or call this fit method.

161
00:11:49,590 --> 00:11:56,040
And then we have our train data set which we pass in the number of epochs.

162
00:11:56,400 --> 00:12:04,770
We'll define this to be 20 and then the batch size, um, equal say 128.

163
00:12:04,770 --> 00:12:05,490
So that's it.

164
00:12:05,490 --> 00:12:12,840
So here, um, unlike before, where we needed, for example, to specify the optimizer in this way

165
00:12:12,840 --> 00:12:21,360
and then actually, uh, get in-depth to understand that this, uh, gradient descent process needs,

166
00:12:21,360 --> 00:12:25,740
for example, the partial derivatives and the trainable weights.

167
00:12:25,770 --> 00:12:32,970
Now you'll see that unlike before, where if you were, for example, needing to make use of some callbacks,

168
00:12:32,970 --> 00:12:39,120
you would have to write that or include that in your with some custom code.

169
00:12:39,120 --> 00:12:43,650
But now all you need to do is just have callbacks.

170
00:12:43,650 --> 00:12:47,400
So we, we, we both will.

171
00:12:47,400 --> 00:12:57,960
Now how to write our own custom training loops while still taking advantage of everything that comes

172
00:12:57,960 --> 00:12:59,550
with the fit method.

173
00:12:59,550 --> 00:13:04,050
So right here you could have some callbacks and that will be it.

174
00:13:04,050 --> 00:13:10,050
Anyway, let's take this off and then let's get back here where there's this slight error.

175
00:13:10,080 --> 00:13:13,650
You see here we have this v dot trainable weights and that.

176
00:13:13,650 --> 00:13:20,910
Now this was in the case where we had defined our this way, we had this model and then get it into

177
00:13:20,910 --> 00:13:23,670
our custom training loop right here.

178
00:13:23,700 --> 00:13:25,050
Let's get down here.

179
00:13:25,050 --> 00:13:32,670
We had to actually, uh, we, the way we got the trainable weights was from our model, which we had

180
00:13:32,670 --> 00:13:33,390
defined.

181
00:13:33,420 --> 00:13:42,390
Now, given that we have this model right here, the way we access this trainable weights is by using

182
00:13:42,390 --> 00:13:43,170
self.

183
00:13:43,170 --> 00:13:45,290
So we have self dot trainable weights.

184
00:13:45,320 --> 00:13:48,980
And then here we have soft, dot trainable weights.

185
00:13:49,940 --> 00:13:59,030
And this is simply possible because we are inheriting from a Keras model which already has this different

186
00:13:59,840 --> 00:14:01,940
attributes which have been defined.

187
00:14:01,970 --> 00:14:04,440
Now, that said, let's run this.

188
00:14:04,460 --> 00:14:05,750
That should be fine.

189
00:14:05,960 --> 00:14:07,250
Training are complete.

190
00:14:07,250 --> 00:14:08,330
You can see that.

191
00:14:08,690 --> 00:14:09,230
Um.

192
00:14:10,510 --> 00:14:15,700
We getting our usual outputs when we making use of the fit method?

193
00:14:16,640 --> 00:14:19,100
And then we could go ahead and start with the testing.

194
00:14:19,100 --> 00:14:24,200
So we run the cells and then in here we have some modifications to make.

195
00:14:24,200 --> 00:14:32,090
So right here or previously we had this layers and the predict and we took in the inputs and we got

196
00:14:32,090 --> 00:14:32,810
some output.

197
00:14:32,840 --> 00:14:39,200
Now here we have our model model dot predict, see that?

198
00:14:39,200 --> 00:14:42,410
But this model is, is a whole model.

199
00:14:42,410 --> 00:14:48,500
So since we want to make use of our decoder, we're going to say model dot, decoder model.

200
00:14:48,500 --> 00:14:49,490
There we go.

201
00:14:49,520 --> 00:14:56,930
Model dot decoder actually dot decoder because here you've defined this decoder attribute.

202
00:14:56,930 --> 00:15:00,650
So we have model dot decoder.

203
00:15:00,680 --> 00:15:01,670
There we go.

204
00:15:01,700 --> 00:15:04,190
Dot predict and that should be fine.

205
00:15:04,340 --> 00:15:07,430
Okay, so let's run this and then see what we get.

206
00:15:09,020 --> 00:15:09,770
There we go.

207
00:15:09,770 --> 00:15:18,290
You see, we have exact same kind of output as when we were having our own custom training loop right

208
00:15:18,290 --> 00:15:18,830
here.

209
00:15:19,700 --> 00:15:21,190
And that's it for this section.

210
00:15:21,200 --> 00:15:22,220
Hope you enjoyed it.

211
00:15:22,250 --> 00:15:23,090
See you in the next section.
