← Back to Python

All Topics

Advertisement

Learn/Python/Deep Learning

Keras Callbacks

Topic: Keras

Advertisement

Introduction

Callbacks provide hooks during training to monitor, stop, and save models at appropriate times.

Early Stopping

from tensorflow.keras.callbacks import EarlyStopping

early_stop = EarlyStopping(
    monitor='val_loss',
    patience=5,
    restore_best_weights=True,
    verbose=1
)

model.fit(x_train, y_train, epochs=50, callbacks=[early_stop])

Model Checkpoint

from tensorflow.keras.callbacks import ModelCheckpoint

checkpoint = ModelCheckpoint(
    'best_model.keras',
    monitor='val_accuracy',
    save_best_only=True,
    mode='max',
    verbose=1
)

model.fit(x_train, y_train, epochs=30, callbacks=[checkpoint])

TensorBoard

from tensorflow.keras.callbacks import TensorBoard

tensorboard = TensorBoard(
    log_dir='./logs',
    histogram_freq=1,
    write_graph=True,
    update_freq='epoch'
)

model.fit(x_train, y_train, epochs=10, callbacks=[tensorboard])

Custom Callback

class CustomCallback(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        if logs.get('accuracy') > 0.95:
            print(f"\nReached 95% accuracy at epoch {epoch}")
            self.model.stop_training = True

    def on_batch_end(self, batch, logs=None):
        print(f"\nBatch {batch}: loss = {logs.get('loss'):.4f}")

model.fit(x_train, y_train, epochs=10, callbacks=[CustomCallback()])

Learning Rate Scheduler

from tensorflow.keras.callbacks import LearningRateScheduler

def scheduler(epoch):
    if epoch < 10:
        return 0.001
    return 0.001 * 0.5 ** (epoch - 10)

lr_scheduler = LearningRateScheduler(scheduler, verbose=1)
model.fit(x_train, y_train, epochs=30, callbacks=[lr_scheduler])

Practice Problems

  1. Use EarlyStopping to prevent overfitting
  2. Save best model with ModelCheckpoint
  3. Monitor training with TensorBoard
  4. Create custom callback
  5. Implement learning rate decay

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →