Convenience function to create a custom TorchCallback.
All arguments that are available in callback_set() are also available here.
For more information on how to correctly implement a new callback, see CallbackSet.
Usage
torch_callback(
id,
classname = paste0("CallbackSet", capitalize(id)),
param_set = NULL,
packages = NULL,
label = capitalize(id),
man = NULL,
on_begin = NULL,
on_end = NULL,
on_exit = NULL,
on_epoch_begin = NULL,
on_before_valid = NULL,
on_epoch_end = NULL,
on_batch_begin = NULL,
on_batch_end = NULL,
on_after_backward = NULL,
on_batch_valid_begin = NULL,
on_batch_valid_end = NULL,
on_valid_end = NULL,
state_dict = NULL,
load_state_dict = NULL,
initialize = NULL,
weight = NULL,
public = NULL,
private = NULL,
active = NULL,
parent_env = parent.frame(),
inherit = CallbackSet,
lock_objects = FALSE
)Arguments
- id
(
character(1))
`
The id for the torch callback.- classname
(
character(1))
The class name.- param_set
(
ParamSet)
The parameter set, if not present it is inferred from the$initialize()method.- packages
(
character())The packages the callback depends on. Default isNULL`.- label
(
character(1))
The label for the torch callback. Defaults to the capitalizedid.- man
(
character(1))
String in the format[pkg]::[topic]pointing to a manual page for this object. The referenced help package can be opened via method$help(). The default isNULL.- on_begin, on_end, on_epoch_begin, on_before_valid, on_epoch_end, on_batch_begin, on_batch_end, on_after_backward, on_batch_valid_begin, on_batch_valid_end, on_valid_end, on_exit
(
function)
Function to execute at the given stage, see section Stages.- state_dict
(
function())
The function that retrieves the state dict from the callback. This is what will be available in the learner after training.- load_state_dict
(
function(state_dict))
Function that loads a callback state.- initialize
(
function())
The initialization method of the callback.- weight
(
numeric(1))
Controls when the callback is called within a stage, see section Ordering ofCallbackSet. Defaults to0.- public, private, active
(
list())
Additional public, private, and active fields to add to the callback.- parent_env
(
environment())
The parent environment for theR6Class.- inherit
(
R6ClassGenerator)
From which class to inherit. This class must either beCallbackSet(default) or inherit from it.- lock_objects
(
logical(1))
Whether to lock the objects of the resultingR6Class. IfFALSE(default), values can be freely assigned toselfwithout declaring them in the class definition.
Internals
It first creates an R6 class inheriting from CallbackSet (using callback_set()) and
then wraps this generator in a TorchCallback that can be passed to a torch learner.
Stages
begin:: Run before the training loop begins.epoch_begin:: Run at the beginning of each epoch.batch_begin:: Run before the forward call.after_backward:: Run after the backward call.batch_end:: Run after the optimizer step.before_valid:: Run before the validation loop begins.batch_valid_begin:: Run before the forward call in the validation loop.batch_valid_end:: Run after the forward call in the validation loop.valid_end:: Run at the end of validation.epoch_end:: Run at the end of each epoch.end:: Run after last epoch.exit:: Run last, usingon.exit().
Ordering
Within a stage, callbacks are called in the order in which they were passed to the learner.
A callback can override this via its $weight field: callbacks with a higher weight are called
after those with a lower one, and callbacks with the same weight keep the order in which they
were passed.
The default weight is 0.
This matters for callbacks that observe what the others did, which is why
CallbackSetCheckpoint has weight Inf: it always runs last and therefore saves the network
and optimizer as the other callbacks left them at the end of the stage.
Examples
custom_tcb = torch_callback("custom",
initialize = function(name) {
self$name = name
},
on_begin = function() {
cat("Hello", self$name, ", we will train for ", self$ctx$total_epochs, "epochs.\n")
},
on_end = function() {
cat("Training is done.")
}
)
learner = lrn("classif.torch_featureless",
batch_size = 16,
epochs = 1,
callbacks = custom_tcb,
cb.custom.name = "Marie",
device = "cpu"
)
task = tsk("iris")
learner$train(task)
#> Hello Marie , we will train for 1 epochs.
#> Training is done.