Skip to contents

This wraps a CallbackSet and annotates it with metadata, most importantly a ParamSet. The callback is created for the given parameter values by calling the $generate() method.

This class is usually used to configure the callback of a torch learner, e.g. when constructing a learner of in a ModelDescriptor.

For a list of available callbacks, see mlr3torch_callbacks. To conveniently retrieve a TorchCallback, use t_clbk().

Parameters

Defined by the constructor argument param_set. If no parameter set is provided during construction, the parameter set is constructed by creating a parameter for each argument of the wrapped callback, where the parameters are then of type ParamUty.

Super class

TorchDescriptor -> TorchCallback

Active bindings

weight

(numeric(1) or NULL)
Overwrites the $weight of the generated CallbackSet, see its section Ordering.

Methods

Inherited methods


TorchCallback$new()

Creates a new instance of this R6 class.

Usage

TorchCallback$new(
  callback_generator,
  param_set = NULL,
  id = NULL,
  label = NULL,
  packages = NULL,
  man = NULL,
  additional_args = NULL,
  weight = NULL
)

Arguments

callback_generator

(R6ClassGenerator)
The class generator for the callback that is being wrapped.

param_set

(ParamSet or NULL)
The parameter set. If NULL (default) it is inferred from callback_generator. Whether inferred or given, it must not contain a weight: that is the callback's ordering weight, which is set via the weight argument.

id

(character(1))
The id for of the new object.

label

(character(1))
Label for the new instance.

packages

(character())
The R packages this object depends on.

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().

additional_args

(any)
Additional arguments if necessary. For learning rate schedulers, this is the torch::LRScheduler.

weight

(numeric(1) or NULL)
Overwrites the $weight of the generated CallbackSet, see its section Ordering. If NULL (default), the callback's own weight is kept.


TorchCallback$generate()

Generates the CallbackSet, applying $weight if it is set.

Usage

TorchCallback$generate()


TorchCallback$clone()

The objects of this class are cloneable with this method.

Usage

TorchCallback$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Create a new torch callback from an existing callback set
torch_callback = TorchCallback$new(CallbackSetCheckpoint)
# The parameters are inferred
torch_callback$param_set
#> <ParamSet(2)>
#>        id    class lower upper nlevels        default  value
#>    <char>   <char> <num> <num>   <num>         <list> <list>
#> 1:   path ParamUty    NA    NA     Inf <NoDefault[0]> [NULL]
#> 2:   freq ParamUty    NA    NA     Inf <NoDefault[0]> [NULL]

# Retrieve a torch callback from the dictionary
torch_callback = t_clbk("checkpoint",
  path = tempfile(), freq = 1
)
torch_callback
#> <TorchCallback:checkpoint> Checkpoint
#> * Generator: CallbackSetCheckpoint
#> * Parameters: path=/tmp/Rtmp4ZCnAs/file1f26790b3078, freq=1
#> * Packages: mlr3torch,torch
torch_callback$label
#> [1] "Checkpoint"
torch_callback$id
#> [1] "checkpoint"

# open the help page of the wrapped callback set
# torch_callback$help()

# Create the callback set
callback = torch_callback$generate()
callback
#> <CallbackSetCheckpoint>
#> * Stages: on_begin, on_epoch_end, on_end
# is the same as
CallbackSetCheckpoint$new(
  path = tempfile(), freq = 1
)
#> <CallbackSetCheckpoint>
#> * Stages: on_begin, on_epoch_end, on_end

