CheckpointManager#
- class braintools.trainer.CheckpointManager(dirpath, max_to_keep=5, filename_template=None, save_best_only=False, monitor=None, mode='min', verbose=True)#
Manager for saving and loading checkpoints.
Provides functionality for: - Saving checkpoints with automatic naming - Tracking and keeping only the best N checkpoints - Loading checkpoints by epoch, step, or best metric - Automatic cleanup of old checkpoints
- Parameters:
dirpath (
str) – Directory to save checkpoints.max_to_keep (
int) – Maximum number of checkpoints to keep. Set to -1 to keep all.filename_template (
str|None) – Template for checkpoint filenames. Available variables: {epoch}, {step}, {metric}, {timestamp}save_best_only (
bool) – Only save when the monitored metric improves.monitor (
str|None) – Metric to monitor for best checkpoint selection.mode (
str) – One of ‘min’ or ‘max’. In ‘min’ mode, lower is better.verbose (
bool) – Whether to print checkpoint operations.
Examples
>>> manager = CheckpointManager( ... 'checkpoints/', ... max_to_keep=3, ... monitor='val_loss', ... mode='min', ... ) >>> manager.save(model, optimizer, epoch=10, metrics={'val_loss': 0.5}) >>> state = manager.load_best()
- load(filepath=None, model=None, optimizer=None)[source]#
Load a checkpoint.
- Parameters:
- Returns:
Loaded checkpoint state.
- Return type:
Examples
>>> state = manager.load('checkpoint.ckpt', model=model)