mlr3torch icon indicating copy to clipboard operation
mlr3torch copied to clipboard

missing batch_size error inconsistent with torch docs, batch_sampler arg

Open tdhock opened this issue 4 months ago • 2 comments

?torch::dataloader says

batch_sampler: (Sampler, optional): like sampler, but returns a batch
          of indices at a time. Mutually exclusive with ‘batch_size’,
          ‘shuffle’, sampler, and ‘drop_last’. Custom samplers can be
          created with sampler().

So batch_size should not be provided at the same time at batch_sampler. However that contradicts the error message I get from mlr3torch when I run the code below:

batch_sampler_class <- torch::sampler(
  "BatchSampler",
  initialize = function(data_source) {
    self$data_source <- data_source
  },
  .iter = function() {
    batch_size <- 2
    indices <- 1:self$.length()
    batch_vec <- (indices-1) %/% batch_size
    batch_list <- rev(split(indices, batch_vec))
    count <- 0L
    function() {
      if (count < length(batch_list)) {
        count <<- count + 1L
        return(batch_list[[count]])
      }
      coro::exhausted()
    }
  },
  .length = function() {
    length(self$data_source)
  }
)
sonar_task <- mlr3::tsk("sonar")
sonar_ingress <- list(feat=mlr3torch::TorchIngressToken(
  features=sonar_task$col_roles$feature,
  batchgetter=mlr3torch::batchgetter_num))
target_batchgetter <- function(data){
  browser()
  torch::torch_tensor()
}
sonar_dataset <- mlr3torch::task_dataset(sonar_task, sonar_ingress, target_batchgetter)
batch_sampler_instance <- batch_sampler_class(sonar_dataset)
inst_learner <- mlr3torch::LearnerTorchMLP$new(task_type="classif")
inst_learner$param_set$set_values(
  epochs=10,
  batch_sampler=batch_sampler_class)
inst_learner$train(sonar_task)

In the code above I specified batch_sampler but I get the error message below:

> inst_learner$train(sonar_task)
Erreur dans .__ParamSet__get_values(self = self, private = private, super = super,  : 
  Missing required parameters: batch_size

The error message says I need to add batch_size parameter, but that contradicts the documentation in torch. @sebffischer Can this error message be removed please? It would be much less confusing for users if error messages in mlr3torch were consistent with the documented behavior in torch.

tdhock avatar Jul 29 '25 04:07 tdhock

Thanks, will fix this!

sebffischer avatar Jul 29 '25 08:07 sebffischer

related to #420 and may be fixed by https://github.com/mlr-org/mlr3torch/pull/425

tdhock avatar Nov 19 '25 14:11 tdhock