1
00:00:00,210 --> 00:00:05,550
Hello, dear friends, and welcome to this new session in which you will be building custom losses and

2
00:00:05,550 --> 00:00:06,330
metrics.

3
00:00:06,450 --> 00:00:13,320
The first method we look at is building a custom loss method without parameters, the next with parameters.

4
00:00:13,320 --> 00:00:16,650
And finally, we'll build a custom loss class.

5
00:00:16,830 --> 00:00:21,090
And then from here we'll go on to build custom metric method with parameters.

6
00:00:21,090 --> 00:00:26,330
Custom metric method without parameters and custom metric classes.

7
00:00:26,340 --> 00:00:32,670
Most times when building models and training them, all you need to do is to pick this last function

8
00:00:32,670 --> 00:00:36,330
from one of these losses available in the documentation.

9
00:00:36,330 --> 00:00:44,580
Now, in a case where you have to build a loss function from scratch or a custom loss function in which

10
00:00:44,580 --> 00:00:51,500
defining the loss isn't as easy as just specifying this loss function name right here.

11
00:00:51,510 --> 00:00:56,250
So right now let's look at how to build such custom loss functions.

12
00:00:56,250 --> 00:01:00,960
Yeah, we're going to have this custom binary custom, let's call it custom.

13
00:01:00,960 --> 00:01:04,460
B.C.E. B.c.e stands for binary cross entropy.

14
00:01:04,470 --> 00:01:11,310
So we have the custom BCE loss right here, and then we'll define it just above this cell.

15
00:01:11,340 --> 00:01:14,940
Now we have our custom BCE loss.

16
00:01:15,540 --> 00:01:16,680
There we go.

17
00:01:16,680 --> 00:01:24,780
And here's what we're going to do is we take our will define this BCE object, the binary cross entropy

18
00:01:24,780 --> 00:01:25,890
loss object.

19
00:01:25,890 --> 00:01:30,510
And obviously, yeah, we have binary cross entropy.

20
00:01:30,510 --> 00:01:31,350
There we go.

21
00:01:31,350 --> 00:01:38,250
So we've defined this object of this class, and then we return this computation of the binary cross

22
00:01:38,250 --> 00:01:42,540
entropy of the Y, true, and the Y.

23
00:01:42,540 --> 00:01:47,340
PRED So, yeah, this white row is the actual output labels.

24
00:01:47,340 --> 00:01:49,890
And here's what the model predicts.

25
00:01:49,890 --> 00:01:56,550
You could understand this better by taking a look at the documentation where you see the define this

26
00:01:56,550 --> 00:01:57,210
object.

27
00:01:57,210 --> 00:02:05,010
And then in computing this binary cross entropy loss, you see it specify this y true and widespread

28
00:02:05,010 --> 00:02:05,790
right here.

29
00:02:05,820 --> 00:02:11,580
Now, this binary cross entropy could take several arguments, for example, from logics labels, mode

30
00:02:11,580 --> 00:02:14,790
in the axis reduction and the name.

31
00:02:14,790 --> 00:02:15,510
So that's it.

32
00:02:15,510 --> 00:02:18,210
We have this custom binary cross entropy.

33
00:02:18,210 --> 00:02:22,410
And just here, all we need to do is to specify this name.

34
00:02:22,410 --> 00:02:24,510
So let's take this off and we'll find.

35
00:02:24,600 --> 00:02:32,100
Okay, now we just run this, we run this binary cross entropy, we run this matrix not defined.

36
00:02:32,100 --> 00:02:36,420
Let's run the cell or on the cell and then finally run the training.

37
00:02:36,420 --> 00:02:37,830
We get this error.

38
00:02:37,860 --> 00:02:42,570
This function takes zero positional arguments back to a given.

39
00:02:42,570 --> 00:02:44,760
So we have to have this.

40
00:02:44,760 --> 00:02:45,450
Here's why.

41
00:02:45,480 --> 00:02:51,660
True and why predicted okay, we run that and run that.

42
00:02:51,660 --> 00:02:58,080
We could see here how this training process looks similar to what we've had already when we're dealing

43
00:02:58,080 --> 00:03:02,670
with a preconceived binary cross entropy loss.

