Usage of callbacks

A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training. You can pass a list of callbacks (as the keyword argument callbacks) to the .fit() method of the Sequential or Model classes. The relevant methods of the callbacks will then be called at each stage of the training.

[source]

Callback

  1. keras.callbacks.callbacks.Callback()

Abstract base class used to build new callbacks.

Properties

  • params: dict. Training parameters (eg. verbosity, batch size, number of epochs…).
  • model: instance of keras.models.Model. Reference of the model being trained.

The logs dictionary that callback methodstake as argument will contain keys for quantities relevant tothe current batch or epoch.

Currently, the .fit() method of the Sequential model classwill include the following quantities in the logs thatit passes to its callbacks:

on_epoch_end: logs include acc and loss, andoptionally include val_loss(if validation is enabled in fit), and val_acc(if validation and accuracy monitoring are enabled).on_batch_begin: logs include size,the number of samples in the current batch.on_batch_end: logs include loss, and optionally acc(if accuracy monitoring is enabled).

[source]

BaseLogger

  1. keras.callbacks.callbacks.BaseLogger(stateful_metrics=None)

Callback that accumulates epoch averages of metrics.

This callback is automatically applied to every Keras model.

Arguments

  • stateful_metrics: Iterable of string names of metrics that should not be averaged over an epoch. Metrics in this list will be logged as-is in on_epoch_end. All others will be averaged in on_epoch_end.

[source]

TerminateOnNaN

  1. keras.callbacks.callbacks.TerminateOnNaN()

Callback that terminates training when a NaN loss is encountered.

[source]

ProgbarLogger

  1. keras.callbacks.callbacks.ProgbarLogger(count_mode='samples', stateful_metrics=None)

Callback that prints metrics to stdout.

Arguments

  • count_mode: One of "steps" or "samples". Whether the progress bar should count samples seen or steps (batches) seen.
  • stateful_metrics: Iterable of string names of metrics that should not be averaged over an epoch. Metrics in this list will be logged as-is. All others will be averaged over time (e.g. loss, etc).

Raises

  • ValueError: In case of invalid count_mode.

[source]

History

  1. keras.callbacks.callbacks.History()

Callback that records events into a History object.

This callback is automatically applied toevery Keras model. The History objectgets returned by the fit method of models.

[source]

ModelCheckpoint

  1. keras.callbacks.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)

Save the model after every epoch.

filepath can contain named formatting options,which will be filled with the values of epoch andkeys in logs (passed in on_epoch_end).

For example: if filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5,then the model checkpoints will be saved with the epoch number andthe validation loss in the filename.

Arguments

  • filepath: string, path to save the model file.
  • monitor: quantity to monitor.
  • verbose: verbosity mode, 0 or 1.
  • save_best_only: if save_best_only=True, the latest best model according to the quantity monitored will not be overwritten.
  • save_weights_only: if True, then only the model's weights will be saved (model.save_weights(filepath)), else the full model is saved (model.save(filepath)).
  • mode: one of {auto, min, max}. If save_best_only=True, the decision to overwrite the current save file is made based on either the maximization or the minimization of the monitored quantity. For val_acc, this should be max, for val_loss this should be min, etc. In auto mode, the direction is automatically inferred from the name of the monitored quantity.
  • period: Interval (number of epochs) between checkpoints.

[source]

EarlyStopping

  1. keras.callbacks.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False)

Stop training when a monitored quantity has stopped improving.

Arguments

  • monitor: quantity to be monitored.
  • min_delta: minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement.
  • patience: number of epochs that produced the monitored quantity with no improvement after which training will be stopped. Validation quantities may not be produced for every epoch, if the validation frequency (model.fit(validation_freq=5)) is greater than one.
  • verbose: verbosity mode.
  • mode: one of {auto, min, max}. In min mode, training will stop when the quantity monitored has stopped decreasing; in max mode it will stop when the quantity monitored has stopped increasing; in auto mode, the direction is automatically inferred from the name of the monitored quantity.
  • baseline: Baseline value for the monitored quantity to reach. Training will stop if the model doesn't show improvement over the baseline.
  • restore_best_weights: whether to restore model weights from the epoch with the best value of the monitored quantity. If False, the model weights obtained at the last step of training are used.

[source]

