Base callback¶
- class argus.callbacks.Callback[source]¶
Base callback class. All callbacks classes should inherit from this class.
A callback may execute actions on the start and the end of the whole training process, each epoch or iteration, as well as any other custom events.
The actions should be specified within corresponding methods that take the
argus.engine.State
as input:start
: triggered when the training is started.complete
: triggered when the training is completed.epoch_start
: triggered when an epoch is started.epoch_complete
: triggered when an epoch is ended.iteration_start
: triggered when an iteration is started.iteration_complete
: triggered when an iteration is ended.catch_exception
: triggered on catching of an exception.
Example
A simple custom callback which stops training after the specified time:
from time import time from argus.engine import State from argus.callbacks.callback import Callback class TimerCallback(Callback): """Stop training after the specified time. Args: time_limit (int): Time to run training in seconds. """ def __init__(self, time_limit: int): self.time_limit = time_limit self.start_time = 0 def start(self, state: State): self.start_time = time() def iteration_complete(self, state: State): if time() - self.start_time > self.time_limit: state.stopped = True state.logger.info(f"Run out of time {self.time_limit} sec, " f"{(state.epoch + 1) * (state.iteration + 1)} " f"iterations performed!")
You can find an example of creating custom events here.
- Raises
TypeError – Attribute is not callable.
- attach(engine: argus.engine.engine.Engine)[source]¶
Attach callback to the
argus.engine.Engine
.- Parameters
engine (Engine) – The engine to which the callback will be attached.
Decorator callbacks¶
- argus.callbacks.on_event(event: argus.engine.engine.EventEnum) Callable [source]¶
Decorator for creating a callback from a function. The function will be executed when the event is triggered. The function should take
argus.engine.State
as the first argument.- Parameters
event (EventEnum) – An event that will be associated with the function.
Example
import argus from argus.engine import Events, State @argus.callbacks.on_event(Events.START) def start_callback(state: State): state.logger.info("Start training!") model.fit(train_loader, val_loader=val_loader, callbacks=[start_callback])
- argus.callbacks.on_start(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the Events.START is triggered. The function should take
argus.engine.State
as the first argument.Example
import argus from argus.engine import State @argus.callbacks.on_start def start_callback(state: State): state.logger.info("Start training!") model.fit(train_loader, val_loader=val_loader, callbacks=[start_callback])
- argus.callbacks.on_complete(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the
Events.COMPLETE
is triggered. The function should takeargus.engine.State
as the first argument.
- argus.callbacks.on_epoch_start(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the
Events.EPOCH_START
is triggered. The function should takeargus.engine.State
as the first argument.
- argus.callbacks.on_epoch_complete(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the
Events.EPOCH_COMPLETE
is triggered. The function should takeargus.engine.State
as the first argument.
- argus.callbacks.on_iteration_start(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the
Events.ITERATION_START
is triggered. The function should takeargus.engine.State
as the first argument.
- argus.callbacks.on_iteration_complete(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the
Events.ITERATION_COMPLETE
is triggered. The function should takeargus.engine.State
as the first argument.
- argus.callbacks.on_catch_exception(func: Callable) argus.callbacks.callback.FunctionCallback [source]¶
Decorator for creating a callback from a function. The function will be executed when the
Events.CATCH_EXCEPTION
is triggered. The function should takeargus.engine.State
as the first argument.
- class argus.callbacks.FunctionCallback(event: argus.engine.engine.EventEnum, handler: Callable)[source]¶
Callback class for executing a single function.
- Parameters
event (EventEnum) – An event that will be associated with the handler.
handler (Callable) – A callable handler that will be executed on the event. The handler should take
argus.engine.State
as the first argument.
- attach(engine: argus.engine.engine.Engine, *args, **kwargs)[source]¶
Attach callback to the
argus.engine.Engine
.- Parameters
engine (Engine) – The engine to which the callback will be attached.
*args – optional args arguments to be passed to the handler.
**kwargs – optional kwargs arguments to be passed to the handler.