Learnable Activation Functions
Learnable / adaptive activation functions.
Implements:
RationalActivation— the Rational Activation Function (RAF) from Transformers with Learnable Activation Functions (Fang et al., EACL 2023, arXiv:2208.14111). A RAF is a learnable ratio of two low-degree polynomials (a Padé approximant), with five “version” variants (A/B/C/D/N) controlling the denominator form. The default configuration matches the paper: degree(5, 4), version"A"(the “safe” per-term absolute-value denominator), initialized by a least-squares fit to GELU on[-3, 3].- Note:
Despite being requested as “Rectified Activation Function”, the paper defines RAF = Rational Activation Function. This implementation follows the paper.
SwishTrainable— Swish with a learnablebetaparameter.Maxout— the Goodfellow et al. (2013) maxout unit.
- class src.model.activation_function.learnable.Maxout(*args: Any, **kwargs: Any)[source]
Bases:
ModuleMaxout unit:
max_{i=0..k-1} (W_i x + b_i).Computes
klinear projections of the input and takes the elementwise maximum. A universal approximator of any continuous function given enough pieces. Increases parameter count by a factor ofk.- Parameters:
dim – Input/output feature dimension.
num_pieces – Number of linear pieces
kto maximize over. Default:2.
- proj
Linear layer producing
dim * num_piecesoutputs.
Reference: Goodfellow et al. (2013), arXiv:1302.4389; Lederer §2.3.4.
- class src.model.activation_function.learnable.RationalActivation(*args: Any, **kwargs: Any)[source]
Bases:
ModuleRational Activation Function (RAF): a learnable Padé
P(x)/Q(x).\[F(x) = \frac{P(x)}{Q(x)} = \frac{\sum_{j=0}^{m} a_j x^{j}}{1 + R(x)}\]where the denominator term
R(x)depends onversion:"A"(default, “safe”):R(x) = \sum_{k} |b_k x^{k+1}|(abs per term)"B":R(x) = |\sum_{k} b_k x^{k+1}|(abs of the whole sum)"C":R(x) = |b_0 + b_1 x + \dots + b_n x^{n}|with floor0.1"D": like"B"but with uniform multiplicative noise on the denominator weights during training only."N":R(x) = \sum_{k} b_k x^{k+1}(no abs; can have poles)
Versions A/B/D keep
Q(x) >= 1(no division by zero); C keeps it>= 0.1; N is unsafe. The paper uses version"A".- Parameters:
degrees –
(m, n)numerator and denominator polynomial degrees. Default:(5, 4)(the paper default).version – Denominator form. One of
"A"(default),"B","C","D","N".approx_func – Initialization target. One of
"gelu"(default),"relu","leaky_relu","leaky_relu_0.1","sigmoid","tanh","swish","silu","identity".trainable – If
False, freeze the rational parameters. Default:True.input_scaling – If
True, apply per-token min-max scaling of the input to[-3, 3]before the rational (the RAFT preprocessing that keeps inputs inside the fitted range). Default:False.noise_eps – Std of the version-D multiplicative noise. Default:
0.1.
- numerator
Learnable parameter of shape
(m + 1,).
- denominator
Learnable parameter of shape
(n,).
- Reference:
Fang et al. (2023). Transformers with Learnable Activation Functions. arXiv:2208.14111.
- class src.model.activation_function.learnable.SwishTrainable(*args: Any, **kwargs: Any)[source]
Bases:
ModuleSwish with a learnable
beta:x * sigmoid(beta * x).When
beta -> 0the function approaches the linear/2 map; largebetarecovers ReLU. Shared-scalarbetaby default (per-channel viadim).- Parameters:
dim – If given,
betais per-channel of shape(dim,). IfNone,betais a single shared scalar. Default:None.beta_init – Initial value of
beta. Default:1.0(== SiLU).
- beta
Learnable Swish slope (scalar or
(dim,)).
Reference: Ramachandran et al. (2017), arXiv:1710.05941; survey §6.