flâneur — a map of the web's best reading

Customize checkpointing behavior (intermediate) — PyTorch Lightning 2.4.0 documentation

lightning.ai · 1,302 words · saved by 1 readers

To save checkpoints based on a (when/which/what/where) condition (for example when the validation_loss is lower) modify the ModelCheckpoint properties. When using iterative training which doesn’t have an epoch, you can checkpoint at every N training steps by specifying every_n_train_steps=N. You can also control the interval of epochs between checkpoints using every_n_epochs, to avoid slowdowns. You can checkpoint at a regular time interval using the train_time_interval argument independent of the steps or epochs. In case you are monitoring a training metric, we’d suggest using save_on_train_epoch_end=True to ensure the required metric is being accumulated correctly for creating a checkpoint. You can save the last checkpoint when training ends using save_last argument. You can save top-K and last-K checkpoints by configuring the monitor and save_top_k argument. NOTE It is recommended that you pass formatting options to filename to include the monitored metric like shown in the example

Customize checkpointing behavior (intermediate) ¶ Audience: Users looking to customize the checkpointing behavior Modify checkpointing behavior ¶ For fine-grained control over checkpointing behavior, use the ModelCheckpoint object from lightning.pytorch.callbacks import ModelCheckpoint checkpoint_callback = ModelCheckpoint ( dirpath = "my/path/" , save_top_k = 2 , monitor = "val_loss" ) trainer = Trainer ( callbacks = [ checkpoint_callback ]) trainer . fit ( model ) # Access best and last model checkpoint directly from the callback print ( checkpoint_callback . best_model_path ) print ( checkp

Explore this link on the map →

related reading