1
00:00:00,000 --> 00:00:01,410
Let's take a look at the code for

2
00:00:01,410 --> 00:00:03,240
callbacks, and see how it works.

3
00:00:03,240 --> 00:00:05,055
You can see the code here.

4
00:00:06,055 --> 00:00:08,800
Here's the notebook with
the code already written,

5
00:00:10,800 --> 00:00:12,120
and here's the class that we

6
00:00:12,120 --> 00:00:13,800
defined to handle the callback,

7
00:00:13,800 --> 00:00:15,510
and here is where we instantiate

8
00:00:15,510 --> 00:00:17,340
the callback class itself.

9
00:00:20,940 --> 00:00:23,040
Finally, here's where we set up

10
00:00:23,040 --> 00:00:25,875
the callback to be called as
part of the training loop.

11
00:00:25,875 --> 00:00:27,380
As we begin training,

12
00:00:27,380 --> 00:00:30,180
note that we asked it to
train for five epochs.

13
00:00:30,180 --> 00:00:32,820
Now, keep an eye on
the losses that trains.

14
00:00:32,820 --> 00:00:35,745
We want to break when
it goes below 0.4,

15
00:00:35,745 --> 00:00:37,380
and by the end of the first epoch

16
00:00:37,380 --> 00:00:39,105
we're actually getting
close already.

17
00:00:39,105 --> 00:00:41,160
As the second epoch begins,

18
00:00:41,160 --> 00:00:43,710
it has already dropped below 0.4,

19
00:00:43,710 --> 00:00:45,465
but the callback
hasn't been hit yet.

20
00:00:45,465 --> 00:00:48,915
That's because we set
it up for on epoch end.

21
00:00:48,915 --> 00:00:50,635
It's good practice to do this,

22
00:00:50,635 --> 00:00:52,940
because with some data
and some algorithms,

23
00:00:52,940 --> 00:00:55,460
the loss may vary up and
down during the epoch,

24
00:00:55,460 --> 00:00:57,530
because all of the data
hasn't yet been processed.

25
00:00:57,530 --> 00:01:00,260
So, I like to wait for
the end to be sure.

26
00:01:00,260 --> 00:01:01,950
Now the epoch has ended,

27
00:01:02,000 --> 00:01:03,860
we can see that
the training ends,

28
00:01:03,860 --> 00:01:06,375
even though we've
only done two epochs.

29
00:01:06,375 --> 00:01:08,535
Note that we're sure we asked for

30
00:01:08,535 --> 00:01:11,030
five epochs and that
we ended after two,

31
00:01:11,030 --> 00:01:13,735
because the loss is below 0.4,

32
00:01:13,735 --> 00:01:14,905
which we checked for in

33
00:01:14,905 --> 00:01:17,885
the callback. It's
pretty cool right?