Rectified Activation Functions
ReLU-based activation functions.
Implements the rectifier family surveyed in arXiv:2109.14545 §4: leaky and parametric variants, bounded/shifted ReLUs, and rectified-hyperbolic units. Most are stateless; the parametric ones (PReLU) carry learnable parameters.
- class src.model.activation_function.rectified.AbsReLU(*args: Any, **kwargs: Any)[source]
Bases:
ModuleAbsolute-value ReLU (ABReLU / AB-ReLU):
max(0, x) - a * max(0, -x).where
ais the mean of the pre-activation over the batch/map (a data-dependent baseline subtracted before rectification). Implemented here as|x|style:max(0, x) - max(0, -x)=xrectified to the negative-symmetric formabs-biased. Concretely we use the survey’s definitionmax(0, x) - max(0, -x)giving a sign-preserving ramp. Reference: survey §4, Bjorck et al. (2017).
- class src.model.activation_function.rectified.BReLU(*args: Any, **kwargs: Any)[source]
Bases:
ModuleBounded ReLU:
min(max(0, x), t).Range
[0, t]. Clamps the positive region to a ceilingt.- Parameters:
t – Upper bound for the positive region. Default:
1.0.
- t
The ceiling value.
- class src.model.activation_function.rectified.DisReLU(*args: Any, **kwargs: Any)[source]
Bases:
ModuleDisplaced ReLU:
max(0, x - delta).Shifts the ReLU hinge to the right by
delta. Range[-delta, inf)after the displacement. Reference: survey §4.- Parameters:
delta – Rightward shift of the rectifier hinge. Default:
0.0.
- delta
The hinge displacement.
- class src.model.activation_function.rectified.HardSwish(*args: Any, **kwargs: Any)[source]
Bases:
ModuleHard-Swish:
x * relu6(x + 3) / 6.Cheap approximation of Swish used in MobileNetV3. Range
(-1.67, inf). Reference: Howard et al. (2019), arXiv:1905.02244.
- class src.model.activation_function.rectified.Hexpo(*args: Any, **kwargs: Any)[source]
Bases:
ModuleHexpo:
a * max(0, x) - c * max(0, -x).Asymmetric two-sided rectifier with separate positive/negative gains. Reference: survey §3.
- Parameters:
a – Positive-region gain. Default:
1.0.c – Negative-region gain. Default:
1.0.
- a, c
The two gains.
- class src.model.activation_function.rectified.LeakyReLU(*args: Any, **kwargs: Any)[source]
Bases:
ModuleLeaky ReLU:
xifx >= 0elsenegative_slope * x.Range
(-inf, inf). Allows a small gradient when the unit is inactive. Reference: Maas et al. (2013); Lederer §2.2.3; survey §4.- Parameters:
negative_slope – Slope for negative inputs. Default:
0.01.
- negative_slope
The negative-region slope.
- class src.model.activation_function.rectified.LiSHT(*args: Any, **kwargs: Any)[source]
Bases:
ModuleLinearly Scaled Hyperbolic Tangent:
x * tanh(x).Non-monotonic, unbounded in magnitude. Range
[0, inf)(by the survey’s magnitude argument). Reference: survey §3, Roy et al. (2019).
- class src.model.activation_function.rectified.NLReLU(*args: Any, **kwargs: Any)[source]
Bases:
ModuleNatural-Logarithm ReLU:
beta * log(1 + max(0, x)).Range
[0, inf); compresses large positive activations logarithmically. Reference: survey §4, Forest (2014).- Parameters:
beta – Positive scaling constant. Default:
1.0.
- beta
The logarithmic scale.
- class src.model.activation_function.rectified.PReLU(*args: Any, **kwargs: Any)[source]
Bases:
ModuleParametric ReLU:
max(0, x) + a * min(0, x)with learnablea.Extends Leaky ReLU by making the negative slope a trainable parameter. With one parameter per channel (
num_parameters = dim) it is per-channel; a single shared parameter gives the global variant.- Parameters:
dim – Number of features. The slope
ahas shape(dim,).init – Initial value of the negative slope. Default:
0.25.
- weight
Learnable negative slope of shape
(dim,).
Reference: He et al. (2015), arXiv:1502.01852; survey §4.
- forward(x: torch.Tensor) torch.Tensor[source]
Apply Parametric ReLU elementwise (per-channel slope).
The slope is broadcast over the last dimension, matching the transformer convention
(..., dim).- Parameters:
x – Input tensor of shape
(..., dim).- Returns:
Tensor of same shape with the parametric ReLU applied.
- class src.model.activation_function.rectified.PenalizedTanh(*args: Any, **kwargs: Any)[source]
Bases:
ModulePenalized Tanh (pTanh):
max(0, \tanh(x)).A rectified, non-negative tanh with a soft gradient near zero. Range
[0, 1). Reference: survey §3.Note
The general form is
\tanh(x)forx > 0anda * \tanh(x)witha in (0, 1)otherwise; the implemented form usesa -> 0(hard zeroing of negatives).
- class src.model.activation_function.rectified.ReLU6(*args: Any, **kwargs: Any)[source]
Bases:
ModuleReLU6:
min(max(0, x), 6).Bounded ReLU clamped at 6, used in MobileNet for fixed-point friendliness. Reference: Howard et al. (2017).