Skip to content

lit_sb3.callbacks.base

source module lit_sb3.callbacks.base

Classes

source class BaseCallback(verbose: int = 0)

Bases : L.Callback, ABC

Base class for callback.

Inherits from lightning.Callback so instances can be passed directly to a lightning.Trainer via its callbacks argument. When training runs through :meth:BaseAlgorithm.learn the SB3 lifecycle methods (on_training_start / on_training_end) are called manually before Trainer.fit; the Lightning shims below detect that and skip to avoid double-calls. When a callback is passed directly to a Trainer without going through learn, the shims invoke the SB3 lifecycle instead.

Parameters

  • verbose : int — Verbosity level: 0 for no output, 1 for info messages, 2 for debug messages

Attributes

  • state_key : str — Identifier for the state of the callback.

Methods

source property BaseCallback.training_env: VecEnv

source property BaseCallback.summary_writer: Any

source method BaseCallback.init_callback(model: BaseAlgorithm) → None

Initialize the callback by saving references to the.

RL model and the training environment for convenience.

source method BaseCallback.on_training_start(locals_: dict[str, Any], globals_: dict[str, Any]) → None

source method BaseCallback.on_rollout_start() → None

source method BaseCallback.on_step() → bool

This method will be called by the model after each call to env.step().

For child callback (of an EventCallback), this will be called when the event is triggered.

Returns

  • bool — If the callback returns False, training is aborted early.

source method BaseCallback.on_training_end() → None

source method BaseCallback.on_rollout_end() → None

source method BaseCallback.update_locals(locals_: dict[str, Any]) → None

Update the references to the local variables.

Parameters

  • locals_ : dict[str, Any] — the local variables during rollout collection

source method BaseCallback.update_child_locals(locals_: dict[str, Any]) → None

Update the references to the local variables on sub callbacks.

Parameters

  • locals_ : dict[str, Any] — the local variables during rollout collection

source method BaseCallback.on_fit_start(trainer: L.Trainer, pl_module: L.LightningModule) → None

Lightning hook: called at the very beginning of Trainer.fit.

When invoked through :meth:BaseAlgorithm.learn, on_training_start has already been called manually, so this is a no-op. When a callback is passed directly to Trainer(callbacks=[cb]) this initialises the SB3 side of the callback instead.

source method BaseCallback.on_fit_end(trainer: L.Trainer, pl_module: L.LightningModule) → None

Lightning hook: called at the very end of Trainer.fit.

When invoked through :meth:BaseAlgorithm.learn, on_training_end has already been called by LightningModule.on_train_end, so this is a no-op. When a callback is passed directly to Trainer this tears down the SB3 side instead.

source class EventCallback(callback: BaseCallback | None = None, verbose: int = 0)

Bases : BaseCallback

Base class for triggering callback on event.

Parameters

  • callback : BaseCallback | None — Callback that will be called when an event is triggered.

  • verbose : int — Verbosity level: 0 for no output, 1 for info messages, 2 for debug messages

Attributes

  • state_key : str — Identifier for the state of the callback.

Methods

source method EventCallback.init_callback(model: BaseAlgorithm) → None

source method EventCallback.update_child_locals(locals_: dict[str, Any]) → None

Update the references to the local variables.

Parameters

  • locals_ : dict[str, Any] — the local variables during rollout collection

source class CallbackList(callbacks: list[BaseCallback])

Bases : BaseCallback

Class for chaining callbacks.

Parameters

  • callbacks : list[BaseCallback] — A list of callbacks that will be called sequentially.

Attributes

  • state_key : str — Identifier for the state of the callback.

Methods

source method CallbackList.update_child_locals(locals_: dict[str, Any]) → None

Update the references to the local variables.

Parameters

  • locals_ : dict[str, Any] — the local variables during rollout collection