mlr-org / mlr3torch

Deep learning framework for the mlr3 ecosystem based on torch
https://mlr3torch.mlr-org.com
Other
40 stars 7 forks source link

simple use case: rf vs torch: no factors, no missing values #284

Open sebffischer opened 2 months ago

sebffischer commented 2 months ago

Goal: Compare random forests with a simple multi layer perceptron in a simple benchmark experiment.

  1. We need to define the tasks:

Use three simple small tasks from OpenML: https://mlr3book.mlr-org.com/chapters/chapter11/large-scale_benchmarking.html. Simple means no missing values, also no factor variables, i.e. only numeric features. Use only classification tasks

  1. We need to define the learners:
  1. We need to define the resampling strategy: Also just some cross-validation

And also we need to parallelize the experiment executing using the future package: https://mlr3book.mlr-org.com/chapters/chapter10/advanced_technical_aspects_of_mlr3.html#sec-parallel-learner

sebffischer commented 2 months ago

Some example:

library(mlr3torch)
library(mlr3tuning)
library(mlr3learners)

learner = lrn("classif.mlp",
  # define the tuning space via the to_tune() tokens

  # use either 16, 32, or 64
  batch_size = to_tune(c(16, 32, 64)),
  # tune the dropout probability in the interval [0.1, 0.9]
  p = to_tune(0.1, 0.9),
  # tune the epochs using early stopping (internal = TRUE)
  epochs = to_tune(upper = 1000L, internal = TRUE),
  # configure the early-stopping / validation
  validate = 0.3,
  measures_valid = msr("classif.acc"),
  patience = 10,

  device = "cpu"
)

at = auto_tuner(
  learner = learner,
  tuner = tnr("grid_search"),
  resampling = rsmp("cv"),
  measure = msr("classif.acc"),
  term_evals = 10
)

task = tsk("iris")

at$train(task)

future::plan("multisesssion")

design = benchmark_grid(
  tsk("iris"),
  learners = list(at, lrn("classif.ranger")),
  resampling = rsmp("cv", folds = 10)
)
benchmark(design)

# parallelize the outer resampling, not the inner resampling

# 1. apply learner at to fold 1 of iris (outer)
# 2. apply learner at to fold 2 of iris (outer)
     #  the autotuner itself also can parallelize execution (inner)
# ...
# 10. apply learner at to fold 10 of iris (outer)
# 11. apply learner ranger to fold 1 of iris (outer)
# ..
# 20. apply learner ranger to fold 10 of iris (outer)