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 torch
brulee
- high level modeling functions for torch
mlr3
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 Measure
mlr3pipelines
mlr3pipelines
1 allows to assemble new Learner
s by connecting PipeOp
s in a Graph
, e.g. for preprocessingPipeOp
s are created via po()
, and combined using the chain operator %>>%
torch
’s “Hello World”torchvision
mlr3torch
in a NutshellLearner
smlr3pipelines::Graph
storch_tensor
sReLU
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::Graph
s
PipeOp
s in a Graph
Graph
is fully interoperable with other PipeOp
smlp_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_tensor
slazy_tensor
s, which wrap a torch::dataset
lazy_tensor
s happens lazily by internally building up a preprocessing Graph
GraphLearner
GraphLearner
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_tensor
s can be stored in data.frame
smlr3torch
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