Bird
Raised Fist0
TensorFlowml~5 mins

Early stopping in TensorFlow

Choose your learning style10 modes available
Introduction

Early stopping helps stop training a model when it stops improving. This saves time and avoids making the model too focused on training data.

When training a model and you want to avoid overfitting.
When training takes a long time and you want to save resources.
When you want to automatically find the best number of training steps.
When you monitor validation performance to stop training early.
When you want to improve model generalization on new data.
Syntax
TensorFlow
tf.keras.callbacks.EarlyStopping(
    monitor='val_loss',
    min_delta=0.0,
    patience=0,
    verbose=0,
    mode='auto',
    baseline=None,
    restore_best_weights=False
)

monitor: What metric to watch (like validation loss).

patience: How many epochs to wait for improvement before stopping.

Examples
Stop training if validation loss does not improve for 3 epochs.
TensorFlow
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
Stop training if validation accuracy does not improve for 5 epochs.
TensorFlow
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, mode='max')
Stop early and restore the model weights from the best epoch.
TensorFlow
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)
Sample Model

This code trains a simple model on random data. It uses early stopping to stop if validation loss does not improve for 2 epochs. It also restores the best weights found during training.

TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models

# Create simple model
model = models.Sequential([
    layers.Dense(10, activation='relu', input_shape=(5,)),
    layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Generate dummy data
import numpy as np
x_train = np.random.random((100, 5))
y_train = np.random.randint(2, size=(100, 1))
x_val = np.random.random((20, 5))
y_val = np.random.randint(2, size=(20, 1))

# Setup early stopping
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)

# Train model with early stopping
history = model.fit(
    x_train, y_train,
    epochs=20,
    validation_data=(x_val, y_val),
    callbacks=[early_stop],
    verbose=2
)

# Print final epoch and best val_loss
final_epoch = len(history.history['loss'])
best_val_loss = min(history.history['val_loss'])
print(f"Training stopped after {final_epoch} epochs")
print(f"Best validation loss: {best_val_loss:.4f}")
OutputSuccess
Important Notes

Early stopping helps prevent overfitting by stopping training when the model stops improving on validation data.

Setting restore_best_weights=True returns the model to the best state found during training.

Choose monitor metric carefully based on your goal (loss or accuracy).

Summary

Early stopping stops training when validation performance stops improving.

It saves time and helps the model generalize better.

Use patience to control how long to wait before stopping.

Practice

(1/5)
1. What is the main purpose of using EarlyStopping in TensorFlow training?
easy
A. To increase the training time for better accuracy
B. To save the model weights after every epoch
C. To stop training when the model stops improving on validation data
D. To shuffle the training data before each epoch

Solution

  1. Step 1: Understand EarlyStopping purpose

    EarlyStopping monitors validation performance to decide when to stop training.
  2. Step 2: Identify correct stopping condition

    It stops training when validation metrics stop improving to avoid overfitting and save time.
  3. Final Answer:

    To stop training when the model stops improving on validation data -> Option C
  4. Quick Check:

    EarlyStopping stops training on no improvement [OK]
Hint: EarlyStopping stops training on validation no improvement [OK]
Common Mistakes:
  • Thinking it increases training time
  • Confusing it with model checkpoint saving
  • Assuming it shuffles data
2. Which of the following is the correct way to create an EarlyStopping callback in TensorFlow to monitor validation loss with patience of 3 epochs?
easy
A. tf.keras.callbacks.EarlyStopping(patience=3, monitor='loss')
B. tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
C. tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3)
D. tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience='3')

Solution

  1. Step 1: Check correct parameters for EarlyStopping

    EarlyStopping requires 'monitor' as metric name and 'patience' as integer.
  2. Step 2: Validate correct metric and patience type

    Monitoring 'val_loss' with patience=3 (integer) is correct syntax.
  3. Final Answer:

    tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3) -> Option B
  4. Quick Check:

    Correct monitor and integer patience [OK]
Hint: Use monitor='val_loss' and integer patience [OK]
Common Mistakes:
  • Using 'loss' instead of 'val_loss' for validation monitoring
  • Passing patience as string instead of integer
  • Monitoring wrong metric like 'val_accuracy' when loss is needed
3. Consider this code snippet using EarlyStopping:
callback = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2)
model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val), callbacks=[callback])
If validation accuracy improves at epochs 1 and 2, then stays the same at epochs 3 and 4, what will happen?
medium
A. Training stops after epoch 4
B. Training continues until epoch 10
C. Training stops after epoch 3
D. Training stops immediately after epoch 2

Solution

  1. Step 1: Understand patience behavior

    Patience=2 means training stops if no improvement for 2 consecutive epochs.
  2. Step 2: Analyze validation accuracy changes

    Improvement at epochs 1 and 2, then no improvement at 3 and 4 means 2 epochs without improvement.
  3. Final Answer:

    Training stops after epoch 4 -> Option A
  4. Quick Check:

    Patience 2 stops after 2 no-improve epochs [OK]
Hint: Patience counts epochs without improvement before stopping [OK]
Common Mistakes:
  • Stopping immediately after first no improvement
  • Ignoring patience and running all epochs
  • Stopping too early or too late
4. You wrote this code but EarlyStopping does not stop training early as expected:
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
model.fit(x_train, y_train, epochs=20, validation_data=(x_val, y_val), callbacks=[callback])
What is the most likely reason?
medium
A. You forgot to include validation_data
B. Patience is set too low
C. EarlyStopping only works with accuracy metric
D. Validation loss keeps improving every epoch

Solution

  1. Step 1: Check EarlyStopping conditions

    EarlyStopping stops only if monitored metric stops improving for patience epochs.
  2. Step 2: Analyze why training never stops early

    If validation loss keeps improving, EarlyStopping will not trigger.
  3. Final Answer:

    Validation loss keeps improving every epoch -> Option D
  4. Quick Check:

    Improving metric prevents EarlyStopping trigger [OK]
Hint: EarlyStopping triggers only if metric stops improving [OK]
Common Mistakes:
  • Assuming EarlyStopping stops regardless of metric
  • Setting patience too high to see effect
  • Not providing validation data
5. You want to use EarlyStopping to stop training when validation accuracy stops improving, but only after waiting 5 epochs to avoid premature stopping. Which code snippet correctly implements this with TensorFlow?
hard
A. tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, mode='max')
B. tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, mode='min')
C. tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, mode='max')
D. tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, mode='min')

Solution

  1. Step 1: Identify correct metric and mode

    Validation accuracy improves when it increases, so mode='max' is needed.
  2. Step 2: Set patience and monitor correctly

    Patience=5 waits 5 epochs before stopping; monitor='val_accuracy' matches metric.
  3. Final Answer:

    tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, mode='max') -> Option A
  4. Quick Check:

    Accuracy needs mode='max' with patience=5 [OK]
Hint: Use mode='max' for accuracy, patience controls wait epochs [OK]
Common Mistakes:
  • Using mode='min' for accuracy metric
  • Monitoring val_loss instead of val_accuracy
  • Setting patience too low or forgetting mode
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard