Skip to contents

A general-purpose Task that can be used for modeling arbitrary problems, including supervised and unsupervised problems. The article on Custom Learning Problems covers all of this in more detail.

The problem this generic task solves is that it is rather complicated to register new task types with mlr3, so this class makes this easier. The price of this flexibility is the loss of some compatibility checks.

Super class

mlr3::Task -> TaskTorch

Active bindings

hash

(character(1))
The hash of the task.

default_encoder

(function() or NULL)
The default prediction encoder. Read-only.

default_measure

(Measure or NULL)
See the construction argument. Read-only, for the same reason as default_encoder.

output_dim

(function() or NULL)
See the construction argument. Called by output_dim_for().

Methods

Inherited methods


TaskTorch$new()

Creates a new instance of this R6 class.

Usage

TaskTorch$new(
  id,
  backend,
  target = NULL,
  label = NA_character_,
  output_dim = NULL,
  default_encoder = NULL,
  default_measure = NULL
)

Arguments

id

(character(1))
The id of the task.

backend

(DataBackend or data.frame())
The data.

target

(character() or NULL)
The names of the target columns. NULL (default) for a task without a target, see section Tasks without a Target of TaskTorch.

label

(character(1))
The label of the task.

output_dim

(function() or NULL)
Returns the number of output units the network needs. Takes an argument task and returns a single positive integer. May be NULL (default), in which case any caller of output_dim_for() errors.

default_encoder

(function() or NULL)
The default prediction encoder for the task. This can be overwritten by a learner's private $.encode_prediction method. See LearnerTorch for more information.

default_measure

(Measure or NULL)
The default measure of the task, i.e. what msr("torch.default") resolves to.


TaskTorch$truth()

The ground truth, see section Scoring. Might return NULL for unsupervised problems.

Usage

TaskTorch$truth(rows = NULL)

Arguments

rows

(integer())
The rows to return the truth for. All rows if NULL.


TaskTorch$clone()

The objects of this class are cloneable with this method.

Usage

TaskTorch$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# multi-label classification: one logical column per label
d = data.frame(x1 = rnorm(50), x2 = rnorm(50))
d$a = d$x1 > 0
d$b = d$x2 > 0
task = as_task_torch(d, target = c("a", "b"), id = "labels",
  output_dim = function(task) length(task$target_names),
  default_encoder = function(task, network_output, predict_type) {
    prob = as.matrix(torch::nnf_sigmoid(network_output)$cpu())
    colnames(prob) = task$target_names
    list(response = prob > 0.5, prob = if (predict_type == "prob") prob)
  })
task
#> 
#> ── <TaskTorch> (50x4) ──────────────────────────────────────────────────────────
#> • Target: a and b
#> • Properties: -
#> • Features (2):
#>   • dbl (2): x1, x2
output_dim_for(task)