Skip to contents

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

This class is usually used to configure the loss function of a torch learner, e.g. when construcing a learner or in a ModelDescriptor.

For a list of available losses, see mlr3torch_losses. Items from this dictionary can be retrieved using t_loss().

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 loss function, where the parameters are then of type ParamUty.

Super class

TorchDescriptor -> TorchLoss

Public fields

task_types

(character())
The task types this loss supports.

Methods

Inherited methods


TorchLoss$new()

Creates a new instance of this R6 class.

Usage

TorchLoss$new(
  torch_loss,
  task_types = NULL,
  param_set = NULL,
  id = NULL,
  label = NULL,
  packages = NULL,
  man = NULL
)

Arguments

torch_loss

(nn_loss or function)
The loss module or function that generates the loss module. Can have arguments task that will be provided when the loss is instantiated.

task_types

(character())
The task types supported by this loss. If NULL (default), the loss is applicable to "classif", "regr" and "torch".

param_set

(ParamSet or NULL)
The parameter set. If NULL (default) it is inferred from torch_loss.

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


TorchLoss$print()

Prints the object

Usage

TorchLoss$print(...)

Arguments

...

any


TorchLoss$generate()

Instantiates the loss function.

Usage

TorchLoss$generate(task = NULL)

Arguments

task

(Task)
The task. Must be provided if the loss function requires a task.

Returns

torch_loss


TorchLoss$clone()

The objects of this class are cloneable with this method.

Usage

TorchLoss$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# Create a new torch loss
torch_loss = TorchLoss$new(torch_loss = nn_mse_loss, task_types = "regr")
torch_loss
#> <TorchLoss:nn_mse_loss> nn_mse_loss
#> * Generator: nn_mse_loss
#> * Parameters: list()
#> * Packages: torch,mlr3torch
#> * Task Types: regr
# the parameters are inferred
torch_loss$param_set
#> <ParamSet(1)>
#>           id    class lower upper nlevels        default  value
#>       <char>   <char> <num> <num>   <num>         <list> <list>
#> 1: reduction ParamUty    NA    NA     Inf <NoDefault[0]> [NULL]

# Retrieve a loss from the dictionary:
torch_loss = t_loss("mse", reduction = "mean")
# is the same as
torch_loss
#> <TorchLoss:mse> Mean Squared Error
#> * Generator: nn_mse_loss
#> * Parameters: reduction=mean
#> * Packages: torch,mlr3torch
#> * Task Types: regr,torch
torch_loss$param_set
#> <ParamSet(1)>
#>           id    class lower upper nlevels default  value
#>       <char>   <char> <num> <num>   <num>  <list> <list>
#> 1: reduction ParamFct    NA    NA       2    mean   mean
torch_loss$label
#> [1] "Mean Squared Error"
torch_loss$task_types
#> [1] "regr"  "torch"
torch_loss$id
#> [1] "mse"

# Create the loss function
loss_fn = torch_loss$generate()
loss_fn
#> An `nn_module` containing 0 parameters.
# Is the same as
nn_mse_loss(reduction = "mean")
#> An `nn_module` containing 0 parameters.

# open the help page of the wrapped loss function
# torch_loss$help()

# Use in a learner
learner = lrn("regr.mlp", loss = t_loss("mse"))
# The parameters of the loss are added to the learner's parameter set
learner$param_set
#> <ParamSetCollection(40)>
#>                       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
#>                       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]
#>            value
#>           <list>