1
00:00:00,000 --> 00:00:03,240
A question I often get at
this point from programmers in

2
00:00:03,240 --> 00:00:04,860
particular when
experimenting with

3
00:00:04,860 --> 00:00:06,975
different numbers of epochs is,

4
00:00:06,975 --> 00:00:08,820
How can I stop training when I

5
00:00:08,820 --> 00:00:10,755
reach a point that
I want to be at?

6
00:00:10,755 --> 00:00:12,720
What do I always
have to hard code it

7
00:00:12,720 --> 00:00:14,895
to go for certain
number of epochs?

8
00:00:14,895 --> 00:00:16,530
Well, the good news is that,

9
00:00:16,530 --> 00:00:18,870
the training loop does
support callbacks.

10
00:00:18,870 --> 00:00:20,265
So in every epoch,

11
00:00:20,265 --> 00:00:22,440
you can callback to
a code function,

12
00:00:22,440 --> 00:00:23,910
having checked the metrics.

13
00:00:23,910 --> 00:00:25,380
If they're what you want to say,

14
00:00:25,380 --> 00:00:26,730
then you can cancel
the training at

15
00:00:26,730 --> 00:00:29,265
that point. Let's take a look.

16
00:00:29,265 --> 00:00:32,190
Okay, so here's
our code for training

17
00:00:32,190 --> 00:00:35,235
the neural network to
recognize the fashion images.

18
00:00:35,235 --> 00:00:36,840
In particular, keep an eye on the

19
00:00:36,840 --> 00:00:40,020
model.fit function that
executes the training loop.

20
00:00:40,020 --> 00:00:42,210
You can see that here.

21
00:00:42,210 --> 00:00:44,410
What we'll now do is write

22
00:00:44,410 --> 00:00:47,005
a callback in Python.
Here's the code.

23
00:00:47,005 --> 00:00:48,900
It's implemented as
a separate class,

24
00:00:48,900 --> 00:00:50,905
but that can be in-line
with your other code.

25
00:00:50,905 --> 00:00:52,960
It doesn't need to be
in a separate file.

26
00:00:52,960 --> 00:00:56,200
In it, we'll implement
the on_epoch_end function,

27
00:00:56,200 --> 00:00:57,760
which gets called by the callback

28
00:00:57,760 --> 00:00:59,500
whenever the epoch ends.

29
00:00:59,500 --> 00:01:02,230
It also sends a logs object
which contains

30
00:01:02,230 --> 00:01:03,714
lots of great information

31
00:01:03,714 --> 00:01:05,815
about the current state
of training.

32
00:01:05,815 --> 00:01:09,610
For example, the current loss
is available in the logs,

33
00:01:09,610 --> 00:01:11,995
so we can query it
for certain amount.

34
00:01:11,995 --> 00:01:14,650
For example, here I'm checking
if the loss is less than

35
00:01:14,650 --> 00:01:18,395
0.4 and canceling
the training itself.

36
00:01:18,395 --> 00:01:20,625
Now that we have our callback,

37
00:01:20,625 --> 00:01:22,440
let's return to
the rest of the code,

38
00:01:22,440 --> 00:01:25,390
and there are two modifications
that we need to make.

39
00:01:25,390 --> 00:01:28,865
First, we instantiate the class
that we just created,

40
00:01:28,865 --> 00:01:30,610
we do that with this code.

41
00:01:30,610 --> 00:01:33,105
Then, in my model.fit,

42
00:01:33,105 --> 00:01:34,925
I used the callbacks parameter

43
00:01:34,925 --> 00:01:37,535
and pass it this instance
of the class.

44
00:01:37,535 --> 00:01:40,020
Let's see this in action.