Callbacks in training

class AbstractCallback

Abstract type of callback functions used in training.

class AbstractBatchCallback

Abstract type of callbacks to be called every mini-batch.

class AbstractEpochCallback

Abstract type of callbacks to be called every epoch.

every_n_batch(callback :: Function, n :: Int; call_on_0 = false)

A convenient function to construct a callback that runs every n mini-batches.

Parameters:call_on_0 (Int) – keyword argument, default false. Unless set, the callback will not be run on batch 0.

For example, the speedometer() callback is defined as

every_n_iter(frequency, call_on_0=true) do state :: OptimizationState
  if state.curr_batch == 0
    # reset timer
  else
    # compute and print speed
  end
end
Seealso:every_n_epoch(), speedometer().
speedometer(; frequency=50)

Create an AbstractBatchCallback that measure the training speed (number of samples processed per second) every k mini-batches.

Parameters:frequency (Int) – keyword argument, default 50. The frequency (number of min-batches) to measure and report the speed.
every_n_epoch(callback :: Function, n :: Int; call_on_0 = false)

A convenient function to construct a callback that runs every n full data-passes.

Parameters:call_on_0 (Int) – keyword argument, default false. Unless set, the callback will not be run on epoch 0. Epoch 0 means no training has been performed yet. This is useful if you want to inspect the randomly initialized model that has not seen any data yet.
Seealso:every_n_iter().
do_checkpoint(prefix; frequency=1, save_epoch_0=false)

Create an AbstractEpochCallback that save checkpoints of the model to disk. The checkpoints can be loaded back later on.

Parameters:
  • prefix (AbstractString) – the prefix of the filenames to save the model. The model architecture will be saved to prefix-symbol.json, while the weights will be saved to prefix-0012.params, for example, for the 12-th epoch.
  • frequency (Int) – keyword argument, default 1. The frequency (measured in epochs) to save checkpoints.
  • save_epoch_0 (Bool) – keyword argument, default false. Whether we should save a checkpoint for epoch 0 (model initialized but not seen any data yet).