How to make tar_map and mlr3misc::pmap work together?

I've written a mlr3-pipeline inside a targets-pipeline. I would like to run this pipeline multiple times for an experiment and I wanted to tar_map the whole thing. I chose static branching since I aim for each pipeline to run individually with its own sample data.

The pipeline contains (multiple) mlr3misc::pmap- functions, which work fine on their own. Once included in a tar_map, it does not work anymore and I don't understand what is wrong. I created two code examples, one without that mapping, this one works. The other one adds a mapping and it throws the error.

The error-message itself:

zero-length inputs cannot be mixed with those of non-zero length

The version that runs, with the mapping-code in comments for easier comparison to the erronious version:

library(targets)

targets::tar_script({
  library(mlr3)
  library(mlr3learners)
  library(mlr3tuningspaces)
  
  # values <- tibble::tribble(
  #   ~name, ~taskname,
  #   1, "sonar",
  #   2, "penguins",
  # )
  
 #function used in the pmap to add HP-values to each learner
  add_params <- function(learner, param_values) {
    learner$param_set$values <- param_values
    return(learner)
  }
  
  list(
    #tarchetypes::tar_map(
      #values = values,
      #names = name,
      tar_target(my_task, tsk("sonar")),
      # a list called "learners" that contains two lists, one with the learners and one with the default HP-search spaces
      tar_target(learners,
                 list(
                   learners = lrns(c("classif.kknn",
                                     "classif.ranger")),
                   params = list(lts("classif.kknn.default")$values,
                                 lts("classif.ranger.default")$values))
      ),
      # a function to map each learner to its default HP-space
      tar_target(learners_params,
                 mlr3misc::pmap(list(learners$learners, learners$params), add_params)
      )
    )
  #)
})


targets::tar_manifest()
tar_visnetwork()
targets::tar_make()

And here the version that throws an error:

library(targets)

targets::tar_script({
  library(mlr3)
  library(mlr3learners)
  library(mlr3tuningspaces)
  
  values <- tibble::tribble(
    ~name, ~taskname,
    1, "sonar",
    2, "penguins",
  )
  
  add_params <- function(learner, param_values) {
    learner$param_set$values <- param_values
    return(learner)
  }
  
  list(
    tarchetypes::tar_map(
    values = values,
    names = name,
    tar_target(my_task, tsk(taskname)),
      # a list called "learners" that contains two lists, one with the learners and one with the default HP-search spaces
      tar_target(learners,
                 list(
                   learners = lrns(c("classif.kknn",
                                     "classif.ranger")),
                   params = list(lts("classif.kknn.default")$values,
                                 lts("classif.ranger.default")$values))
      ),
      # a function to map each learner to its default HP-space
      tar_target(learners_params,
                 mlr3misc::pmap(list(learners$learners, learners$params), add_params)
      )
    )
  )
})


targets::tar_manifest()
tar_visnetwork()
targets::tar_make()

So far, I've stripped the complex pipeline down to find this problem. I also see that, in the example above, the mapped targets have nothing to do with the target that does the pmap. Thus, I am confused.

Do you see anything I could do to make this work? Let me know if I need to add information.

Upvotes: 0

Views: 27

Answers (0)

Related Questions