Bird
Raised Fist0
TensorFlowml~20 mins

Callbacks (EarlyStopping, ModelCheckpoint) in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this EarlyStopping callback configuration?
Consider the following code snippet for training a TensorFlow model with EarlyStopping callback:

early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
model.fit(x_train, y_train, epochs=20, validation_data=(x_val, y_val), callbacks=[early_stop])

What will happen if the validation loss does not improve for 4 consecutive epochs?
ATraining stops after 3 epochs without improvement, and model weights are restored to the best epoch.
BTraining continues for all 20 epochs regardless of validation loss.
CTraining stops after 4 epochs without improvement, and model weights are restored to the best epoch.
DTraining stops immediately after the first epoch without improvement, and weights are not restored.
Attempts:
2 left
💡 Hint
Patience defines how many epochs to wait after last improvement before stopping.
Model Choice
intermediate
2:00remaining
Which ModelCheckpoint option saves the model only when validation accuracy improves?
You want to save your TensorFlow model only when the validation accuracy improves during training. Which ModelCheckpoint callback configuration achieves this?
Atf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_accuracy', save_best_only=False, mode='max')
Btf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_accuracy', save_best_only=True, mode='max')
Ctf.keras.callbacks.ModelCheckpoint('model.h5', monitor='accuracy', save_best_only=False)
Dtf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True, mode='min')
Attempts:
2 left
💡 Hint
You want to monitor validation accuracy and save only the best model.
Hyperparameter
advanced
2:00remaining
What is the effect of setting 'restore_best_weights=False' in EarlyStopping?
In the EarlyStopping callback, what happens if you set restore_best_weights=False and training stops due to no improvement?
AThe model weights remain as they were at the last epoch before stopping, not necessarily the best.
BThe model weights are reset to the initial random weights before training.
CThe model weights are restored to the best epoch's weights automatically.
DTraining restarts from the best epoch automatically.
Attempts:
2 left
💡 Hint
Consider what happens to weights if you do not restore best weights.
🔧 Debug
advanced
2:00remaining
Why does this ModelCheckpoint callback not save any model files?
Given this callback:

checkpoint = tf.keras.callbacks.ModelCheckpoint('best_model.h5', monitor='val_accuracy', save_best_only=True, mode='min')

and training where validation accuracy improves, why might no model files be saved?
ABecause monitor='val_accuracy' is not a valid metric name.
BBecause save_best_only=True disables saving any model files.
CBecause mode='min' expects validation accuracy to decrease, so no improvement is detected.
DBecause the file path 'best_model.h5' is invalid and causes silent failure.
Attempts:
2 left
💡 Hint
Think about whether validation accuracy should increase or decrease for improvement.
🧠 Conceptual
expert
2:00remaining
Why combine EarlyStopping with ModelCheckpoint in training?
What is the main advantage of using both EarlyStopping and ModelCheckpoint callbacks together during model training?
ABoth callbacks perform the same function, so using both doubles the saving frequency.
BEarlyStopping saves the model after every epoch, and ModelCheckpoint stops training when performance degrades.
CEarlyStopping increases training epochs, and ModelCheckpoint deletes old models automatically.
DEarlyStopping stops training early to avoid overfitting, while ModelCheckpoint saves the best model weights during training.
Attempts:
2 left
💡 Hint
Think about how each callback helps training and model saving.

Practice

(1/5)
1. What is the main purpose of the EarlyStopping callback in TensorFlow training?
easy
A. To increase the learning rate during training
B. To save the model weights after every epoch
C. To stop training when the model stops improving to save time
D. To shuffle the training data before each epoch

Solution

  1. Step 1: Understand EarlyStopping's role

    EarlyStopping monitors a metric like validation loss and stops training if no improvement occurs for a set number of epochs.
  2. Step 2: Compare options with EarlyStopping behavior

    Only To stop training when the model stops improving to save time describes stopping training to save time when no improvement happens.
  3. Final Answer:

    To stop training when the model stops improving to save time -> Option C
  4. Quick Check:

    EarlyStopping stops training early = C [OK]
Hint: EarlyStopping stops training early to save time [OK]
Common Mistakes:
  • Confusing EarlyStopping with saving models
  • Thinking EarlyStopping changes learning rate
  • Assuming EarlyStopping shuffles data
2. Which of the following is the correct way to create a ModelCheckpoint callback that saves only the best model based on validation accuracy?
easy
A. tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=False, monitor='accuracy')
B. tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True, monitor='val_accuracy')
C. tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_weights_only=True, monitor='val_loss')
D. tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True, monitor='loss')

