The futurize package allows you to easily turn sequential code
into parallel code by piping the sequential code to the futurize()
function. Easy!
library(futurize)
plan(multisession)
library(mlr3)
task <- tsk("penguins")
learner <- lrn("classif.rpart")
resampling <- rsmp("cv", folds = 10)
rr <- resample(task, learner, resampling) |> futurize()
This vignette demonstrates how to use this approach to parallelize
mlr3 functions such as resample() and benchmark().
The mlr3 package is a modern, object-oriented framework for machine learning in R. It provides a unified interface to many learning algorithms, together with tools for resampling, benchmarking, and model evaluation. Resampling and benchmarking fit and evaluate a model once per resampling iteration, which can be computationally intensive. Since these iterations are independent of each other, they are ideal candidates for parallelization.
The resample() function fits a learner on each training set of a
resampling strategy and evaluates it on the corresponding test set.
For example, 10-fold cross-validation of a classification tree
results in ten independent model fits:
library(futurize)
library(mlr3)
task <- tsk("penguins")
learner <- lrn("classif.rpart")
resampling <- rsmp("cv", folds = 10)
rr <- resample(task, learner, resampling) |> futurize()
rr$aggregate(msr("classif.ce"))
The iterations are processed in parallel by the workers defined
by your current plan(). For example, to parallelize on your local
machine:
plan(multisession)
The benchmark() function runs multiple resampling experiments,
e.g. to compare several learners on one or more tasks:
library(futurize)
library(mlr3)
design <- benchmark_grid(
tasks = tsk("penguins"),
learners = lrns(c("classif.rpart", "classif.featureless")),
resamplings = rsmp("cv", folds = 10)
)
bmr <- benchmark(design) |> futurize()
bmr$aggregate(msr("classif.ce"))
All resampling iterations across all experiments are processed in parallel.
The mlr3 package already parallelizes resample() and
benchmark() via future.apply, using the future backend set
by plan(). It is also possible to configure mlr3 to
parallelize via mirai instead, by setting up mirai daemons for
the "mlr3_parallelization" compute profile. When you pipe one of
these functions to futurize(), mlr3 is guaranteed to
parallelize via futures, even if mirai daemons have been set up for
mlr3, e.g. by other code in your R session.
The mlr3 package itself decides which globals and packages to
export to the parallel workers, and it always uses parallel random
number generation (rng = TRUE). Because of this, futurize()
options globals and packages are ignored for mlr3 functions.
Other future options, e.g. chunk_size and stdout, are passed on
as-is, e.g.
rr <- resample(task, learner, resampling) |> futurize(chunk_size = 5)
The following mlr3 functions are supported by futurize():
benchmark() with rng = TRUE as the defaultresample() with rng = TRUE as the default