Callback#
- class composer.Callback(*args, **kwargs)[source]#
Base class for callbacks.
Callbacks provide hooks that can run at each training loop
Event
. A callback is similar to anAlgorithm
in that they are run on specific events, but it differs from anAlgorithm
in that it should not modify the training of the model. By convention, callbacks should not modify theState
. They are typically used to for non-essential recording functions such as logging or timing.Callbacks can be implemented in two ways:
Override the individual methods named for each
Event
.For example,
>>> class MyCallback(Callback): ... def epoch_start(self, state: State, logger: Logger): ... print(f'Epoch: {int(state.timestamp.epoch)}') >>> # construct trainer object with your callback >>> trainer = Trainer( ... model=model, ... train_dataloader=train_dataloader, ... eval_dataloader=eval_dataloader, ... optimizers=optimizer, ... max_duration="1ep", ... callbacks=[MyCallback()], ... ) >>> # trainer will run MyCallback whenever the EPOCH_START >>> # is triggered, like this: >>> _ = trainer.engine.run_event(Event.EPOCH_START) Epoch: 0
Override
run_event()
if you want a single method to handle all events. If this method is overridden, then the individual methods corresponding to each event name (such asepoch_start()
) will no longer be automatically invoked. For example, if you overriderun_event()
, thenepoch_start()
will not be called on theEvent.EPOCH_START
event,batch_start()
will not be called on theEvent.BATCH_START
, etc. However, you can invokeepoch_start()
,batch_start()
, etc. in your overriding implementation ofrun_event()
.For example,
>>> class MyCallback(Callback): ... def run_event(self, event: Event, state: State, logger: Logger): ... if event == Event.EPOCH_START: ... print(f'Epoch: {int(state.timestamp.epoch)}') >>> # construct trainer object with your callback >>> trainer = Trainer( ... model=model, ... train_dataloader=train_dataloader, ... eval_dataloader=eval_dataloader, ... optimizers=optimizer, ... max_duration="1ep", ... callbacks=[MyCallback()], ... ) >>> # trainer will run MyCallback whenever the EPOCH_START >>> # is triggered, like this: >>> _ = trainer.engine.run_event(Event.EPOCH_START) Epoch: 0
- after_backward(state, logger)[source]#
Called on the
Event.AFTER_BACKWARD
event.
- after_dataloader(state, logger)[source]#
Called on the
Event.AFTER_DATALOADER
event.
- after_forward(state, logger)[source]#
Called on the
Event.AFTER_FORWARD
event.
- after_load(state, logger)[source]#
Called on the
Event.AFTER_LOAD
event.
- after_loss(state, logger)[source]#
Called on the
Event.AFTER_LOSS
event.
- after_train_batch(state, logger)[source]#
Called on the
Event.AFTER_TRAIN_BATCH
event.
- batch_checkpoint(state, logger)[source]#
Called on the
Event.BATCH_CHECKPOINT
event.
- batch_end(state, logger)[source]#
Called on the
Event.BATCH_END
event.Note
The following
State.timestamp
member variables are incremented immediately before theEvent.BATCH_END
event.
- batch_start(state, logger)[source]#
Called on the
Event.BATCH_START
event.
- before_backward(state, logger)[source]#
Called on the
Event.BEFORE_BACKWARD
event.
- before_dataloader(state, logger)[source]#
Called on the
Event.BEFORE_DATALOADER
event.
- before_forward(state, logger)[source]#
Called on the
Event.BEFORE_FORWARD
event.
- before_load(state, logger)[source]#
Called on the
Event.BEFORE_LOAD
event.
- before_loss(state, logger)[source]#
Called on the
Event.BEFORE_LOSS
event.
- before_train_batch(state, logger)[source]#
Called on the
Event.BEFORE_TRAIN_BATCH
event.
- close(state, logger)[source]#
Called whenever the trainer finishes training, even when there is an exception.
It should be used for clean up tasks such as flushing I/O streams and/or closing any files that may have been opened during the
Event.INIT
event.
- epoch_checkpoint(state, logger)[source]#
Called on the
Event.EPOCH_CHECKPOINT
event.
- epoch_end(state, logger)[source]#
Called on the
Event.EPOCH_END
event.Note
The following
State.timestamp
member variables are incremented immediately before theEvent.EPOCH_END
event.
- epoch_start(state, logger)[source]#
Called on the
Event.EPOCH_START
event.
- eval_after_all(state, logger)[source]#
Called on the
Event.EVAL_AFTER_ALL
event.
- eval_after_forward(state, logger)[source]#
Called on the
Event.EVAL_AFTER_FORWARD
event.
- eval_batch_end(state, logger)[source]#
Called on the
Event.EVAL_BATCH_END
event.
- eval_batch_start(state, logger)[source]#
Called on the
Event.EVAL_BATCH_START
event.
- eval_before_all(state, logger)[source]#
Called on the
Event.EVAL_BEFORE_ALL
event.
- eval_end(state, logger)[source]#
Called on the
Event.EVAL_END
event.
- eval_standalone_end(state, logger)[source]#
Called on the
Event.EVAL_STANDALONE_END
event.
- eval_standalone_start(state, logger)[source]#
Called on the
Event.EVAL_STANDALONE_START
event.
- eval_start(state, logger)[source]#
Called on the
Event.EVAL_START
event.
- fit_end(state, logger)[source]#
Called on the
Event.FIT_END
event.
- fit_start(state, logger)[source]#
Called on the
Event.FIT_START
event.
- init(state, logger)[source]#
Called on the
Event.INIT
event.
- iteration_checkpoint(state, logger)[source]#
Called on the
Event.ITERATION_CHECKPOINT
event.
- iteration_end(state, logger)[source]#
Called on the
Event.ITERATION_END
event.Note
State.timestamp
member variableTimestamp.iteration
is incremented immediately beforeEvent.ITERATION_END
.
- iteration_start(state, logger)[source]#
Called on the
Event.ITERATION_START
event.
- post_close()[source]#
Called after
close()
has been invoked for each callback.Very few callbacks should need to implement
post_close()
. This callback can be used to back up any data that may have been written by other callbacks duringclose()
.