Represents a neural network using a Graph that contains mostly PipeOpModules.
Arguments
- graph
- shapes_in
(named
integer)
Shape info of tensors that go intograph. Names must begraph$input$name, possibly in different order.- output_map
(
character)
Which ofgraph's outputs to use. Must be a subset ofgraph$output$name.- list_output
(
logical(1))
Whether output should be a list of tensors. IfFALSE(default), thenlength(output_map)must be 1.
Fields
graph::Graph
The graph (consisting primarily ofPipeOpModules) that is wrapped by the network. When it consists only ofPipeOpModules andPipeOpNOPs, the order in which the modules are called is determined once and$forward()calls them directly, which is much faster than running the graph. This is determined anew when PipeOps are added to or removed from the graph, but not when only its edges are changed, so the graph should not be modified in place. The field cannot be set to another graph, because the parameters of the modules are registered when the network is created. Create a newnn_graphinstead.input_map::character()
The names of the input arguments of the network. Unnamed arguments of$forward()are matched in this order.shapes_in::list()
The shapes of the input tensors of the network.output_map::character()
Which output elements of the graph are returned by the$forward()method.list_output::logical(1)
Whether the output is a list of tensors.module_list::nn_module_list
The list of modules in the network.list_output::logical(1)
Whether the output is a list of tensors.
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(),
pipeop_torch()
Examples
graph = mlr3pipelines::Graph$new()
graph$add_pipeop(po("module_1", module = nn_linear(10, 20)), clone = FALSE)
graph$add_pipeop(po("module_2", module = nn_relu()), clone = FALSE)
graph$add_pipeop(po("module_3", module = nn_linear(20, 1)), clone = FALSE)
graph$add_edge("module_1", "module_2")
graph$add_edge("module_2", "module_3")
network = nn_graph(graph, shapes_in = list(module_1.input = c(NA, 10)))
x = torch_randn(16, 10)
network(module_1.input = x)
#> torch_tensor
#> -0.2289
#> 0.1131
#> -0.1302
#> -0.1225
#> 0.0016
#> -0.3070
#> -0.4943
#> -0.0887
#> -0.2062
#> -0.1422
#> 0.1346
#> 0.2569
#> -0.0886
#> 0.0758
#> -0.3757
#> -0.2076
#> [ CPUFloatType{16,1} ][ grad_fn = <AddmmBackward0> ]