44
00:03:02,970 --> 00:03:08,640
Now, that said, let's suppose that we are going to pass in a parameter here.

45
00:03:08,640 --> 00:03:16,140
So one apartment tries the way in which we calculate this binary cross entropy that is one to have this

46
00:03:16,140 --> 00:03:19,290
binary across entropy multiplied by a given factor.

47
00:03:19,290 --> 00:03:23,360
Let's say one multiply this by a factor of, say, 0.5.

48
00:03:23,370 --> 00:03:30,680
In that case, we could have this here as a factor and then we'll pass in this factor right here.

49
00:03:30,690 --> 00:03:32,340
So we pass this in.

50
00:03:32,340 --> 00:03:36,540
Let's top the training and then we train to see how that works.

51
00:03:36,930 --> 00:03:41,730
We run this here and run this factor not defined.

52
00:03:41,730 --> 00:03:44,910
We could include this factor here.

53
00:03:44,910 --> 00:03:47,400
We run it factor still not defined.

54
00:03:47,400 --> 00:03:54,210
And this brings us to the way in which we define models or rather defined loss functions in which we

55
00:03:54,210 --> 00:03:55,800
have a given parameter.

56
00:03:55,800 --> 00:04:03,690
So right here we're going to define a loss here, a loss function, and then we pass in a y true y pred

57
00:04:03,690 --> 00:04:07,980
So it's going to be like similar to what we had with a custom B.C.E. without a factor.

58
00:04:07,980 --> 00:04:09,180
We have that.

59
00:04:09,180 --> 00:04:15,930
And then in here we have this BCI and then we return this right here.

60
00:04:15,960 --> 00:04:19,410
Now notice how we take this to off.

61
00:04:19,410 --> 00:04:23,730
So we haven't just this factor and that's what we're going to be passing here.

62
00:04:23,730 --> 00:04:29,820
And then here what we return is this loss method right here.

63
00:04:29,820 --> 00:04:33,630
So we define this custom binary cross entropy.

64
00:04:33,630 --> 00:04:39,870
We return this last method, and this last method we take in the Y through the white print, and we

65
00:04:39,870 --> 00:04:43,170
carry out the computation as we had done before.

66
00:04:43,170 --> 00:04:46,290
But now note that we multiplying this by a given factor.

67
00:04:46,290 --> 00:04:48,120
So here we could define our factor.

68
00:04:48,120 --> 00:04:52,080
Let's have this factor and your factor.

69
00:04:52,200 --> 00:04:55,850
Then before this, we'll define this factor.

70
00:04:55,860 --> 00:04:59,730
Let's take this factor to be equal one, because knock is actually equal one.

71
00:05:00,280 --> 00:05:05,320
Supposing you are having a different problem, you may want to modify this parameter as you wish.

72
00:05:05,350 --> 00:05:07,420
Let's have this factor here.

73
00:05:07,450 --> 00:05:08,350
That's fine.

74
00:05:08,380 --> 00:05:11,520
Now let's rerun this and hopefully we should have no error.

75
00:05:11,530 --> 00:05:12,070
So that's it?

76
00:05:12,070 --> 00:05:13,070
We have no error.

77
00:05:13,090 --> 00:05:14,200
That seems fine.

78
00:05:14,200 --> 00:05:16,030
And then we run our training.

79
00:05:16,040 --> 00:05:22,540
As you can see, the training process continues normally and we could now check out on another method

80
00:05:22,540 --> 00:05:31,900
of building custom loss functions, which is that of actually building a last class which inherits from

81
00:05:31,990 --> 00:05:34,270
the crash loss class.

82
00:05:34,270 --> 00:05:43,270
So that said, let's add this year and then we have our class, we'll call this custom B.C.E., and

83
00:05:43,270 --> 00:05:49,900
then we'll inherit from crash losses loss.

84
00:05:50,560 --> 00:05:51,970
Okay, so that's it.

85
00:05:52,000 --> 00:05:58,210
Now, once we have this right, we have our init method and just like we did here, we're going to pass

86
00:05:58,210 --> 00:05:58,930
in a factor.

87
00:05:58,930 --> 00:06:00,480
So let's have this factor.

88
00:06:00,490 --> 00:06:03,330
Then we inherit methods from the parent class.

