Activation functions introduce the non-linearity that lets neural networks approximate complex functions. Clorch provides every standard activation in two interchangeable forms: a stateful module form underDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/antlobach/clorch/llms.txt
Use this file to discover all available pages before exploring further.
clorch.nn (for use inside defmodel or nn/sequential) and a purely functional form under clorch.nn.functional (for direct call in a forward body). Both delegate to the same LibTorch C++ kernels.
Standard Activations
| Activation | clorch.nn module | clorch.nn.functional |
|---|---|---|
| ReLU | (nn/relu) | (F/relu x) |
| Sigmoid | (nn/sigmoid) | (F/sigmoid x) |
| Tanh | (nn/tanh) | (F/tanh x) |
| GeLU | (nn/gelu) | (F/gelu x) |
| SiLU (Swish) | (nn/silu) | (F/silu x) |
| Softmax | (nn/softmax dim) | (F/softmax x dim) |
- ReLU
- GeLU
- SiLU / Swish
- Softmax
The most widely-used activation for hidden layers. Computationally cheap and avoids the vanishing gradient problem for positive activations.
Enhanced and Specialized Activations
LeakyReLU
Passes negative inputs through with a small, fixed slope instead of zeroing them. Useful when dead neurons are a concern.- Module
- Functional
PReLU — Parametric ReLU
The negative slope is a learnable parameter rather than a fixed constant. BecausePReLURecord stores the slope as an nn/parameter, it participates in nn/parameters and gradient descent automatically.
- Module
- Functional
ELU — Exponential Linear Unit
Smooth for negative values (alpha * (exp(x) - 1)), which can help learning speed compared to ReLU.
- Module
- Functional
SELU — Scaled Exponential Linear Unit
Self-normalizing variant of ELU with fixed scale and alpha parameters. Designed for use withalpha-dropout.
CELU — Continuously Differentiable ELU
A variant of ELU that is continuously differentiable everywhere. Thealpha parameter controls the saturation value for negative inputs.
GLU — Gated Linear Unit
Splits the input in half alongdim and uses one half as a sigmoid gate on the other. Foundation of modern gated feed-forward blocks (SwiGLU, GeGLU).
- Module
- Functional
Softplus
A smooth approximation of ReLU defined aslog(1 + exp(beta * x)) / beta. The threshold parameter switches to a linear function for large values for numerical stability.
Log-Softmax and Softmin
Log-Sigmoid and Softsign
ReLU6
ReLU clamped to a maximum value of 6. Used in MobileNet-style architectures.RReLU — Randomized Leaky ReLU
Uses a random slope drawn from a uniform distribution during training and the midpoint during evaluation.Hardtanh
Clamps values to[min_val, max_val] with unit slope in between. Defaults to [-1, 1].
Threshold
Sets all values belowthreshold-val to value.
Shrinkage Functions
Shrinkage activations set small-magnitude values to zero, encouraging sparse representations.- Hardshrink
- Softshrink
- Tanhshrink
Sets values in
(-λ, λ) to zero; passes everything else through unchanged.Modern Activations
- Mish
- Hardswish
- Hardsigmoid
x * tanh(softplus(x)). Often outperforms ReLU and Swish in image models without any hyperparameter tuning.Comparison Table
| Activation | Output Range | Primary Use Case |
|---|---|---|
ReLU | [0, ∞) | Default hidden-layer activation, CNNs |
Sigmoid | (0, 1) | Binary classification output, gating |
Tanh | (−1, 1) | Centered output, RNN hidden states |
Softmax | (0, 1) per class | Multi-class classification output |
GeLU | (−0.17, ∞) | Transformer encoder/decoder layers |
SiLU | (−0.28, ∞) | Modern vision and language models |
LeakyReLU | (−∞, ∞) | GANs, models prone to dead neurons |
ELU | (−α, ∞) | Faster convergence vs ReLU in deep nets |
SELU | (−λα, ∞) | Self-normalizing networks |
GLU | (−∞, ∞) | Gated feed-forward blocks |
Softplus | (0, ∞) | Smooth approximation of ReLU |
Mish | (−0.31, ∞) | Image classification, drop-in ReLU swap |
Hardswish | [0, ∞) | Mobile / edge inference (efficient) |
Hardsigmoid | [0, 1] | Mobile / edge inference (efficient) |
Hardtanh | [min, max] | Quantization-aware training |
The
nn/silu and nn/hardswish module constructors return Clojure function wrappers (not native Module objects) because LibTorch does not expose separate module classes for these. They are fully compatible with nn/sequential and defmodel forward bodies.