WEBVTT

00:00.120 --> 00:03.040
We created the model architecture.

00:03.200 --> 00:07.640
The input layer, the hidden layers and the output layer.

00:07.840 --> 00:11.560
Now let's train our model here.

00:11.880 --> 00:18.480
Create a new cell and let's set up early stopping to prevent overfitting.

00:18.760 --> 00:24.920
In the previous videos we learned how to use Model.fit function to train the model.

00:25.000 --> 00:32.000
But in this example we're going to use a new thing called early stopping.

00:32.000 --> 00:34.400
Also we're going to use Model.fit function.

00:34.400 --> 00:39.480
But I'm going to use something called early stopping.

00:39.640 --> 00:48.640
Early stopping equals to Tf.keras dot callback dot early stopping.

00:48.800 --> 00:52.440
Pass the monitor equals to val loss.

00:52.520 --> 00:58.160
Patients 20 and restore best weights equals to true.

00:58.280 --> 01:00.600
What does this code do?

01:00.640 --> 01:09.470
Early stopping automatically halts training when the model stops, improving on validation data.

01:09.630 --> 01:13.070
Preventing overfitting to the training set.

01:13.270 --> 01:25.070
So if you have large data and your model, for example, get trained with the first 200 epochs and you

01:25.070 --> 01:28.510
specified, for example, 500 epochs.

01:28.790 --> 01:36.310
Why you should make unnecessary 300 epochs and get the same results.

01:36.470 --> 01:42.910
So here the early stopping to prevent overfitting to the training set.

01:42.950 --> 01:51.270
When the model stops improving on validation data, it's very important we're going to depend on the

01:51.310 --> 01:53.230
Mae and MSE.

01:53.630 --> 01:57.870
We're going to see the results here in this video.

01:57.870 --> 02:08.670
And you should see how early stopping is important to prevent memory leakage, overfitting, um, resource

02:08.710 --> 02:10.350
heating and memory.

02:10.510 --> 02:16.270
Okay, so there are a lot of things we can prevent using the early stopping.

02:16.470 --> 02:16.990
Don't worry.

02:17.030 --> 02:23.910
We're going to show you, uh, to see all those in the next couple of minutes.

02:24.150 --> 02:24.990
Monitor.

02:25.390 --> 02:27.670
Tell us what to watch.

02:27.910 --> 02:31.510
Tracks the validation loss during the training.

02:31.510 --> 02:33.430
So go and see the value.

02:33.710 --> 02:34.790
Val loss.

02:34.910 --> 02:37.390
You can also use val accuracy.

02:37.430 --> 02:38.710
Loss accuracy.

02:38.710 --> 02:42.150
So you can you can specify accuracy.

02:42.190 --> 02:44.310
You can specify val accuracy.

02:44.310 --> 02:51.910
But for this example we're going to focus on val loss val loss is preferred for regression tasks.

02:51.950 --> 02:54.070
Patience equals to 20.

02:54.110 --> 02:55.110
How long to wait.

02:55.150 --> 02:58.990
Number of epochs with no improvement before stopping.

02:59.230 --> 03:04.780
And if the validation loss doesn't improve for 20 consecutive epochs.

03:04.820 --> 03:14.620
Training stops balanced to best weights and crucial feature reverts model weights to the apex with the

03:14.620 --> 03:15.660
best performance.

03:15.660 --> 03:22.460
Without this, you'd get weights from the final, possibly overfitted epoch.

03:22.700 --> 03:26.500
Ensures you get the optimal model from training.

03:26.620 --> 03:32.420
Now let's start training the model so history equals to model dot fit.

03:32.460 --> 03:38.940
We use this fit function to train the model we used in the previous videos and previous sections.

03:38.980 --> 03:42.620
Passing the exit train scaled.

03:42.620 --> 03:44.580
This is very important guys.

03:44.580 --> 03:52.660
So here we need to pass the scaled because we talked about feature scaling.

03:52.820 --> 04:00.860
And here x train scaled parameter is passed passed Y train apex 1000.

04:00.860 --> 04:03.860
You can make it 200 400, 300.

04:03.900 --> 04:07.660
I'll start 200 and see the parameters.

04:07.660 --> 04:10.020
If there is a stop early stop.

04:10.260 --> 04:14.060
So there is no need to use 1000.

04:14.100 --> 04:18.020
Okay, so let me start with 200 and see the results.

04:18.060 --> 04:20.220
Validation split 0.2.

04:20.260 --> 04:23.980
Batch size 32 callbacks early stopping.

04:23.980 --> 04:29.660
So I'm gonna use this early stopping with the defined parameters here.

04:29.820 --> 04:32.860
And set the verbose equals to one.

04:32.900 --> 04:37.060
Let's click run and start training our model.

04:37.060 --> 04:40.300
Don't worry we're going to analyze all the results.

04:40.300 --> 04:42.100
And there we go guys.

04:42.100 --> 04:43.700
We trained our model.

04:44.020 --> 04:48.820
But if you notice we didn't complete the 200 epochs.

04:49.100 --> 04:56.980
We stopped at 169 epoch okay so let me study those parameters.

04:57.020 --> 05:04.050
Loss and Ma are very low values is 7.15.

05:04.610 --> 05:09.450
Val e 1.9 and then Ms. 7.1.

05:09.690 --> 05:15.090
If we go up for the first epoch, scroll up.

05:15.250 --> 05:18.730
We see that those values were high.

05:19.410 --> 05:26.290
So as we go down, those values becomes very small become very small.

05:26.330 --> 05:26.850
Okay.

05:27.210 --> 05:37.770
So as we see they are decreasing until we get the same results and there's no improvement if we continue

05:37.930 --> 05:38.690
the epochs.

05:38.810 --> 05:43.530
So for this purpose we used the early stop.

05:43.570 --> 05:44.090
Okay.

05:44.450 --> 05:49.530
So go and train the model whenever you see there is no improvement.

05:49.810 --> 05:52.050
Stop this training.

05:52.090 --> 05:52.650
Okay.

05:52.690 --> 05:58.970
This is how we train the model and how we use the early stopping feature.
