Skip to content
fn

hardsigmoid

→Tensor
hardsigmoid(input: Tensor, inplace: bool = False)

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

inputTensor
Input tensor of any shape; activation is element-wise.
inplacebool= False
If True, write the result into input and return input.

Returns

Tensor

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

Notes

HardSigmoid(x)={0x≤−3x/6+1/2−3<x<31x≥3\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., 0.3333, 0.5, 0.6667, 1.])

Used by 2