as_learner_torch: Convert a Graph to a Torch Learner

View source: R/GraphLearnerTorch.R

as_learner_torchR Documentation

Convert a Graph to a Torch Learner

Description

Converts a Graph representing a deep learning pipeline into a GraphLearnerTorch. The graph either builds the network from PipeOpTorch operators and ends in a PipeOpTorchModel, or its base learner is a LearnerTorch, e.g. po("scale") %>>% lrn("classif.mlp"). The advantage over using as_learner() is that the resulting learner exposes methods like ⁠$dataset()⁠ and fields like ⁠$network⁠.

Usage

as_learner_torch(x, ...)

## S3 method for class 'Graph'
as_learner_torch(x, id = NULL, ...)

## S3 method for class 'PipeOp'
as_learner_torch(x, ...)

## S3 method for class 'GraphLearner'
as_learner_torch(x, id = x$id, ...)

Arguments

x

(any)
The object to convert, e.g. a Graph.

...

(any)
Unused.

id

(character(1))
The id of the learner. Defaults to "<task_type>.graph".

Value

GraphLearner

See Also

Other Graph Network: ModelDescriptor(), TorchIngressToken(), mlr_learners_torch_model, mlr_pipeops_module, mlr_pipeops_torch, mlr_pipeops_torch_ingress, mlr_pipeops_torch_ingress_categ, mlr_pipeops_torch_ingress_ltnsr, mlr_pipeops_torch_ingress_num, model_descriptor_to_learner(), model_descriptor_to_module(), model_descriptor_union(), nn_graph(), pipeop_torch()

Other Learner: GraphLearnerTorch, mlr_learners.ft_transformer, mlr_learners.mlp, mlr_learners.module, mlr_learners.realmlp, mlr_learners.tab_resnet, mlr_learners.tabm, mlr_learners.torch_featureless, mlr_learners_torch, mlr_learners_torch_image, mlr_learners_torch_model

Examples


graph = po("scale") %>>%
  po("torch_ingress_num") %>>%
  nn("linear", out_features = 10) %>>%
  nn("relu") %>>%
  nn("head") %>>%
  po("torch_loss", "cross_entropy") %>>%
  po("torch_optimizer", "adam", lr = 0.1) %>>%
  po("torch_model_classif", epochs = 1, batch_size = 32)

learner = as_learner_torch(graph)
learner$id
learner$param_set$set_values(linear.out_features = 20, torch_optimizer.lr = 0.01)

task = tsk("iris")
learner$train(task)
learner$network
learner$predict(task)

learner$dataset(task, "train")


mlr3torch documentation built on Oct. 30, 2026, 5:10 p.m.