RemoteMonitor

  1. keras.callbacks.callbacks.RemoteMonitor(root='http://localhost:9000', path='/publish/epoch/end/', field='data', headers=None, send_as_json=False)

Callback used to stream events to a server.

Requires the requests library.Events are sent to root + '/publish/epoch/end/' by default. Calls areHTTP POST, with a data argument which is aJSON-encoded dictionary of event data.If send_as_json is set to True, the content type of the request will beapplication/json. Otherwise the serialized JSON will be send within a form

Arguments

  • root: String; root url of the target server.
  • path: String; path relative to root to which the events will be sent.
  • field: String; JSON field under which the data will be stored. The field is used only if the payload is sent within a form (i.e. send_as_json is set to False).
  • headers: Dictionary; optional custom HTTP headers.
  • send_as_json: Boolean; whether the request should be send as application/json.

[source]

LearningRateScheduler

  1. keras.callbacks.callbacks.LearningRateScheduler(schedule, verbose=0)

Learning rate scheduler.

Arguments

  • schedule: a function that takes an epoch index as input (integer, indexed from 0) and current learning rate and returns a new learning rate as output (float).
  • verbose: int. 0: quiet, 1: update messages.

[source]

ReduceLROnPlateau

  1. keras.callbacks.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)

Reduce learning rate when a metric has stopped improving.

Models often benefit from reducing the learning rate by a factorof 2-10 once learning stagnates. This callback monitors aquantity and if no improvement is seen for a 'patience' numberof epochs, the learning rate is reduced.

Example

  1. reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
  2. patience=5, min_lr=0.001)
  3. model.fit(X_train, Y_train, callbacks=[reduce_lr])

Arguments

  • monitor: quantity to be monitored.
  • factor: factor by which the learning rate will be reduced. new_lr = lr * factor
  • patience: number of epochs that produced the monitored quantity with no improvement after which training will be stopped. Validation quantities may not be produced for every epoch, if the validation frequency (model.fit(validation_freq=5)) is greater than one.
  • verbose: int. 0: quiet, 1: update messages.
  • mode: one of {auto, min, max}. In min mode, lr will be reduced when the quantity monitored has stopped decreasing; in max mode it will be reduced when the quantity monitored has stopped increasing; in auto mode, the direction is automatically inferred from the name of the monitored quantity.
  • min_delta: threshold for measuring the new optimum, to only focus on significant changes.
  • cooldown: number of epochs to wait before resuming normal operation after lr has been reduced.
  • min_lr: lower bound on the learning rate.

[source]

CSVLogger

  1. keras.callbacks.callbacks.CSVLogger(filename, separator=',', append=False)

Callback that streams epoch results to a csv file.

Supports all values that can be represented as a string,including 1D iterables such as np.ndarray.

Example

  1. csv_logger = CSVLogger('training.log')
  2. model.fit(X_train, Y_train, callbacks=[csv_logger])

Arguments

  • filename: filename of the csv file, e.g. 'run/log.csv'.
  • separator: string used to separate elements in the csv file.
  • append: True: append if file exists (useful for continuing training). False: overwrite existing file,

[source]

LambdaCallback

  1. keras.callbacks.callbacks.LambdaCallback(on_epoch_begin=None, on_epoch_end=None, on_batch_begin=None, on_batch_end=None, on_train_begin=None, on_train_end=None)

Callback for creating simple, custom callbacks on-the-fly.

This callback is constructed with anonymous functions that will be calledat the appropriate time. Note that the callbacks expects positionalarguments, as:

  • on_epoch_begin and on_epoch_end expect two positional arguments:epoch, logs
  • on_batch_begin and on_batch_end expect two positional arguments:batch, logs
  • on_train_begin and on_train_end expect one positional argument:logs

Arguments

  • on_epoch_begin: called at the beginning of every epoch.
  • on_epoch_end: called at the end of every epoch.
  • on_batch_begin: called at the beginning of every batch.
  • on_batch_end: called at the end of every batch.
  • on_train_begin: called at the beginning of model training.
  • on_train_end: called at the end of model training.