89
00:06:03,340 --> 00:06:04,180
There we go.

90
00:06:04,180 --> 00:06:10,540
We have our custom B.C.E. custom BCE and that's it.

91
00:06:11,020 --> 00:06:19,120
So we have this right, and then we now go straight forward to creating this factor variable and takes

92
00:06:19,150 --> 00:06:23,160
this value factor and that's it for the init method.

93
00:06:23,170 --> 00:06:28,270
From here we're going to define a call method where this operations are going to be carried out.

94
00:06:28,300 --> 00:06:33,790
Now here we have this call method and then what we take in is why true and then why Pred.

95
00:06:34,210 --> 00:06:35,440
So we've taken this.

96
00:06:35,440 --> 00:06:39,280
We now do exactly the same thing we did here.

97
00:06:39,620 --> 00:06:42,940
We could copy this out and paste it right here.

98
00:06:42,940 --> 00:06:50,200
We have our b.c.e defined operation carried out, and here we have self factor.

99
00:06:50,740 --> 00:06:53,500
Okay, so everything looks fine.

100
00:06:53,500 --> 00:06:58,000
We have our custom b.c.e defined invalid syntax.

101
00:06:58,150 --> 00:07:00,250
There should be a dot just right here.

102
00:07:00,250 --> 00:07:01,270
We run that again.

103
00:07:01,270 --> 00:07:02,080
That's fine.

104
00:07:02,590 --> 00:07:03,790
So we have that right.

105
00:07:03,790 --> 00:07:06,880
And what we do now is take this off.

106
00:07:06,880 --> 00:07:14,460
So here we have custom b.c.e, which is this class where defined all we just defined right here.

107
00:07:14,470 --> 00:07:14,950
Okay.

108
00:07:14,950 --> 00:07:18,160
Now we should also note that you could pass in the factor here.

109
00:07:18,160 --> 00:07:20,020
We actually have to pass on the factor.

110
00:07:20,020 --> 00:07:20,620
That's it.

111
00:07:20,620 --> 00:07:25,000
Because we didn't we didn't specify a default value, so we have to pass that.

112
00:07:25,030 --> 00:07:26,820
Now let's run this.

113
00:07:26,830 --> 00:07:27,640
That's fine.

114
00:07:27,640 --> 00:07:31,120
And then we rerun our training and make sure everything works well.

115
00:07:31,120 --> 00:07:32,230
We get in this error.

116
00:07:32,260 --> 00:07:35,470
The call takes two arguments, but three were given.

117
00:07:35,470 --> 00:07:40,000
Let's get back to this and we see this error We do not put in the self.

118
00:07:40,270 --> 00:07:40,840
So that's it.

119
00:07:40,840 --> 00:07:43,170
And let's rerun this.

120
00:07:43,180 --> 00:07:46,080
As you can see, the training went on successfully.

121
00:07:46,090 --> 00:07:51,070
Now we know how to build custom loss functions with TensorFlow.

122
00:07:51,070 --> 00:07:57,970
We then move on to building this custom matrix just similar to the way we did with a loss.

123
00:07:57,970 --> 00:08:01,570
So here we have a custom metric we'll start with a custom method.

124
00:08:01,570 --> 00:08:07,240
So we take this off and then just right here we have custom metric.

125
00:08:07,240 --> 00:08:09,220
Let's add this and this.

126
00:08:09,220 --> 00:08:13,630
We paste this out here and have custom accuracy.

127
00:08:13,630 --> 00:08:18,220
We now get back to the documentation, TensorFlow crest and then metrics.

128
00:08:18,220 --> 00:08:22,570
So right here you would have this accuracy which has been defined.

129
00:08:22,570 --> 00:08:24,970
Here we go, we have binary accuracy.

130
00:08:24,970 --> 00:08:25,660
So that's it.

131
00:08:25,660 --> 00:08:26,980
We have the binary accuracy.

132
00:08:26,980 --> 00:08:28,000
We specify why.

133
00:08:28,000 --> 00:08:30,670
True, why pride and the threshold.

134
00:08:30,670 --> 00:08:31,330
There we go.

135
00:08:31,330 --> 00:08:33,370
We can paste this out here.

