snn.activations

Base

class snn.activations.Activation[source]

Bases: object

Base class for all activation functions.

forward(x)[source]
backward(grad)[source]

Classic activations

class snn.activations.Linear[source]

Bases: Activation

Identity / no-op activation. f(x) = x.

forward(x)[source]
backward(grad)[source]
class snn.activations.ReLU[source]

Bases: Activation

Rectified Linear Unit. f(x) = max(0, x).

forward(x)[source]
backward(grad)[source]
class snn.activations.LeakyReLU(alpha=0.01)[source]

Bases: Activation

Leaky ReLU. f(x) = x if x > 0 else alpha*x.

Parameters:

alpha (float) – Slope for negative inputs (default 0.01).

forward(x)[source]
backward(grad)[source]
class snn.activations.ELU(alpha=1.0)[source]

Bases: Activation

Exponential Linear Unit. f(x) = x if x > 0 else alpha*(exp(x)-1).

Parameters:

alpha (float) – Scale for negative saturation region (default 1.0).

forward(x)[source]
backward(grad)[source]
class snn.activations.SELU[source]

Bases: Activation

Scaled ELU — self-normalising activation for deep networks.

forward(x)[source]
backward(grad)[source]
class snn.activations.Sigmoid[source]

Bases: Activation

Logistic sigmoid. f(x) = 1 / (1 + exp(-x)).

forward(x)[source]
backward(grad)[source]
class snn.activations.Tanh[source]

Bases: Activation

Hyperbolic tangent. f(x) = tanh(x).

forward(x)[source]
backward(grad)[source]
class snn.activations.Softmax[source]

Bases: Activation

Softmax over the last axis. Typically used on the output layer for multi-class classification.

forward(x)[source]
backward(grad)[source]
class snn.activations.Softplus[source]

Bases: Activation

Smooth approximation of ReLU. f(x) = log(1 + exp(x)).

forward(x)[source]
backward(grad)[source]
class snn.activations.Swish[source]

Bases: Activation

Swish / SiLU. f(x) = x * sigmoid(x). Smooth, non-monotonic.

forward(x)[source]
backward(grad)[source]
class snn.activations.Mish[source]

Bases: Activation

Mish. f(x) = x * tanh(softplus(x)). Self-regularising, smooth.

forward(x)[source]
backward(grad)[source]

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: Activation

Gaussian 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.

forward(x)[source]
backward(grad)[source]
class snn.activations.PReLU(alpha=0.25)[source]

Bases: Activation

Parametric ReLU with a learnable negative slope.

f(x) = x if x ≥ 0 f(x) = alpha * x if x < 0

alpha is updated by the optimiser just like any weight — pass the layer that contains this activation through a Sequential model and alpha will be trained automatically.

Parameters:

alpha (float) – Initial slope for the negative region (default 0.25).

forward(x)[source]
backward(grad)[source]
property params
property grads
class snn.activations.Sine[source]

Bases: Activation

Sine 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_uniform works for shallow networks.

forward(x)[source]
backward(grad)[source]
class snn.activations.Hardswish[source]

Bases: Activation

Hard 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.

forward(x)[source]
backward(grad)[source]
class snn.activations.BentIdentity[source]

Bases: Activation

Bent 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.

forward(x)[source]
backward(grad)[source]
class snn.activations.Squareplus(b=4.0)[source]

Bases: Activation

Squareplus — 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.

forward(x)[source]
backward(grad)[source]

Recent additions (efficiency + NLP)

class snn.activations.ReLU6[source]

Bases: Activation

ReLU 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.

forward(x)[source]
backward(grad)[source]
class snn.activations.Hardsigmoid[source]

Bases: Activation

Piecewise-linear sigmoid approximation.

f(x) = clip((x + 3) / 6, 0, 1)

Computationally cheaper than the exact sigmoid; commonly used in quantisation-friendly networks (MobileNetV3).

forward(x)[source]
backward(grad)[source]
class snn.activations.LogSoftmax[source]

Bases: Activation

Log-softmax. f(x) = x − log(Σ exp(x)).

Numerically stable; used together with negative log-likelihood loss (equivalent to CrossEntropy = NLLLoss(LogSoftmax(x))).

forward(x)[source]
backward(grad)[source]
class snn.activations.Sparsemax[source]

Bases: Activation

Sparsemax — 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.

forward(x)[source]
backward(grad)[source]
class snn.activations.CELU(alpha=1.0)[source]

Bases: Activation

Continuously Differentiable ELU (Barron 2017).

f(x) = x if x >= 0 else alpha*(exp(x/alpha) - 1)

Unlike ELU, CELU is continuously differentiable at x=0 for any alpha.

Parameters:

alpha (float) – Scale for the negative branch (default 1.0).

forward(x)[source]
backward(grad)[source]
class snn.activations.Softsign[source]

Bases: Activation

Softsign activation. f(x) = x / (1 + |x|).

A smooth, bounded alternative to Tanh with heavier tails. Saturates more slowly, which can help with gradient flow.

forward(x)[source]
backward(grad)[source]
class snn.activations.Tanhshrink[source]

Bases: Activation

Tanhshrink activation. f(x) = x - tanh(x).

A soft-thresholding function that shrinks values toward zero. Used in sparse autoencoders and shrinkage estimators.

forward(x)[source]
backward(grad)[source]

Factory

snn.activations.get(identifier)[source]

Return an Activation instance from a string or object.

Parameters:

identifier (str, Activation, or None) – None or "linear"Linear.

Raises:

ValueError – If the string key is not recognised.