View source: R/torch-code-gen.R
| nn_module_generator | R Documentation |
nn_module_generator() is a generalized function that generates neural network
module expressions for various architectures. It provides a flexible framework for creating
custom neural network modules by parameterizing layer types, construction arguments, and
forward pass behavior.
While designed primarily for {torch} modules, it can work with custom layer implementations
from the current environment, including user-defined layers like RBF networks, custom
attention mechanisms, or other novel architectures.
This function serves as the foundation for specialized generators like ffnn_generator()
and rnn_generator(), but can be used directly to create custom architectures.
nn_module_generator(
nn_name = "nnModule",
nn_layer = NULL,
out_nn_layer = NULL,
nn_layer_args = list(),
layer_arg_fn = NULL,
forward_extract = NULL,
before_output_transform = NULL,
after_output_transform = NULL,
last_layer_args = list(),
hd_neurons,
no_x,
no_y,
activations = NULL,
output_activation = NULL,
bias = TRUE,
eval = FALSE,
.env = parent.frame(),
...
)
nn_name |
Character string specifying the name of the generated neural network module class.
Default is |
nn_layer |
The type of neural network layer to use. Can be specified as:
The layer constructor is first searched in the current environment, then in parent
environments, and finally falls back to the |
out_nn_layer |
Default
Internally, it almost works the same as |
nn_layer_args |
Named list of additional arguments passed to the layer constructor
specified by |
layer_arg_fn |
Optional function or formula that generates layer-specific construction arguments. Can be specified as:
The formula/function should return a named list of arguments to pass to the layer constructor. Available variables in formula context:
If |
forward_extract |
Optional formula or function that processes layer outputs in the forward pass.
Useful for layers that return complex structures (e.g., RNNs return
Common patterns:
If |
before_output_transform |
Optional formula or function that transforms input before the output layer. This is applied after the last hidden layer (and its activation) but before the output layer. Can be specified as:
Common patterns:
If |
after_output_transform |
Optional formula or function that transforms the output after the output layer.
This is applied after
Common patterns:
If |
last_layer_args |
Optional named list or formula specifying additional arguments
for the output layer only. These arguments are appended to the output layer constructor
after the arguments from
This is useful when you need to override or add specific parameters to the final layer
without affecting hidden layers. For example, in CNNs you might want a different kernel
size for the output layer, or in RNNs you might want to disable bias in the final linear
projection. Arguments in |
hd_neurons |
Integer vector specifying the number of neurons (hidden units) in each hidden layer. The length determines the number of hidden layers in the network. Must contain at least one element. |
no_x |
Integer specifying the number of input features (input dimension). |
no_y |
Integer specifying the number of output features (output dimension). |
activations |
Activation function specifications for hidden layers. Can be:
If a single activation is provided, it will be replicated across all hidden layers. Otherwise, the length should match the number of hidden layers. |
output_activation |
Optional activation function for the output layer.
Same format as |
bias |
Logical indicating whether to include bias terms in layers.
Default is |
eval |
Logical indicating whether to evaluate the generated expression immediately.
If |
.env |
Default is |
... |
Additional arguments passed to layer constructors or for future extensions. |
If eval = FALSE (default): A language object (unevaluated expression) representing
a torch::nn_module definition. This expression can be evaluated with eval() to
create the module class, which can then be instantiated with eval(result)() to
create a model instance.
If eval = TRUE: An instantiated nn_module class constructor that can be called
directly to create model instances (e.g., result()).
## Not run:
\donttest{
if (torch::torch_is_installed()) {
# Basic usage with formula interface
nn_module_generator(
nn_name = "MyGRU",
nn_layer = "nn_gru",
layer_arg_fn = ~ if (.is_output) {
list(.in, .out)
} else {
list(input_size = .in, hidden_size = .out,
num_layers = 1L, batch_first = TRUE)
},
forward_extract = ~ .[[1]],
before_output_transform = ~ .[, .$size(2), ],
hd_neurons = c(128, 64, 32),
no_x = 20,
no_y = 5,
activations = "relu"
)
# LSTM with cleaner syntax
nn_module_generator(
nn_name = "MyLSTM",
nn_layer = "nn_lstm",
layer_arg_fn = ~ list(
input_size = .in,
hidden_size = .out,
batch_first = TRUE
),
forward_extract = ~ .[[1]],
before_output_transform = ~ .[, .$size(2), ],
hd_neurons = c(64, 32),
no_x = 10,
no_y = 2
)
# CNN with global average pooling
nn_module_generator(
nn_name = "SimpleCNN",
nn_layer = "nn_conv1d",
layer_arg_fn = ~ list(
in_channels = .in,
out_channels = .out,
kernel_size = 3L,
padding = 1L
),
before_output_transform = ~ .$mean(dim = 2),
hd_neurons = c(16, 32, 64),
no_x = 1,
no_y = 10,
activations = "relu"
)
# CNN with after_output_transform (pooling applied AFTER output layer)
nn_module_generator(
nn_name = "CNN1DClassifier",
nn_layer = "nn_conv1d",
layer_arg_fn = ~ if (.is_output) {
list(.in, .out)
} else {
list(
in_channels = .in,
out_channels = .out,
kernel_size = 3L,
stride = 1L,
padding = 1L
)
},
after_output_transform = ~ .$mean(dim = 2),
last_layer_args = list(kernel_size = 1, stride = 2),
hd_neurons = c(16, 32, 64),
no_x = 1,
no_y = 10,
activations = "relu"
)
} else {
message("torch not installed - skipping examples")
}
}
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.