Saves the optimizer and network states during training. The final network and optimizer are always stored.
Checkpoints are written at the end of an epoch. For one written after epoch <n>, two files
are created in path:
network<n>.pt:: The$state_dict()of the network.optimizer<n>.pt:: The$state_dict()of the optimizer.
An epoch that was interrupted – because training failed or was stopped – is not written under
its own number, so network<n>.pt is always the network at the end of epoch n.
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.
Super class
CallbackSet -> CallbackSetCheckpoint
Public fields
weight(
numeric(1))Inf, so that this callback runs after the other callbacks and hence saves the network and optimizer as they are at the end of the stage, see section Ordering ofCallbackSet. The only exception is the restore ofrestore_best_weights, which happens afterwards, so a checkpoint always holds the network as training left it.
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 or empty, so that a checkpoint never overwrites unrelated data.freq(
integer(1))
How often the model is saved, in epochs.
CallbackSetCheckpoint$on_epoch_end()
Saves the network and optimizer state dict.
Does nothing if freq is not met.
CallbackSetCheckpoint$on_exit()
Saves the final network and optimizer, unless the last complete 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"