Early stopping
In Chapter 6, Improving the Model, we introduced the concept of early stopping as an effective way of preventing overfitting. It does this by halting training when the model’s performance fails to improve over a defined number of epochs, as indicated in Figure 8.3. This way, we prevent our model from overfitting.
Figure 8.3 – A learning curve showing early stopping
Let’s recreate the same baseline model, but this time, we will apply a built-in callback to stop training when the validation accuracy fails to improve. We will use the same build and compile steps as in the first model and then add a callback when we fit the model:
#Fit the model # Add an early stopping callback callbacks = [tf.keras.callbacks.EarlyStopping( monitor="val_accuracy", patience=3, restore_best_weights=True)] history_2 = model_2.fit(train_data, epochs=20, ...