Helper function to create a custom PipeOpTorch class for the most common cases.
A practical guide to this function is the article
Writing your own PipeOpTorch.
For more information and the more general case, see the Inheriting section of PipeOpTorch.
The function works similarly to nn_module(), except that
$initialize() can take two further arguments that the PipeOp supplies: shapes_in and task
and that it also needs to implement the method $shapes_out() which belongs to the PipeOpTorch.
Usage
pipeop_torch(
id,
initialize = NULL,
forward,
shapes_out,
param_set = NULL,
in_channels = NULL,
out_channels = 1L,
packages = character(0),
tags = NULL,
classname = NULL,
parent_env = parent.frame()
)Arguments
- id
(
character(1))
The id for of the new object.- initialize
(
functionorNULL)
The$initialize()method of the module. Its arguments become the hyperparameters of thePipeOp, except forshapes_in(the input shapes, named after the input channels) andtask(theTaskorNULL), which are supplied by thePipeOpand passed only if the function declares them.- forward
(
function)
The$forward()method of the module.- shapes_out
(
function)
The shapes of the tensors that the module produces, as a function ofshapes_in,param_valsandtask– only the arguments that are declared are passed, so an operator whose output shape depends on nothing else is written asfunction(shapes_in). Any dimension ofshapes_incan beNA, i.e. unknown, so this function must not assume that a dimension it reads is known; see the Inheriting section ofPipeOpTorch.- param_set
(
ParamSetorNULL)
The parameter set. If left asNULL(default), it is inferred from the arguments ofinitialize: All arguments butshapes_inandtaskbecome an untyped parameter tagged"train", and additionally"required"if it has no default.- in_channels
(
character()orinteger(1)orNULL)
The input channels, either as names or as a count, where0means a single vararg channel. IfNULL(default), the arguments offorwardare used.- out_channels
(
character()orinteger(1))
The output channels, either as names or as a count,1by default. A module with more than one output channel must return alist(), in the order of the channels.- packages
(
character())
The R packages this object depends on.(
character())
Tags for thePipeOp. The tag"torch"is always added.- classname
(
character(1))
The class name of the generatedR6Class. By default it is derived fromid: a leading"nn_"is dropped, the remaining_-separated words are capitalized and pasted together, and"PipeOpTorch"is prepended, so the id"nn_scale"gives"PipeOpTorchScale".- parent_env
(
environment)
The environment in which the module's methods are evaluated, the calling environment by default, as fornn_module().initializeandforwardbecome methods of the module and are therefore evaluated in this environment rather than in the one they were written in;shapes_out, which is not a method of the module, keeps its own environment. The two only differ when the functions are written somewhere other than the caller ofpipeop_torch(), e.g. in a function that wraps it.
Value
An R6Class generator inheriting from PipeOpTorch.
See also
Other Graph Network:
ModelDescriptor(),
TorchIngressToken(),
as_learner_torch(),
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()
Examples
# A layer that scales its input by a learned factor
# Note that the number of features is read from the input shape
PipeOpTorchCustomScale = pipeop_torch("nn_custom_scale",
initialize = function(shapes_in, init = 1) {
self$weight = nn_parameter(torch_full(tail(shapes_in[[1L]], 1L), init))
},
forward = function(input) input * self$weight,
shapes_out = function(shapes_in) shapes_in # scaling leaves the shape as it is
)
po_custom_scale = PipeOpTorchCustomScale$new()
# `init` is a hyperparameter, the number of features is not
po_custom_scale$param_set$ids()
#> [1] "init"
po_custom_scale$shapes_out(list(c(NA, 4)))
#> $output
#> [1] NA 4
#>
# the operator can now be used like any other, and the module is built with 4 features
md = po("torch_ingress_num") %>>% po_custom_scale %>>% po("nn_head")
network = model_descriptor_to_module(md$train(tsk("iris"))[[1L]])
network
#> An `nn_module` containing 19 parameters.
#>
#> ── Modules ─────────────────────────────────────────────────────────────────────
#> • module_list: <nn_module_list> #19 parameters
# To use it via `nn("custom_scale")` or `po("nn_custom_scale")` we could run the line below:
# mlr_pipeops$add("nn_custom_scale", PipeOpTorchCustomScale)