Trainers¶
Trainers — orchestrate the epoch loop over training and validation runners.
A :class:Trainer owns the lifecycle: epoch loop, validation, LR
scheduling, early stopping. Like a :class:~srforge.training.runners.EpochRunner,
it exposes its lifecycle stages as :class:HookPoint attributes
(on_training_began, on_trainer_epoch_finished).
Hooks attached via Trainer(hooks=[…]) bind only to the Trainer
itself — they fire from the Trainer's own HookPoints and nowhere
else. If you want a hook to fire from a runner too, pass it to that
runner's hooks=[...] directly. Hook instances are plain objects,
so the same instance can appear in multiple lists.
Trainer
¶
Bases: ABC
Base trainer.
No longer inherits :class:~srforge.observers.Observable — the
legacy bus registration now happens directly from the concrete
trainer's __init__ via _init_hookpoints_and_register.
Subclasses declare lifecycle stages as :class:HookPoint-typed
class attributes. The base class harvests them at subclass-creation
time so __init__ can materialise one HookPoint per name on each
instance.
train(epochs: int, training_loader, validation_loader)
abstractmethod
¶
Run the training loop.
PyTorchTrainer
¶
Bases: Trainer
Orchestrates the training loop over training and validation runners.
The trainer owns the loss criteria and passes them to the runners at
call time via runner.run_epoch(criterion=...). It also manages
the LR scheduler, early stopping, and checkpoint restoration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Union[Model, Module]
|
The model to train. Any |
required |
training_epoch_runner
|
EpochRunner
|
Runner that executes one training epoch
(typically :class: |
required |
validation_epoch_runner
|
EpochRunner
|
Runner that executes one validation
epoch (typically
:class: |
required |
training_criterion
|
Union[Loss, LossScheduler]
|
Loss or :class: |
required |
validation_criterion
|
Union[Loss, LossScheduler]
|
Loss or :class: |
required |
lr_scheduler
|
LRScheduler
|
Any |
None
|
stop_condition
|
StopCondition
|
A :class: |
NoCondition()
|
hooks
|
List
|
Optional list of :class: |
None
|
event_bus
property
¶
The shared event bus for this training session.
Backward-compat shim — fetches the framework's singleton bus
from :class:GlobalSettings. New code should use HookPoints
directly rather than the bus.
restore(checkpoint: CheckpointState | None) -> None
¶
Apply a checkpoint to this trainer.
Sets initial_epoch and best_losses.
Runner state (optimizers, scalers) is restored via
resume_from_checkpoint before this method is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
CheckpointState | None
|
A :class: |
required |
sanity_check(training_loader: data.DataLoader, validation_loader: data.DataLoader) -> None
¶
Run one batch through training and validation to catch errors early.
Bypasses :meth:run_epoch (so no HookPoints fire and no observer
events are emitted) and runs a manual forward + loss pass on each
runner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
training_loader
|
DataLoader
|
Training data loader. |
required |
validation_loader
|
DataLoader
|
Validation data loader. |
required |