# Use in a learner
learner = lrn("regr.mlp", callbacks = t_clbk("checkpoint"))
# the parameters of the callback are added to the learner's parameter set
learner$param_set
#> <ParamSetCollection(42)>
#>                       id    class lower upper nlevels        default
#>                   <char>   <char> <num> <num>   <num>         <list>
#>  1:               epochs ParamInt 0e+00   Inf     Inf <NoDefault[0]>
#>  2:               device ParamFct    NA    NA      12 <NoDefault[0]>
#>  3:          num_threads ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#>  4:  num_interop_threads ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#>  5:                 seed ParamInt  -Inf   Inf     Inf <NoDefault[0]>
#>  6:               resume ParamUty    NA    NA     Inf         [NULL]
#>  7:            eval_freq ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#>  8:       measures_train ParamUty    NA    NA     Inf <NoDefault[0]>
#>  9:       measures_valid ParamUty    NA    NA     Inf <NoDefault[0]>
#> 10:             patience ParamInt 0e+00   Inf     Inf <NoDefault[0]>
#> 11:            min_delta ParamDbl 0e+00   Inf     Inf <NoDefault[0]>
#> 12: restore_best_weights ParamLgl    NA    NA       2 <NoDefault[0]>
#> 13:           batch_size ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#> 14:   batch_size_predict ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#> 15:              shuffle ParamLgl    NA    NA       2          FALSE
#> 16:              sampler ParamUty    NA    NA     Inf <NoDefault[0]>
#> 17:        batch_sampler ParamUty    NA    NA     Inf <NoDefault[0]>
#> 18:          num_workers ParamInt 0e+00   Inf     Inf              0
#> 19:           collate_fn ParamUty    NA    NA     Inf         [NULL]
#> 20:           pin_memory ParamLgl    NA    NA       2          FALSE
#> 21:            drop_last ParamLgl    NA    NA       2          FALSE
#> 22:              timeout ParamDbl  -Inf   Inf     Inf             -1
#> 23:       worker_init_fn ParamUty    NA    NA     Inf <NoDefault[0]>
#> 24:       worker_globals ParamUty    NA    NA     Inf <NoDefault[0]>
#> 25:      worker_packages ParamUty    NA    NA     Inf <NoDefault[0]>
#> 26:       tensor_dataset ParamFct    NA    NA       1 <NoDefault[0]>
#> 27:            jit_trace ParamLgl    NA    NA       2 <NoDefault[0]>
#> 28:              neurons ParamUty    NA    NA     Inf <NoDefault[0]>
#> 29:                    p ParamDbl 0e+00     1     Inf <NoDefault[0]>
#> 30:             n_layers ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#> 31:           activation ParamUty    NA    NA     Inf <NoDefault[0]>
#> 32:      activation_args ParamUty    NA    NA     Inf <NoDefault[0]>
#> 33:                shape ParamUty    NA    NA     Inf <NoDefault[0]>
#> 34:               opt.lr ParamDbl 0e+00   Inf     Inf          0.001
#> 35:            opt.betas ParamUty    NA    NA     Inf    0.900,0.999
#> 36:              opt.eps ParamDbl 1e-16   Inf     Inf          1e-08
#> 37:     opt.weight_decay ParamDbl 0e+00   Inf     Inf              0
#> 38:          opt.amsgrad ParamLgl    NA    NA       2          FALSE
#> 39:     opt.param_groups ParamUty    NA    NA     Inf <NoDefault[0]>
#> 40:       loss.reduction ParamFct    NA    NA       2           mean
#> 41:   cb.checkpoint.path ParamUty    NA    NA     Inf <NoDefault[0]>
#> 42:   cb.checkpoint.freq ParamInt 1e+00   Inf     Inf <NoDefault[0]>
#>                       id    class lower upper nlevels        default
#>                   <char>   <char> <num> <num>   <num>         <list>
#>            value
#>           <list>
#>  1:       [NULL]
#>  2:         auto
#>  3:            1
#>  4:       [NULL]
#>  5:       random
#>  6:       [NULL]
#>  7:            1
#>  8:    <list[0]>
#>  9:    <list[0]>
#> 10:            0
#> 11:            0
#> 12:        FALSE
#> 13:       [NULL]
#> 14:       [NULL]
#> 15:         TRUE
#> 16:       [NULL]
#> 17:       [NULL]
#> 18:       [NULL]
#> 19:       [NULL]
#> 20:       [NULL]
#> 21:       [NULL]
#> 22:       [NULL]
#> 23:       [NULL]
#> 24:       [NULL]
#> 25:       [NULL]
#> 26:        FALSE
#> 27:        FALSE
#> 28:             
#> 29:          0.1
#> 30:       [NULL]
#> 31: <nn_relu[1]>
#> 32:    <list[0]>
#> 33:       [NULL]
#> 34:       [NULL]
#> 35:       [NULL]
#> 36:       [NULL]
#> 37:       [NULL]
#> 38:       [NULL]
#> 39:       [NULL]
#> 40:       [NULL]
#> 41:       [NULL]
#> 42:       [NULL]
#>            value
#>           <list>