pub struct CheckpointCallback {
pub checkpoint_dir: PathBuf,
pub save_frequency: usize,
pub save_best_only: bool,
pub keep_top_k: Option<usize>,
/* private fields */
}Expand description
Callback for model checkpointing with auto-cleanup.
Fields§
§checkpoint_dir: PathBufDirectory to save checkpoints.
save_frequency: usizeFrequency of checkpointing (every N epochs).
save_best_only: boolWhether to save only the best model.
keep_top_k: Option<usize>Maximum number of checkpoints to keep (None = keep all).
Implementations§
Source§impl CheckpointCallback
impl CheckpointCallback
Sourcepub fn new(
checkpoint_dir: PathBuf,
save_frequency: usize,
save_best_only: bool,
) -> Self
pub fn new( checkpoint_dir: PathBuf, save_frequency: usize, save_best_only: bool, ) -> Self
Create a new checkpoint callback.
Sourcepub fn with_cleanup(
checkpoint_dir: PathBuf,
save_frequency: usize,
save_best_only: bool,
keep_top_k: usize,
) -> Self
pub fn with_cleanup( checkpoint_dir: PathBuf, save_frequency: usize, save_best_only: bool, keep_top_k: usize, ) -> Self
Create a new checkpoint callback with auto-cleanup.
This will automatically delete old checkpoints when the number exceeds keep_top_k,
keeping only the checkpoints with the best (lowest) validation loss.
§Arguments
checkpoint_dir- Directory to save checkpointssave_frequency- Save every N epochssave_best_only- Only save when validation loss improveskeep_top_k- Maximum number of checkpoints to keep (keeps best by validation loss)
§Example
use tensorlogic_train::CheckpointCallback;
use std::path::PathBuf;
// Keep only the top 5 best checkpoints
let callback = CheckpointCallback::with_cleanup(
PathBuf::from("/tmp/checkpoints"),
1, // save every epoch
false, // save all, not just best
5 // keep top 5
);Sourcepub fn num_saved_checkpoints(&self) -> usize
pub fn num_saved_checkpoints(&self) -> usize
Get the number of saved checkpoints being tracked.
Sourcepub fn cleanup_checkpoints(&mut self) -> TrainResult<usize>
pub fn cleanup_checkpoints(&mut self) -> TrainResult<usize>
Manually cleanup checkpoints, keeping only the top-k best.
This can be called manually to trigger cleanup if you’ve changed the
keep_top_k setting.
Trait Implementations§
Source§impl Callback for CheckpointCallback
impl Callback for CheckpointCallback
Source§fn on_epoch_end(
&mut self,
epoch: usize,
state: &TrainingState,
) -> TrainResult<()>
fn on_epoch_end( &mut self, epoch: usize, state: &TrainingState, ) -> TrainResult<()>
Called at the end of an epoch.
Source§fn on_train_begin(&mut self, _state: &TrainingState) -> TrainResult<()>
fn on_train_begin(&mut self, _state: &TrainingState) -> TrainResult<()>
Called at the beginning of training.
Source§fn on_train_end(&mut self, _state: &TrainingState) -> TrainResult<()>
fn on_train_end(&mut self, _state: &TrainingState) -> TrainResult<()>
Called at the end of training.
Source§fn on_epoch_begin(
&mut self,
_epoch: usize,
_state: &TrainingState,
) -> TrainResult<()>
fn on_epoch_begin( &mut self, _epoch: usize, _state: &TrainingState, ) -> TrainResult<()>
Called at the beginning of an epoch.
Source§fn on_batch_begin(
&mut self,
_batch: usize,
_state: &TrainingState,
) -> TrainResult<()>
fn on_batch_begin( &mut self, _batch: usize, _state: &TrainingState, ) -> TrainResult<()>
Called at the beginning of a batch.
Source§fn on_batch_end(
&mut self,
_batch: usize,
_state: &TrainingState,
) -> TrainResult<()>
fn on_batch_end( &mut self, _batch: usize, _state: &TrainingState, ) -> TrainResult<()>
Called at the end of a batch.
Source§fn on_validation_end(&mut self, _state: &TrainingState) -> TrainResult<()>
fn on_validation_end(&mut self, _state: &TrainingState) -> TrainResult<()>
Called after validation.
Source§fn should_stop(&self) -> bool
fn should_stop(&self) -> bool
Check if training should stop early.
Auto Trait Implementations§
impl Freeze for CheckpointCallback
impl RefUnwindSafe for CheckpointCallback
impl Send for CheckpointCallback
impl Sync for CheckpointCallback
impl Unpin for CheckpointCallback
impl UnwindSafe for CheckpointCallback
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more