new_act_fn: Custom Activation Function Constructor

View source: R/act-funnew.R

new_act_fnR Documentation

Custom Activation Function Constructor

Description

[Experimental]

Wraps a user-supplied function into a validated custom activation, ensuring it accepts and returns a torch_tensor. Performs an eager dry-run probe at definition time so errors surface early, and wraps the function with a call-time type guard for safety.

Usage

new_act_fn(fn, probe = TRUE, .name = "<custom>")

Arguments

fn

A function taking a single tensor argument and returning a tensor. E.g. ⁠\(x) torch::torch_tanh(x)⁠.

probe

Logical. If TRUE (default), runs a dry-run with a small dummy tensor at definition time to catch obvious errors early.

.name

A string to be stored in an attribute. Nothing special, except it is used when displaying the info of a trained neural network model. Default is "<custom>".

Value

An object of class c("custom_activation", "parameterized_activation"), compatible with act_funs().

Examples

## Not run: 
\donttest{
act_funs(relu, elu, new_act_fn(\(x) torch::torch_tanh(x)))
act_funs(new_act_fn(\(x) torch::nnf_silu(x)))
}

## End(Not run)


kindling documentation built on March 3, 2026, 9:07 a.m.