tsk_mtcars = tsk("mtcars")
lrn_tree = lrn("regr.rpart")
rsmp_cv = rsmp("cv", folds = 3)
rr = resample(
task = tsk_mtcars,
learner = lrn_tree,
resampling = rsmp_cv
)
rr$aggregate(msr("regr.mse"))regr.mse
19
mlr3torch

mlr3torch is a high level deep learning framework in R, built mainly on top of:
mlr3 - A machine learning framework in Rmlr3pipelines - A dataflow programming toolkittorch - A PyTorch implementation written in native RIt allows to easily build, train and evaluate deep learning models
tensorflow, keras & rtorch - use python libraries through reticulatetorch - native R libraryluz - higher level deep learning library built on top of torchbrulee - high level modeling functions for torchmlr3 Ecosystem
mlr3’s “Hello World”
tsk() creates an example Task, where the goal is to predict the miles-per-galleon of carslrn() defines a simple regression tree Learner from the {rpart} packagersmp() sets a Resampling strategyresample() runs the resample experimentmsr() initializes an mlr3 performance Measuremlr3pipelines

mlr3pipelines1 allows to assemble new Learners by connecting PipeOps in a Graph, e.g. for preprocessingPipeOps are created via po(), and combined using the chain operator %>>% torch’s “Hello World”
torchvisionmlr3torch in a NutshellLearnersmlr3pipelines::Graphstorch_tensorsReLU activationlibrary(mlr3torch)
lrn_mlp = lrn("regr.mlp",
activation = torch::nn_relu,
neurons = 10,
batch_size = 32,
epochs = 100,
loss = t_loss("mse"),
optimizer = t_opt("adam", lr = 0.5),
callbacks = t_clbk("history")
)
design = benchmark_grid(
tsk_mtcars, list(lrn_mlp, lrn_tree), rsmp_cv
)
bmr = benchmark(design)
bmr$aggregate(msr("regr.mse")) nr task_id learner_id resampling_id iters regr.mse
1: 1 mtcars regr.mlp cv 3 86
2: 2 mtcars regr.rpart cv 3 21
Hidden columns: resample_result
mlr3pipelines::Graphs
PipeOps in a GraphGraph is fully interoperable with other PipeOpsmlp_graph = po("torch_ingress_ltnsr") %>>%
po("nn_linear", out_features = 20) %>>%
po("nn_relu") %>>%
po("nn_head") %>>%
po("torch_loss", t_loss("cross_entropy")) %>>%
po("torch_optimizer", t_opt("adam", lr = 0.1)) %>>%
po("torch_model_classif", batch_size = 16, epochs = 5)
lrn_mlp_graph = as_learner(mlp_graph)lazy_tensorslazy_tensors, which wrap a torch::datasetlazy_tensors happens lazily by internally building up a preprocessing GraphGraphLearnerGraphLearner has a parameter set representing all configuration options, which can be tuned!mlr3mbo. id class lower upper
1: nn_linear.out_features ParamInt 1 Inf
2: torch_optimizer.lr ParamDbl 0 Inf
mlr3torch naturally supports multi-modal data as lazy_tensors can be stored in data.framesmlr3torch allows to easily build neural networks with multiple inputs, e.g. each operating on a different subset of features<TaskClassif:medical> (100 x 4): Medical Diagnosis
* Target: status
* Properties: twoclass
* Features (3):
- dbl (2): age, lesion_size
- lt (1): image
status age image lesion_size
1: benign NA <tnsr[3x64x64]> 0.99
2: benign 21 <tnsr[3x64x64]> 0.56
3: malignant 56 <tnsr[3x64x64]> 0.71

branch_num = po("select_1", selector = selector_type("numeric")) %>>%
po("pca") %>>%
po("torch_ingress_num") %>>%
po("nn_linear", out_features = 20) %>>%
po("nn_sigmoid")
branch_img = po("select_2", selector = selector_type("lazy_tensor")) %>>%
po("torch_ingress_ltnsr") %>>%
po("nn_conv2d", out_channels = 3) %>>%
po("nn_relu") %>>%
po("nn_flatten")
graph = po("imputeoor") %>>% list(branch_num, branch_img) %>>%
po("nn_merge_cat") %>>%
po("nn_head") %>>%
po("torch_loss", t_loss("cross_entropy")) %>>%
po("torch_optimizer", t_opt("adam", lr = 1e-4)) %>>%
po("torch_model_classif", batch_size = 16, epochs = 50)
UseR 2024