View source: R/nn_functional.R
| ggml_layer_custom | R Documentation |
Turns an arbitrary composition of ggml operations into a reusable functional
layer. ggml_layer_custom() does not build anything itself — it
returns a layer function that behaves like any other
ggml_layer_*() and can be piped into a model.
ggml_layer_custom(forward, name = NULL, output_shape = NULL)
forward |
A function of |
name |
Optional character name for the layer. Used as the base for auto-generated node names. |
output_shape |
Output shape excluding the batch dimension. Defaults to
|
The forward function is called once while the compute graph is being
built, with the active ggml_context and the parent tensor. Because
the returned tensor becomes an ordinary graph node, gradients flow through a
custom layer automatically — as long as every operation used inside
forward implements a backward pass.
A layer function of (x, name = NULL) that appends a custom
node to the graph and returns the new ggml_tensor_node.
Custom layers are stateless: forward maps activations to activations
and owns no trainable parameters. Put learnable weights in a neighbouring
ggml_layer_dense(), or use the autograd API (ag_*) when a layer
needs its own parameters.
# Mish activation: x * tanh(softplus(x))
layer_mish <- ggml_layer_custom(
name = "mish",
forward = function(ctx, x) ggml_mul(ctx, x, ggml_tanh(ctx, ggml_softplus(ctx, x)))
)
inp <- ggml_input(shape = 8L)
out <- inp |>
ggml_layer_dense(16L) |>
layer_mish() |>
ggml_layer_dense(2L, activation = "softmax")
model <- ggml_model(inputs = inp, outputs = out)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.