Solution

  1. Step 1: Identify correct parameters for ModelCheckpoint

    To save only the best model, save_best_only=True is needed, and to monitor validation accuracy, monitor='val_accuracy' is correct.
  2. Step 2: Check options for matching parameters

    tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True, monitor='val_accuracy') matches these requirements exactly.
  3. Final Answer:

    tf.keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True, monitor='val_accuracy') -> Option B
  4. Quick Check:

    Best model saved by val_accuracy = A [OK]
Hint: Use save_best_only=True and monitor='val_accuracy' [OK]
Common Mistakes:
  • Using monitor='accuracy' instead of 'val_accuracy'
  • Setting save_best_only=False by mistake
  • Confusing save_weights_only with saving full model
3. Consider this code snippet using EarlyStopping:
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2)
model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val), callbacks=[callback])
If the validation loss stops improving after epoch 4, at which epoch will training stop?
medium
A. Epoch 4
B. Epoch 10
C. Epoch 5
D. Epoch 7

Solution

  1. Step 1: Understand patience parameter in EarlyStopping

    Patience=2 means training continues 2 more epochs after last improvement before stopping.
  2. Step 2: Calculate stopping epoch

    If last improvement is at epoch 4, training continues epochs 5 and 6, then stops before epoch 7 starts, so training stops at epoch 7.
  3. Final Answer:

    Epoch 7 -> Option D
  4. Quick Check:

    Patience 2 means stop 2 epochs after no improvement = B [OK]
Hint: Training stops after patience epochs without improvement [OK]
Common Mistakes:
  • Stopping immediately at last improvement epoch
  • Stopping one epoch too early or too late
  • Confusing patience with number of total epochs
4. You wrote this code but the model never stops early even when validation loss stops improving:
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 training does not stop early?
medium
A. The validation data is not passed correctly, so val_loss is not computed
B. Patience is too low to allow stopping
C. EarlyStopping requires save_best_only=True to work
D. The model.fit call is missing the callbacks argument

Solution

  1. Step 1: Check if validation data is correctly passed

    EarlyStopping monitors validation metrics, so if validation data is missing or incorrect, val_loss won't update and stopping won't trigger.
  2. Step 2: Evaluate other options

    Patience=3 is reasonable, save_best_only is unrelated to EarlyStopping, and callbacks argument is present.
  3. Final Answer:

    The validation data is not passed correctly, so val_loss is not computed -> Option A
  4. Quick Check:

    EarlyStopping needs valid val_loss metric = D [OK]
Hint: EarlyStopping needs valid validation data to monitor val_loss [OK]
Common Mistakes:
  • Confusing ModelCheckpoint's save_best_only with EarlyStopping
  • Ignoring validation_data argument
  • Setting patience too high and expecting early stop
5. You want to train a model and save the best weights based on validation accuracy, but also stop training early if validation accuracy does not improve for 4 epochs. Which callback setup is correct?
hard
A. [tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=4), tf.keras.callbacks.ModelCheckpoint('best.h5', save_best_only=True, monitor='val_accuracy')]
B. [tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=4), tf.keras.callbacks.ModelCheckpoint('best.h5', save_best_only=False, monitor='val_accuracy')]
C. [tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=4), tf.keras.callbacks.ModelCheckpoint('best.h5', save_best_only=True, monitor='loss')]
D. [tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2), tf.keras.callbacks.ModelCheckpoint('best.h5', save_best_only=True, monitor='val_accuracy')]

Solution

  1. Step 1: Match EarlyStopping parameters to requirement

    We want to stop if validation accuracy does not improve for 4 epochs, so monitor='val_accuracy' and patience=4 are correct.
  2. Step 2: Match ModelCheckpoint parameters

    We want to save best weights based on validation accuracy, so save_best_only=True and monitor='val_accuracy' are needed.
  3. Step 3: Check options for both callbacks

    Only [tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=4), tf.keras.callbacks.ModelCheckpoint('best.h5', save_best_only=True, monitor='val_accuracy')] has both callbacks correctly configured.
  4. Final Answer:

    [tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=4), tf.keras.callbacks.ModelCheckpoint('best.h5', save_best_only=True, monitor='val_accuracy')] -> Option A
  5. Quick Check:

    EarlyStopping and ModelCheckpoint monitor val_accuracy correctly = A [OK]
Hint: Match monitor and patience for both callbacks [OK]
Common Mistakes:
  • Using 'accuracy' instead of 'val_accuracy' for validation monitoring
  • Setting save_best_only=False when saving best model
  • Mismatching patience with requirement