Skip to content
fn

hardswish

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

Hard Swish activation — piecewise linear approximation of silu.

Introduced in MobileNetV3 (Howard et al. 2019) as a compute-friendly replacement for the sigmoid in Swish: the exponential is approximated by a clipped linear function, which is far cheaper on mobile / quantised hardware while preserving the shape of the activation.

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.

Notes

HardSwish(x)=x⋅ReLU6(x+3)6={0x≤−3x(x+3)/6−3<x<3xx≥3\text{HardSwish}(x) = x \cdot \frac{\text{ReLU6}(x + 3)}{6} = \begin{cases} 0 & x \le -3 \\ x(x+3)/6 & -3 < x < 3 \\ x & x \ge 3 \end{cases}

Continuous but only piecewise-C1C^1 (the second derivative jumps at x=±3x = \pm 3). Cheap to evaluate and to quantise — preferred over silu in mobile architectures.

Examples

>>> import lucid
>>> from lucid.nn.functional import hardswish
>>> x = lucid.tensor([-4.0, -1.0, 0.0, 1.0, 4.0])
>>> hardswish(x)
tensor([-0., -0.3333, 0., 0.6667, 4.])

Used by 2