136
00:08:33,370 --> 00:08:34,690
This is the loss.

137
00:08:35,140 --> 00:08:37,150
Let's go back up to this metric.

138
00:08:37,480 --> 00:08:38,470
Let's pick it out here.

139
00:08:38,470 --> 00:08:42,280
So now we have this accuracy which takes in the white row and white bread.

140
00:08:42,280 --> 00:08:43,870
So we just return.

141
00:08:44,290 --> 00:08:45,400
We basically return this.

142
00:08:45,430 --> 00:08:52,030
We return binary accuracy accuracy, which takes in why true and why pride.

143
00:08:52,780 --> 00:08:56,050
Okay, so let's take this off and that looks fine.

144
00:08:56,050 --> 00:08:58,360
Now we have our custom accuracy.

145
00:08:58,360 --> 00:08:59,530
Custom accuracy.

146
00:08:59,530 --> 00:09:02,080
We run this as well.

147
00:09:02,080 --> 00:09:04,870
We get to this compile custom accuracy.

148
00:09:04,870 --> 00:09:05,410
Fine.

149
00:09:05,410 --> 00:09:08,530
We compile a model and we train the model.

150
00:09:08,530 --> 00:09:09,640
Everything looks good.

151
00:09:09,640 --> 00:09:11,650
You can see this custom accuracy.

152
00:09:11,650 --> 00:09:13,840
Now, let's suppose we had a factor.

153
00:09:13,840 --> 00:09:20,200
We are going to use this same kind of approach we had with a loss method, so we just simply copy this

154
00:09:20,200 --> 00:09:20,620
out.

155
00:09:20,620 --> 00:09:28,000
And then right here we have this paste stat, then we have our custom metric right here.

156
00:09:28,000 --> 00:09:31,660
Here again, we have custom accuracy, custom accuracy.

157
00:09:31,660 --> 00:09:36,310
We take a factor, we have this metric which changes to metric.

158
00:09:36,310 --> 00:09:37,120
There we go.

159
00:09:37,120 --> 00:09:38,980
And then we simply have this.

160
00:09:38,980 --> 00:09:43,210
So let's just copy this out and paste right here.

161
00:09:43,210 --> 00:09:47,170
So here we have this your let's copy this out.

162
00:09:47,170 --> 00:09:51,700
Okay, so we have this and we now multiply this by the factor.

163
00:09:51,700 --> 00:09:55,240
So yeah, again, we call the metric method and that's fine.

164
00:09:55,240 --> 00:09:56,020
We run that.

165
00:09:56,020 --> 00:09:57,670
We have this custom accuracy.

166
00:09:58,150 --> 00:09:59,710
We haven't stopped this training year.

167
00:09:59,960 --> 00:10:01,630
So let's stop this training.

168
00:10:01,640 --> 00:10:02,270
That's fine.

169
00:10:02,270 --> 00:10:04,880
We get back here and run.

170
00:10:04,880 --> 00:10:06,380
This looks good.

171
00:10:07,070 --> 00:10:13,070
Compiled and training model where all this takes one positional argument but to a given.

172
00:10:13,070 --> 00:10:17,060
So let's get back to this and then include this factor on that.

173
00:10:17,060 --> 00:10:18,140
And the training looks fine.

174
00:10:18,140 --> 00:10:19,900
We can see our metric right here.

175
00:10:19,910 --> 00:10:25,220
Our next approach will be the class based approach similar to what we did with a loss.

176
00:10:25,220 --> 00:10:27,770
So here we have this custom loss class.

177
00:10:27,770 --> 00:10:32,900
We'll build a custom metric class, let's add this and add this code.

178
00:10:32,900 --> 00:10:36,200
So we have that and then we have our custom metric class.

179
00:10:36,200 --> 00:10:40,100
This class here is a subclass of the class metric.

180
00:10:40,580 --> 00:10:42,320
We have our class metric.

181
00:10:42,320 --> 00:10:47,750
And then just right here we have custom accuracy.

182
00:10:47,750 --> 00:10:52,910
So we have this custom accuracy and then we'll specify the name.

183
00:10:53,210 --> 00:10:54,950
Let's have this name.

184
00:10:54,980 --> 00:10:58,460
Let's say we want to put the custom accuracy.

