Saves the optimizer, weights, and callback states every freq epochs as well as the final state.
This can be used to later continue a training run via the resume parameter of LearnerTorch.
Checkpoints are written at the end of an epoch. For one written after epoch <n>, three files
are created in path:
network<n>.pt:: The$state_dict()of the network.optimizer<n>.pt:: The$state_dict()of the optimizer.state<n>.rds:: The epoch, the version ofmlr3torchthat wrote the checkpoint, the$state_dict()s of the training run's other callbacks, so that a later run can continue, as well as some other information. Additionally, there isrun.rdswhich contains some additioanl global meta information.
Details
Saving the learner itself in the callback with a trained model is impossible, as the model slot is set after the last callback step is executed.
Resuming
This callback is special because it enables resuming a training run.
Its own state is the folder it writes to, which learner$model$callbacks$<id>$path reports –
the only way to learn where a path function sent a run, e.g. one fit of a resample().
That state is not part of a checkpoint and is not restored: a resuming run writes where its own
path says.
Ordering
This callback has weight Inf and therefore runs last, so it captures all the changes other
callbacks made.
Super class
CallbackSet -> CallbackSetCheckpoint
Methods
Inherited methods
CallbackSetCheckpoint$new()
Creates a new instance of this R6 class.
Usage
CallbackSetCheckpoint$new(path, freq)Arguments
path(
character(1)|function())
The path to a folder where the models are saved, or a function of no arguments returning it. The latter is especially useful to create unique directories duringresample()orbenchmark()per fit. The folder must be new, empty, or already contain checkpoints. A half-written checkpoint – what a run killed mid-write leaves behind – may be written over, since a resuming run continues from the newest complete one.freq(
integer(1))
How often the model is saved, in epochs.
CallbackSetCheckpoint$state_dict()
Returns the folder this callback writes to so it can be accessed from the learner
when the path was a function.
CallbackSetCheckpoint$on_epoch_end()
Saves the network and optimizer state dict.
Does nothing if freq is not met.
CallbackSetCheckpoint$on_end()
Saves the final network and optimizer, unless the last epoch was already saved.
Examples
cb = t_clbk("checkpoint", freq = 1)
task = tsk("iris")
pth = tempfile()
learner = lrn("classif.mlp", epochs = 3, batch_size = 1, callbacks = cb)
learner$param_set$set_values(cb.checkpoint.path = pth)
learner$train(task)
list.files(pth)
#> [1] "network1.pt" "network2.pt" "network3.pt" "optimizer1.pt"
#> [5] "optimizer2.pt" "optimizer3.pt" "run.rds" "state1.rds"
#> [9] "state2.rds" "state3.rds"
# continue training for 3 more epochs, starting from the last checkpoint
learner_resumed = lrn("classif.mlp", epochs = 6, batch_size = 1, resume = pth)
learner_resumed$train(task)
learner_resumed$model$epochs
#> [1] 6