Wraps a torch::nn_module into a LearnerTorch, so an architecture written directly in
torch can be trained, resampled and tuned like any other mlr3 learner without going through a
Graph of PipeOpTorch operators.
This is the shortest path from an existing torch model to mlr3, and the natural choice for an
architecture whose forward pass is easier to write as code than to assemble from operators.
The module receives the task's features as its arguments, which ingress_tokens maps.
Super classes
mlr3::Learner -> LearnerTorch -> LearnerTorchModule
Methods
Inherited methods
mlr3::Learner$base_learner()mlr3::Learner$configure()mlr3::Learner$encapsulate()mlr3::Learner$help()mlr3::Learner$predict()mlr3::Learner$predict_newdata()mlr3::Learner$reset()mlr3::Learner$selected_features()mlr3::Learner$train()LearnerTorch$dataset()LearnerTorch$format()LearnerTorch$marshal()LearnerTorch$print()LearnerTorch$unmarshal()
LearnerTorchModule$new()
Creates a new instance of this R6 class.
Usage
LearnerTorchModule$new(
module_generator = NULL,
param_set = NULL,
ingress_tokens = NULL,
task_type,
properties = NULL,
optimizer = NULL,
loss = NULL,
callbacks = list(),
packages = character(0),
feature_types = NULL,
predict_types = NULL,
target_batchgetter = NULL
)Arguments
module_generator(
functionornn_module_generator)
Ann_module_generatororfunctionreturning annn_module. Both must take as argument thetaskfor which to construct the network. Other arguments to its initialize method can be provided as parameters.param_set(
NULLorParamSet)
If provided, contains the parameters for the module_generator. IfNULL, parameters will be inferred from the module_generator.ingress_tokens(
listofTorchIngressToken())
A list with ingress tokens that defines how the dataset will be defined. The names must correspond to the arguments of the network's forward method. For numeric, categorical, and lazy tensor features, you can useingress_num(),ingress_categ(), andingress_ltnsr()to create them.task_type(
character(1))
The task type, either"classif" or"regr".task_type(
character(1))
The task type.properties(
NULLorcharacter())
The properties of the learner. Defaults to all available properties for the given task type.optimizer(
TorchOptimizer)
The optimizer to use for training. Per default, adam is used.loss(
TorchLoss)
The loss used to train the network. Per default, mse is used for regression and cross_entropy for classification.callbacks(
list()ofTorchCallbacks)
The callbacks. Must have unique ids.packages(
character())
The R packages this object depends on.feature_types(
NULLorcharacter())
The feature types. Defaults to all available feature types.predict_types(
character())
The predict types. Seemlr_reflections$learner_predict_typesfor available values.target_batchgetter(
function()orNULL)
Converts the target columns of a batch into the target tensorythat the loss is applied to. Takes an argumentdata, adata.tablewith only the target columns, and optionally an argumentx, the named list of feature tensors of the batch, which is what a target that is a function of the input needs, seeget_target_batchgetter(). IfNULL(default), it is taken from the task viaget_target_batchgetter(), which the built-in task types provide, but aTaskTorchonly if it has no target at all, in which case the batches have noyelement.
Examples
nn_one_layer = nn_module("nn_one_layer",
initialize = function(task, size_hidden) {
self$first = nn_linear(task$n_features, size_hidden)
self$second = nn_linear(size_hidden, output_dim_for(task))
},
# argument x corresponds to the ingress token x
forward = function(x) {
x = self$first(x)
x = nnf_relu(x)
self$second(x)
}
)
learner = lrn("classif.module",
module_generator = nn_one_layer,
ingress_tokens = list(x = ingress_num()),
epochs = 10,
size_hidden = 20,
batch_size = 16
)
task = tsk("iris")
learner$train(task)
learner$network
#> An `nn_module` containing 163 parameters.
#>
#> ── Modules ─────────────────────────────────────────────────────────────────────
#> • first: <nn_linear> #100 parameters
#> • second: <nn_linear> #63 parameters