185
00:10:58,970 --> 00:10:59,600
Okay?

186
00:10:59,600 --> 00:11:04,280
And then we pass the factor to which by default was set to one.

187
00:11:04,280 --> 00:11:04,940
So that's it.

188
00:11:04,940 --> 00:11:08,690
Now we have custom accuracy, we have our factor.

189
00:11:08,900 --> 00:11:17,270
And then what we do is define this accuracy by calling on the add weight method, which comes with this

190
00:11:17,270 --> 00:11:19,340
metric class right here.

191
00:11:19,340 --> 00:11:23,720
So we have this add weight and then we'll specify the name, which is the name we've passed.

192
00:11:23,720 --> 00:11:27,870
We have the initial user equals zeros.

193
00:11:27,890 --> 00:11:28,880
So that's it.

194
00:11:29,210 --> 00:11:35,390
You'll notice that this will look slightly different from what we saw with a custom loss, as unlike

195
00:11:35,390 --> 00:11:38,090
with the last class which has this call method.

196
00:11:38,090 --> 00:11:38,600
Yeah.

197
00:11:38,600 --> 00:11:42,290
We would instead use three other different methods.

198
00:11:42,410 --> 00:11:48,800
The first will be looking at will be the update state method we have update state.

199
00:11:48,830 --> 00:11:54,590
Then next we will have result method and then the reset method.

200
00:11:54,890 --> 00:11:57,230
So we have this reset right here.

201
00:11:57,230 --> 00:12:05,840
The update state method here permits us, update our metrics, state the results, permits us to output

202
00:12:05,840 --> 00:12:11,900
the metric values, and then the reset permits us reset the metric states.

203
00:12:11,900 --> 00:12:14,420
So we have here reset states.

204
00:12:14,630 --> 00:12:15,440
Here we go.

205
00:12:15,440 --> 00:12:16,370
We start with us.

206
00:12:16,370 --> 00:12:17,930
Update State method.

207
00:12:17,930 --> 00:12:22,790
In this update state, we're going to be assigning a value to this accuracy variable.

208
00:12:22,790 --> 00:12:25,610
So just right here we have several accuracy.

209
00:12:25,610 --> 00:12:28,150
We assign a given value.

210
00:12:28,160 --> 00:12:33,890
Now the value we're going to assign is exactly the same we had here with this binary accuracy.

211
00:12:33,890 --> 00:12:40,160
So let's take this factor and then paste out your we paste it here.

212
00:12:40,610 --> 00:12:43,520
So this is our self dot factor.

213
00:12:43,760 --> 00:12:44,510
That's fine.

214
00:12:44,510 --> 00:12:45,230
So that's it.

215
00:12:45,230 --> 00:12:56,120
We assign this value to this accuracy variable, this method to takes in the Y, true, the y print,

216
00:12:56,240 --> 00:13:00,110
and then with the result we have a return.

217
00:13:00,110 --> 00:13:04,340
So here we have this return self, not accuracy.

218
00:13:04,430 --> 00:13:05,390
That's it.

219
00:13:05,390 --> 00:13:09,350
And then here for the reset states reset states.

220
00:13:09,350 --> 00:13:12,980
We are going to reset the state at the end of each epoch.

221
00:13:12,980 --> 00:13:20,120
So once we finish with one epoch, we reset the state and then re compute re update the state and then

222
00:13:20,120 --> 00:13:21,320
output the result.

223
00:13:21,320 --> 00:13:26,990
So here again we have self the accuracy, assign the value zero.

224
00:13:26,990 --> 00:13:28,970
That's fine, let's run the cell.

225
00:13:28,970 --> 00:13:32,150
We get this error because this is actually metrics.

226
00:13:32,150 --> 00:13:33,860
So let's take this off.

227
00:13:33,860 --> 00:13:37,010
We have your metrics, we run that again.

228
00:13:37,010 --> 00:13:38,030
This should be fine.

229
00:13:38,240 --> 00:13:40,940
Okay, so we have our custom accuracy built.

230
00:13:40,940 --> 00:13:44,150
We now get into this year.

231
00:13:44,150 --> 00:13:49,850
So let's take this off now and specify our custom accuracy.

