| pipeop_torch | R Documentation |
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.
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()
)
id |
( |
initialize |
( |
forward |
( |
shapes_out |
( |
param_set |
( |
in_channels |
( |
out_channels |
( |
packages |
( |
tags |
( |
classname |
( |
parent_env |
( |
An R6Class generator inheriting from PipeOpTorch.
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()
# 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()
po_custom_scale$shapes_out(list(c(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
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.