Skip to contents

Returns the function that builds a whole batch of a task, i.e. both the features x and the target y. This is how task_dataset loads its batches.

The returned function takes the arguments

  • data, a data.table with the feature and target columns of the batch, and

  • cache, a rather internal hashtab that can be used as a cache when loading multiple lazy_tensor columns.

It returns a list() with elements x (a named list() of torch_tensors) and y (a torch_tensor or NULL).

The default method applies the ingress tokens to obtain x and the target batchgetter to obtain y.

Usage

get_batch_constructor(
  task,
  feature_ingress_tokens,
  target_batchgetter = NULL,
  ...
)

Arguments

task

(Task)
The task.

feature_ingress_tokens

(named list() of TorchIngressToken)
The ingress tokens that define x. Their features are already resolved, i.e. they are character() vectors and not Selectors.

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.

...

(any)
Additional arguments. Not used yet.

Value

function(data, cache) -> list(x = list<torch_tensor>, y = torch_tensor | NULL)

Examples

task = tsk("iris")
token = TorchIngressToken(task$feature_names, batchgetter_num, c(NA, 4))
# the token's features are a `Selector`; `task_dataset()` resolves them for you
token$features = token$features(task)
batch_constructor = get_batch_constructor(
  task,
  feature_ingress_tokens = list(input = token),
  target_batchgetter = get_target_batchgetter(task)
)
batch = batch_constructor(data = task$data(rows = 1:2))
batch$x$input
#> torch_tensor
#>  1.4000  0.2000  5.1000  3.5000
#>  1.4000  0.2000  4.9000  3.0000
#> [ CPUFloatType{2,4} ]
batch$y
#> torch_tensor
#>  1
#>  1
#> [ CPULongType{2} ]