Example

  1. # Print the batch number at the beginning of every batch.
  2. batch_print_callback = LambdaCallback(
  3. on_batch_begin=lambda batch,logs: print(batch))
  4. # Stream the epoch loss to a file in JSON format. The file content
  5. # is not well-formed JSON but rather has a JSON object per line.
  6. import json
  7. json_log = open('loss_log.json', mode='wt', buffering=1)
  8. json_logging_callback = LambdaCallback(
  9. on_epoch_end=lambda epoch, logs: json_log.write(
  10. json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'),
  11. on_train_end=lambda logs: json_log.close()
  12. )
  13. # Terminate some processes after having finished model training.
  14. processes = ...
  15. cleanup_callback = LambdaCallback(
  16. on_train_end=lambda logs: [
  17. p.terminate() for p in processes if p.is_alive()])
  18. model.fit(...,
  19. callbacks=[batch_print_callback,
  20. json_logging_callback,
  21. cleanup_callback])

[source]

TensorBoard

  1. keras.callbacks.tensorboard_v1.TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=32, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None, embeddings_data=None, update_freq='epoch')

TensorBoard basic visualizations.

TensorBoardis a visualization tool provided with TensorFlow.

This callback writes a log for TensorBoard, which allowsyou to visualize dynamic graphs of your training and testmetrics, as well as activation histograms for the differentlayers in your model.

If you have installed TensorFlow with pip, you should be ableto launch TensorBoard from the command line:

  1. tensorboard --logdir=/full_path_to_your_logs

When using a backend other than TensorFlow, TensorBoard will still work(if you have TensorFlow installed), but the only feature available willbe the display of the losses and metrics plots.

Arguments

  • log_dir: the path of the directory where to save the log files to be parsed by TensorBoard.
  • histogram_freq: frequency (in epochs) at which to compute activation and weight histograms for the layers of the model. If set to 0, histograms won't be computed. Validation data (or split) must be specified for histogram visualizations.
  • batch_size: size of batch of inputs to feed to the network for histograms computation.
  • write_graph: whether to visualize the graph in TensorBoard. The log file can become quite large when write_graph is set to True.
  • write_grads: whether to visualize gradient histograms in TensorBoard. histogram_freq must be greater than 0.
  • write_images: whether to write model weights to visualize as image in TensorBoard.
  • embeddings_freq: frequency (in epochs) at which selected embedding layers will be saved. If set to 0, embeddings won't be computed. Data to be visualized in TensorBoard's Embedding tab must be passed as embeddings_data.
  • embeddings_layer_names: a list of names of layers to keep eye on. If None or empty list all the embedding layer will be watched.
  • embeddings_metadata: a dictionary which maps layer name to a file name in which metadata for this embedding layer is saved. See the details about metadata files format. In case if the same metadata file is used for all embedding layers, string can be passed.
  • embeddings_data: data to be embedded at layers specified in embeddings_layer_names. Numpy array (if the model has a single input) or list of Numpy arrays (if the model has multiple inputs). Learn more about embeddings.
  • update_freq: 'batch' or 'epoch' or integer. When using 'batch', writes the losses and metrics to TensorBoard after each batch. The same applies for 'epoch'. If using an integer, let's say 10000, the callback will write the metrics and losses to TensorBoard every 10000 samples. Note that writing too frequently to TensorBoard can slow down your training.

Create a callback

You can create a custom callback by extending the base class keras.callbacks.Callback. A callback has access to its associated model through the class property self.model.

Here's a simple example saving a list of losses over each batch during training:

  1. class LossHistory(keras.callbacks.Callback):
  2. def on_train_begin(self, logs={}):
  3. self.losses = []
  4. def on_batch_end(self, batch, logs={}):
  5. self.losses.append(logs.get('loss'))

Example: recording loss history

  1. class LossHistory(keras.callbacks.Callback):
  2. def on_train_begin(self, logs={}):
  3. self.losses = []
  4. def on_batch_end(self, batch, logs={}):
  5. self.losses.append(logs.get('loss'))
  6. model = Sequential()
  7. model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
  8. model.add(Activation('softmax'))
  9. model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
  10. history = LossHistory()
  11. model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
  12. print(history.losses)
  13. # outputs
  14. '''
  15. [0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
  16. '''

Example: model checkpoints

  1. from keras.callbacks import ModelCheckpoint
  2. model = Sequential()
  3. model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
  4. model.add(Activation('softmax'))
  5. model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
  6. '''
  7. saves the model weights after each epoch if the validation loss decreased
  8. '''
  9. checkpointer = ModelCheckpoint(filepath='/tmp/weights.hdf5', verbose=1, save_best_only=True)
  10. model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])