| nn_arch | R Documentation |
nn_arch() defines an architecture specification object consumed by
train_nn() via architecture (or legacy arch).
Conceptual workflow:
Define architecture with nn_arch().
Train with train_nn(..., architecture = <nn_arch>).
Predict with predict().
Architecture fields mirror nn_module_generator() and are passed through
without additional branching logic.
nn_arch(
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(),
input_transform = NULL
)
nn_name |
Character. Name of the generated module class. Default |
nn_layer |
Layer type. See |
out_nn_layer |
Optional. Layer type forced on the last layer. Default |
nn_layer_args |
Named list. Additional arguments passed to every layer constructor.
Default |
layer_arg_fn |
Formula or function. Generates per-layer constructor arguments.
Default |
forward_extract |
Formula or function. Processes layer output in the forward pass.
Default |
before_output_transform |
Formula or function. Transforms input before the output
layer. Default |
after_output_transform |
Formula or function. Transforms output after the output
layer. Default |
last_layer_args |
Named list or formula. Extra arguments for the output layer only.
Default |
input_transform |
Formula or function. Transforms the entire input tensor
before training begins. Applied once to the full dataset tensor, not per-batch.
Transforms must therefore be independent of batch size. Safe examples:
|
An object of class c("nn_arch", "kindling_arch"), implemented as a
named list of nn_module_generator() arguments with an "env" attribute
capturing the calling environment for custom layer resolution.
if (torch::torch_is_installed()) {
# Standard architecture object for train_nn()
std_arch = nn_arch(nn_name = "mlp_model")
# GRU architecture spec
gru_arch = nn_arch(
nn_name = "GRU",
nn_layer = "torch::nn_gru",
layer_arg_fn = ~ if (.is_output) {
list(.in, .out)
} else {
list(input_size = .in, hidden_size = .out, batch_first = TRUE)
},
out_nn_layer = "torch::nn_linear",
forward_extract = ~ .[[1]],
before_output_transform = ~ .[, .$size(2), ],
input_transform = ~ .$unsqueeze(2)
)
# Custom layer architecture (resolved from calling environment)
custom_linear = torch::nn_module(
"CustomLinear",
initialize = function(in_features, out_features, bias = TRUE) {
self$layer = torch::nn_linear(in_features, out_features, bias = bias)
},
forward = function(x) self$layer(x)
)
custom_arch = nn_arch(
nn_name = "CustomMLP",
nn_layer = ~ custom_linear
)
model = train_nn(
Sepal.Length ~ .,
data = iris[, 1:4],
hidden_neurons = c(64, 32),
activations = "relu",
epochs = 50,
architecture = gru_arch
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.