snn.activations¶
Base¶
Classic activations¶
- class snn.activations.Linear[source]¶
Bases:
ActivationIdentity / no-op activation. f(x) = x.
- class snn.activations.ReLU[source]¶
Bases:
ActivationRectified Linear Unit. f(x) = max(0, x).
- class snn.activations.LeakyReLU(alpha=0.01)[source]¶
Bases:
ActivationLeaky ReLU. f(x) = x if x > 0 else alpha*x.
- Parameters:
alpha (float) – Slope for negative inputs (default 0.01).
- class snn.activations.ELU(alpha=1.0)[source]¶
Bases:
ActivationExponential Linear Unit. f(x) = x if x > 0 else alpha*(exp(x)-1).
- Parameters:
alpha (float) – Scale for negative saturation region (default 1.0).
- class snn.activations.SELU[source]¶
Bases:
ActivationScaled ELU — self-normalising activation for deep networks.
- class snn.activations.Sigmoid[source]¶
Bases:
ActivationLogistic sigmoid. f(x) = 1 / (1 + exp(-x)).
- class snn.activations.Tanh[source]¶
Bases:
ActivationHyperbolic tangent. f(x) = tanh(x).
- class snn.activations.Softmax[source]¶
Bases:
ActivationSoftmax over the last axis. Typically used on the output layer for multi-class classification.
- class snn.activations.Softplus[source]¶
Bases:
ActivationSmooth approximation of ReLU. f(x) = log(1 + exp(x)).
- class snn.activations.Swish[source]¶
Bases:
ActivationSwish / SiLU. f(x) = x * sigmoid(x). Smooth, non-monotonic.
Non-linear / smooth activations¶
These activations are particularly effective when the target function is smooth or polynomial-like (e.g. y = x², signal reconstruction, physics- informed networks).
- class snn.activations.GELU[source]¶
Bases:
ActivationGaussian Error Linear Unit.
Uses the tanh approximation: f(x) ≈ 0.5 * x * (1 + tanh(√(2/π) * (x + 0.044715 * x³)))
The standard choice in modern Transformers (BERT, GPT). Smooth and non-monotonic; outperforms ReLU/ELU on many regression and NLP tasks.
- class snn.activations.PReLU(alpha=0.25)[source]¶
Bases:
ActivationParametric ReLU with a learnable negative slope.
f(x) = x if x ≥ 0 f(x) = alpha * x if x < 0
alphais updated by the optimiser just like any weight — pass the layer that contains this activation through aSequentialmodel andalphawill be trained automatically.- Parameters:
alpha (float) – Initial slope for the negative region (default 0.25).
- property params¶
- property grads¶
- class snn.activations.Sine[source]¶
Bases:
ActivationSine activation for SIREN (Sinusoidal Representation Networks).
f(x) = sin(x)
Designed for learning smooth, periodic, and polynomial-like functions. A network with sine activations can exactly represent derivatives of itself and naturally approximates functions like y = x², y = sin(x), or any smooth manifold.
Initialisation note: use
kernel_initializer="siren_uniform"(or scale weights by 1/fan_in for hidden layers) for best convergence. As a quick start,glorot_uniformworks for shallow networks.
- class snn.activations.Hardswish[source]¶
Bases:
ActivationHard Swish activation (MobileNetV3).
f(x) = x * ReLU6(x + 3) / 6
Piecewise-smooth approximation of Swish. Efficient for deployment while retaining the non-monotonic character of Swish.
- class snn.activations.BentIdentity[source]¶
Bases:
ActivationBent Identity activation.
f(x) = (√(x² + 1) − 1) / 2 + x
Smooth everywhere, non-saturating, and non-linear at every point. The derivative is always positive (≥ 1) so it avoids the dying-neuron problem while still introducing the curvature needed to fit polynomial or quadratic data. Good default for regression tasks.
- class snn.activations.Squareplus(b=4.0)[source]¶
Bases:
ActivationSquareplus — smooth, monotonic ReLU alternative.
f(x) = (x + √(x² + b)) / 2
Always positive and differentiable, with quadratic behaviour near the origin controlled by
b. Approaches ReLU as b → 0.- Parameters:
b (float) – Curvature at origin (default 4.0). Larger values = smoother transition.
Recent additions (efficiency + NLP)¶
- class snn.activations.ReLU6[source]¶
Bases:
ActivationReLU capped at 6. f(x) = min(max(0, x), 6).
Used in MobileNet and other efficient architectures. The cap at 6 improves fixed-point quantisation precision.
- class snn.activations.Hardsigmoid[source]¶
Bases:
ActivationPiecewise-linear sigmoid approximation.
f(x) = clip((x + 3) / 6, 0, 1)
Computationally cheaper than the exact sigmoid; commonly used in quantisation-friendly networks (MobileNetV3).
- class snn.activations.LogSoftmax[source]¶
Bases:
ActivationLog-softmax. f(x) = x − log(Σ exp(x)).
Numerically stable; used together with negative log-likelihood loss (equivalent to
CrossEntropy = NLLLoss(LogSoftmax(x))).
- class snn.activations.Sparsemax[source]¶
Bases:
ActivationSparsemax — differentiable sparse softmax (Martins & Astudillo 2016).
Like softmax but returns a sparse probability distribution: many outputs are exactly 0. Useful for attention mechanisms where hard, interpretable focus is desired.
f(x) = max(0, x − τ(x)) where τ is the threshold that makes outputs sum to 1.
- class snn.activations.CELU(alpha=1.0)[source]¶
Bases:
ActivationContinuously Differentiable ELU (Barron 2017).
f(x) = xifx >= 0elsealpha*(exp(x/alpha) - 1)Unlike ELU, CELU is continuously differentiable at
x=0for anyalpha.- Parameters:
alpha (float) – Scale for the negative branch (default 1.0).
- class snn.activations.Softsign[source]¶
Bases:
ActivationSoftsign activation.
f(x) = x / (1 + |x|).A smooth, bounded alternative to Tanh with heavier tails. Saturates more slowly, which can help with gradient flow.
Factory¶
- snn.activations.get(identifier)[source]¶
Return an
Activationinstance from a string or object.- Parameters:
identifier (str, Activation, or None) –
Noneor"linear"→Linear.- Raises:
ValueError – If the string key is not recognised.