232
00:13:49,850 --> 00:13:51,770
Then here we specify the name.

233
00:13:52,400 --> 00:13:55,970
But anyway, we've gotten default value, so we could just pass it this way.

234
00:13:56,810 --> 00:14:01,580
Let's run the cell and then we start the training again.

235
00:14:01,580 --> 00:14:03,440
We get this error of the state.

236
00:14:03,440 --> 00:14:05,750
Got an unexpected keyword argument sample.

237
00:14:05,750 --> 00:14:14,150
Wait, now we get in this error because your we didn't put the sample wait as one of our arguments.

238
00:14:14,150 --> 00:14:15,740
So yeah, we should have the sample.

239
00:14:15,740 --> 00:14:18,950
Wait, we're not going to be using that.

240
00:14:18,950 --> 00:14:20,630
So we just set it to unknown.

241
00:14:20,630 --> 00:14:21,980
Let's run this again.

242
00:14:22,100 --> 00:14:23,030
Fine.

243
00:14:23,030 --> 00:14:27,440
We recompile and then train our model.

244
00:14:27,740 --> 00:14:29,120
We get this other error.

245
00:14:29,150 --> 00:14:37,730
The input y of equal OP has type flow 32 that doesn't match type 64 of argument X.

246
00:14:37,730 --> 00:14:45,470
So this is actually an error which comes up because right here when competing this binary accuracy,

247
00:14:45,800 --> 00:14:48,680
we have y true and y pair of different data types.

248
00:14:48,680 --> 00:14:52,280
So let's print this out and you could see that y true.

249
00:14:52,280 --> 00:14:59,630
And then y pred we run this again looks.

250
00:14:59,680 --> 00:15:00,250
Fine.

251
00:15:01,120 --> 00:15:08,770
We get down to this year, this compilation recall or when we want to debug, we could run eagerly.

252
00:15:08,770 --> 00:15:12,310
So let's run eagerly set out to true.

253
00:15:12,310 --> 00:15:13,510
And that's fine.

254
00:15:13,510 --> 00:15:16,960
We run this again and try to understand what's going on.

255
00:15:17,860 --> 00:15:20,690
So as you could see, that's exactly what we expected.

256
00:15:20,710 --> 00:15:23,500
We have this in 64.

257
00:15:23,500 --> 00:15:25,960
So this is an inch and this is a float 32.

258
00:15:26,110 --> 00:15:27,160
We have the Y.

259
00:15:27,160 --> 00:15:27,750
True.

260
00:15:27,760 --> 00:15:30,250
So let's we could cast this year.

261
00:15:30,250 --> 00:15:39,850
So let's cast this value typecast and then we'll specify the D type to float 30 to float 32.

262
00:15:39,880 --> 00:15:40,870
That's fine.

263
00:15:40,870 --> 00:15:43,780
And then just here, we take this off.

264
00:15:44,260 --> 00:15:45,700
So we run this again.

265
00:15:46,360 --> 00:15:47,260
There we go.

266
00:15:47,260 --> 00:15:49,810
We now compile and then train.

267
00:15:49,810 --> 00:15:55,150
We get this other arrow kind of assign value to variable custom accuracy.

268
00:15:55,150 --> 00:15:59,260
The variable shape and the assigned value shape aren't compatible.

269
00:15:59,380 --> 00:16:07,960
We get back to this year and what we do is we're going to print out this output which we assign to the

270
00:16:07,960 --> 00:16:08,800
accuracy.

271
00:16:09,010 --> 00:16:17,440
Yeah, we define this output and then we simply take this out from your and have this as our output.

272
00:16:17,440 --> 00:16:20,380
And now we're going to assign this output.

273
00:16:20,500 --> 00:16:25,150
So since we now have the output, we could print it out so we understand exactly what we're assigning

274
00:16:25,150 --> 00:16:26,440
to the accuracy.

275
00:16:26,590 --> 00:16:29,110
Let's run this cell, that's fine.

276
00:16:29,380 --> 00:16:34,570
And then we get to this compile and train the model.

277
00:16:35,560 --> 00:16:36,640
Okay, There we go.

278
00:16:36,640 --> 00:16:39,760
So now we see exactly what we're assigning.

