fn

hardsigmoid

Tensor
hardsigmoid(x: Tensor)
source

Hard sigmoid — piecewise linear approximation of sigmoid.

Avoids the cost of an exponential by clipping a scaled-and-shifted identity to [0,1][0, 1]. Widely used as the gating function inside quantised mobile networks (MobileNetV3 Squeeze-and-Excitation blocks).

Parameters

xTensor
Input tensor of any shape; activation is element-wise.

Returns

Tensor

Activated tensor with the same shape as x, values in [0,1][0, 1].

Notes

HardSigmoid(x)={0x3x/6+1/23<x<31x3\text{HardSigmoid}(x) = \begin{cases} 0 & x \le -3 \\ x/6 + 1/2 & -3 < x < 3 \\ 1 & x \ge 3 \end{cases}

Matches sigmoid exactly at x=0x = 0 (both return 0.5) but is only piecewise-linear. Derivative is 1/61/6 in the linear region and 00 in the saturated regions.

Examples

>>> import lucid
>>> from lucid.nn.functional import hardsigmoid
>>> x = lucid.tensor([-4.0, -1.0, 0.0, 1.0, 4.0])
>>> hardsigmoid(x)
Tensor([0.0000, 0.3333, 0.5000, 0.6667, 1.0000])