class

Hardsigmoid

extendsModule
Hardsigmoid()
source

Hard Sigmoid activation function.

Applies element-wise:

Hardsigmoid(x)=max ⁣(0,min ⁣(1,x6+12))\text{Hardsigmoid}(x) = \max\!\left(0,\, \min\!\left(1,\, \frac{x}{6} + \frac{1}{2}\right)\right)

This is a piecewise-linear approximation of the logistic sigmoid that saturates to 0 for x3x \leq -3 and to 1 for x3x \geq 3, with a linear ramp in between. It avoids the exponential required by the exact sigmoid, making it suitable for resource-constrained deployments.

Notes

  • Input: ()(*) — any shape.
  • Output: ()(*) — same shape as input, values in [0,1][0, 1].

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Hardsigmoid()
>>> x = lucid.tensor([-4.0, -3.0, 0.0, 3.0, 4.0])
>>> m(x)
tensor([0.  , 0.  , 0.5 , 1.  , 1.  ])
>>> # Lightweight gating in mobile attention heads
>>> x = lucid.randn(2, 32)
>>> out = m(x)
>>> out.shape
(2, 32)

Methods (1)

fn

forward

Tensor
forward(x: Tensor)
source

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.