C.W.K.
Stream
Lesson 05 of 06 · published

Callbacks — EarlyStopping, ModelCheckpoint, ReduceLROnPlateau

~11 min · callbacks, early-stopping, checkpoint

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

Hooks that change everything

Callbacks run at specific points during training — epoch start/end, batch start/end, training/validation begin/end. They let you control training without rewriting model.fit.

The standard recipe for almost any training run:

  • EarlyStopping(patience=10, restore_best_weights=True) — stop when val_loss stops improving, revert to best.
  • ModelCheckpoint(save_best_only=True) — save the best model snapshot.
  • ReduceLROnPlateau(factor=0.5, patience=5) — cut LR by half when training plateaus.
  • TensorBoard(log_dir=...) — log everything for later visualization.

Set epochs=500 and let these callbacks decide when to stop. You'll never tune the epoch count manually again.

Code

표준 callback 레시피·python
import datetime
from tensorflow import keras

callbacks = [
    keras.callbacks.EarlyStopping(
        monitor='val_loss',
        patience=10,
        min_delta=1e-4,
        restore_best_weights=True,
        start_from_epoch=5,    # don't stop in first 5 epochs (warm-up)
    ),
    keras.callbacks.ModelCheckpoint(
        filepath='checkpoints/best_{epoch:02d}_{val_accuracy:.3f}.keras',
        monitor='val_accuracy',
        save_best_only=True,
        mode='max',
    ),
    keras.callbacks.ReduceLROnPlateau(
        monitor='val_loss',
        factor=0.5,
        patience=5,
        min_delta=1e-4,
        cooldown=2,
        min_lr=1e-7,
    ),
    keras.callbacks.TensorBoard(
        log_dir="logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S"),
        histogram_freq=1,
    ),
]

model.fit(train_ds, validation_data=val_ds, epochs=500, callbacks=callbacks)

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.