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.
BaseLogger
keras.callbacks.BaseLogger()
Callback that accumulates epoch averages of metrics.
This callback is automatically applied to every Keras model.
Callback
keras.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 methods
take as argument will contain keys for quantities relevant to
the current batch or epoch.
Currently, the .fit()
method of the Sequential
model class
will include the following quantities in the logs
that
it passes to its callbacks:
- on_epoch_end: logs include
acc
andloss
, and optionally includeval_loss
(if validation is enabled infit
), andval_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 optionallyacc
(if accuracy monitoring is enabled).
ProgbarLogger
keras.callbacks.ProgbarLogger(count_mode='samples')
Callback that prints metrics to stdout.
Arguments
- count_mode: One of "steps" or "samples". Whether the progress bar should count samples seens or steps (batches) seen.
Raises
- ValueError: In case of invalid
count_mode
.
History
keras.callbacks.History()
Callback that records events into a History
object.
This callback is automatically applied to
every Keras model. The History
object
gets returned by the fit
method of models.
ModelCheckpoint
keras.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 the value of epoch
and
keys 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 and
the 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. - 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. Forval_acc
, this should bemax
, forval_loss
this should bemin
, etc. Inauto
mode, the direction is automatically inferred from the name of the monitored quantity. - 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)
). - period: Interval (number of epochs) between checkpoints.
EarlyStopping
keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto')
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 with no improvement after which training will be stopped.
- verbose: verbosity mode.
- mode: one of {auto, min, max}. In
min
mode, training will stop when the quantity monitored has stopped decreasing; inmax
mode it will stop when the quantity monitored has stopped increasing; inauto
mode, the direction is automatically inferred from the name of the monitored quantity.
RemoteMonitor
keras.callbacks.RemoteMonitor(root='http://localhost:9000', path='/publish/epoch/end/', field='data', headers=None)
Callback used to stream events to a server.
Requires the requests
library.
Events are sent to root + '/publish/epoch/end/'
by default. Calls are
HTTP POST, with a data
argument which is a
JSON-encoded dictionary of event data.
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.
- headers: Dictionary; optional custom HTTP headers.
Defaults to:
- `{'Accept': 'application/json',
- 'Content-Type': 'application/json'}`
LearningRateScheduler
keras.callbacks.LearningRateScheduler(schedule)
Learning rate scheduler.
Arguments
- schedule: a function that takes an epoch index as input (integer, indexed from 0) and returns a new learning rate as output (float).
TensorBoard
keras.callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=False)
Tensorboard basic visualizations.
This callback writes a log for TensorBoard, which allows you to visualize dynamic graphs of your training and test metrics, as well as activation histograms for the different layers in your model.
TensorBoard is a visualization tool provided with TensorFlow.
If you have installed TensorFlow with pip, you should be able to launch TensorBoard from the command line:
tensorboard --logdir=/full_path_to_your_logs
You can find more information about TensorBoard - __here.
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 histograms for the layers of the model. If set to 0, histograms won't be computed.
- write_graph: whether to visualize the graph in Tensorboard. The log file can become quite large when write_graph is set to True.
- write_images: whether to write model weights to visualize as image in Tensorboard.
ReduceLROnPlateau
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', epsilon=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 factor of 2-10 once learning stagnates. This callback monitors a quantity and if no improvement is seen for a 'patience' number of epochs, the learning rate is reduced.
Example
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=5, min_lr=0.001)
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 with no improvement after which learning rate will be reduced.
- 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; inmax
mode it will be reduced when the quantity monitored has stopped increasing; inauto
mode, the direction is automatically inferred from the name of the monitored quantity. - epsilon: 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.
CSVLogger
keras.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
csv_logger = CSVLogger('training.log')
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,
LambdaCallback
keras.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 called
at the appropriate time. Note that the callbacks expects positional
arguments, 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
# Print the batch number at the beginning of every batch.
batch_print_callback = LambdaCallback(
on_batch_begin=lambda batch,logs: print(batch))
# Plot the loss after every epoch.
import numpy as np
import matplotlib.pyplot as plt
plot_loss_callback = LambdaCallback(
on_epoch_end=lambda epoch, logs: plt.plot(np.arange(epoch),
logs['loss']))
# Terminate some processes after having finished model training.
processes = ...
cleanup_callback = LambdaCallback(
on_train_end=lambda logs: [
p.terminate() for p in processes if p.is_alive()])
model.fit(...,
callbacks=[batch_print_callback,
plot_loss_callback,
cleanup_callback])
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:
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
Example: recording loss history
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
model = Sequential()
model.add(Dense(10, input_dim=784, init='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
history = LossHistory()
model.fit(X_train, Y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
print history.losses
# outputs
'''
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
'''
Example: model checkpoints
from keras.callbacks import ModelCheckpoint
model = Sequential()
model.add(Dense(10, input_dim=784, init='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
'''
saves the model weights after each epoch if the validation loss decreased
'''
checkpointer = ModelCheckpoint(filepath="/tmp/weights.hdf5", verbose=1, save_best_only=True)
model.fit(X_train, Y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])