callcut.training.SaveBestModelCallbackπ
- class callcut.training.SaveBestModelCallback(save_path, monitor='val_f1', mode='max')[source]π
Callback that saves the best model weights during training.
Monitors a metric and saves the modelβs
state_dictwhen it improves. After training, load the best weights withtorch.load()andmodel.load_state_dict(), then usesave_pipeline()to save the full pipeline.- Parameters:
Methods
on_validation_epoch_end(trainer, pl_module)Check if model improved and save weights if so.
Examples
>>> from callcut.training import SaveBestModelCallback >>> import lightning as L >>> >>> trainer = L.Trainer( ... max_epochs=10, ... callbacks=[SaveBestModelCallback("best_weights.pt", monitor="val_f1")], ... ) >>> trainer.fit(module, datamodule=dm) >>> >>> # After training, load best weights and save full pipeline >>> model.load_state_dict(torch.load("best_weights.pt", weights_only=True)) >>> save_pipeline(model, extractor, "pipeline.pt", decoder=decoder)
- on_validation_epoch_end(trainer, pl_module)[source]π
Check if model improved and save weights if so.
- Parameters:
- trainer
Trainer The Lightning trainer instance.
- pl_module
LightningModule The Lightning module being trained.
- trainer