task_dataset: Create a Dataset from a Task

task_datasetR Documentation

Create a Dataset from a Task

Description

Creates a torch dataset from an mlr3 Task. The resulting dataset's ⁠$.getbatch()⁠ method returns a list with elements x, y and .index:

  • x is a list with tensors, whose content is defined by the parameter feature_ingress_tokens.

  • y is the target variable and its content is defined by the parameter target_batchgetter.

  • .index is the index of the batch in the task's data.

The data is returned on the device specified by the parameter device.

Usage

task_dataset(
  task,
  feature_ingress_tokens,
  target_batchgetter = NULL,
  cache_data = TRUE
)

Arguments

task

(Task)
The task for which to build the dataset.

feature_ingress_tokens

(named list() of TorchIngressToken)
Each ingress token defines one item in the ⁠$x⁠ value of a batch with corresponding names.

target_batchgetter

(⁠function()⁠ or NULL)
Converts the target columns of a batch into the target tensor y that the loss is applied to. Takes an argument data, a data.table with only the target columns, and optionally an argument x, the named list of feature tensors of the batch, which is what a target that is a function of the input needs, see get_target_batchgetter(). If NULL (default), the batches have no y element.

cache_data

(logical(1))
Whether to retrieve the data from the task once (in ⁠$initialize()⁠) and to take the batches from it, instead of retrieving every batch from the task, which is slow. This needs memory for a copy of the task data (lazy tensor columns only contain indices and are small).

Value

torch::dataset

Examples


task = tsk("iris")
sepal_ingress = TorchIngressToken(
  features = c("Sepal.Length", "Sepal.Width"),
  batchgetter = batchgetter_num,
  shape = c(NA, 2)
)
petal_ingress = TorchIngressToken(
  features = c("Petal.Length", "Petal.Width"),
  batchgetter = batchgetter_num,
  shape = c(NA, 2)
)
ingress_tokens = list(sepal = sepal_ingress, petal = petal_ingress)

target_batchgetter = function(data) {
  torch_tensor(data = data[[1L]], dtype = torch_float32())$unsqueeze(2)
}
dataset = task_dataset(task, ingress_tokens, target_batchgetter)
batch = dataset$.getbatch(1:10)
batch


mlr3torch documentation built on Oct. 30, 2026, 5:10 p.m.