
Remap Layer Block Arguments for Model Specification
inp_spec.Rd
Creates a wrapper function around a Keras layer block to rename its
arguments. This is a powerful helper for defining the layer_blocks
in
create_keras_functional_spec()
and create_keras_sequential_spec()
,
allowing you to connect reusable blocks into a model graph without writing
verbose anonymous functions.
Value
A new function (a closure) that wraps the block
function with
renamed arguments, ready to be used in a layer_blocks
list.
Details
inp_spec()
makes your model definitions cleaner and more readable. It
handles the metaprogramming required to create a new function with the
correct argument names, while preserving the original block's hyperparameters
and their default values.
The function supports two modes of operation based on input_map
:
Single Input Renaming: If
input_map
is a single character string, the wrapper function renames the first argument of theblock
function to the provided string. This is the common case for blocks that take a single tensor input.Multiple Input Mapping: If
input_map
is a named character vector, it provides an explicit mapping from new argument names (the names of the vector) to the original argument names in theblock
function (the values of the vector). This is used for blocks with multiple inputs, like a concatenation layer.
Examples
if (FALSE) { # \dontrun{
# --- Example Blocks ---
# A standard dense block with one input tensor and one hyperparameter.
dense_block <- function(tensor, units = 16) {
tensor |> keras3::layer_dense(units = units, activation = "relu")
}
# A block that takes two tensors as input.
concat_block <- function(input_a, input_b) {
keras3::layer_concatenate(list(input_a, input_b))
}
# An output block with one input.
output_block <- function(tensor) {
tensor |> keras3::layer_dense(units = 1)
}
# --- Usage ---
layer_blocks <- list(
main_input = keras3::layer_input,
path_a = inp_spec(dense_block, "main_input"),
path_b = inp_spec(dense_block, "main_input"),
concatenated = inp_spec(
concat_block,
c(path_a = "input_a", path_b = "input_b")
),
output = inp_spec(output_block, "concatenated")
)
} # }