279
00:16:39,760 --> 00:16:47,080
But you'll notice that instead of just assigning a single value, we are assigning this list.

280
00:16:47,080 --> 00:16:52,000
And this is the reason why we were getting that error to solve this problem.

281
00:16:52,660 --> 00:17:01,450
We'll also have to understand that this output actually corresponds to whether the model's prediction

282
00:17:01,450 --> 00:17:05,620
was correct or not for each and every element of the batch.

283
00:17:05,620 --> 00:17:10,570
So when we have a one like this, it means the prediction was correct or the model's prediction was

284
00:17:10,570 --> 00:17:13,720
correct and one and so on and so forth.

285
00:17:13,720 --> 00:17:19,750
So because we've trained a model for quite a while, you see that for all this or for this particular

286
00:17:19,750 --> 00:17:25,290
batch, we have kind of like 100% accuracy.

287
00:17:25,330 --> 00:17:30,780
So instead of printed out 100%, what the model gives out is this list.

288
00:17:30,790 --> 00:17:32,860
Now let's look at this again.

289
00:17:33,250 --> 00:17:36,760
We see now that with this other batch, we have some zeros.

290
00:17:36,760 --> 00:17:40,300
So out of 32, we've had two zeros.

291
00:17:40,300 --> 00:17:46,000
And hence our accuracy in this case is 90% for this batch.

292
00:17:46,930 --> 00:17:54,910
That said, we have to figure out a way of counting the total number of ones and then dividing by the

293
00:17:54,910 --> 00:18:03,040
total all by this length, which in this case is 32 or by the batch size, which is 32, and then multiplying

294
00:18:03,040 --> 00:18:03,820
by 100.

295
00:18:03,820 --> 00:18:05,830
Or we could just let it like that.

296
00:18:06,220 --> 00:18:14,320
So we make use of this method count non zero where yeah, we could count the number of non zeros values

297
00:18:14,320 --> 00:18:17,500
very easily by just calling it and I'll be fine.

298
00:18:17,500 --> 00:18:26,770
So let's copy this here and then instead of having this output will count the number of non zeros.

299
00:18:27,730 --> 00:18:34,330
There we go, we count that and then we divide by the length of this output.

300
00:18:34,330 --> 00:18:39,490
So we divide by the length of the output, but this lens is going to be an inch.

301
00:18:39,490 --> 00:18:40,720
So we're going to cast this.

302
00:18:40,720 --> 00:18:47,650
So get a float, we have that and then Z type equal to flow 32 looks fine.

303
00:18:47,680 --> 00:18:52,900
Okay, so now we have that and then we could let's take off this output and then we run again.

304
00:18:54,070 --> 00:18:56,890
We then compile and then train the model.

305
00:18:56,890 --> 00:18:59,290
We still get another error.

306
00:18:59,290 --> 00:19:04,600
So what we'll do is we're going to just check in the documentation and then you're what do we see?

307
00:19:04,600 --> 00:19:06,940
We see that by default this is an end.

308
00:19:06,940 --> 00:19:09,820
So your we could also cast this.

309
00:19:09,820 --> 00:19:11,710
We cast this into a float.

310
00:19:11,710 --> 00:19:12,550
There we go.

311
00:19:12,550 --> 00:19:14,080
We have this output.

312
00:19:14,350 --> 00:19:17,530
We don't really need to cast that because we could specify the data type.

313
00:19:17,530 --> 00:19:23,500
So here we have the output and then the type equal to float 32.

314
00:19:23,530 --> 00:19:25,090
Okay, so that's it.

315
00:19:25,810 --> 00:19:26,980
Hopefully this should be okay.

316
00:19:26,980 --> 00:19:33,430
Now we check, we compile the model start of training and everything looks okay.

317
00:19:33,430 --> 00:19:40,390
Now we could now stop this and then get back to graph mode.

318
00:19:40,390 --> 00:19:43,990
So let's run again so that we could now train even faster.

319
00:19:43,990 --> 00:19:44,560
And that's it.

320
00:19:44,560 --> 00:19:45,340
For this section.

321
00:19:45,340 --> 00:19:48,640
We've seen how to build custom losses and metrics.

322
00:19:48,640 --> 00:19:51,760
Thank you for getting right up to this point and see you next time.
