fn

hardshrink

Tensor
hardshrink(x: Tensor, lambd: float = 0.5)
source

Hard shrinkage operator.

Sets elements with magnitude below λ\lambda to zero, leaves larger-magnitude elements untouched. Standard tool for sparse coding and the proximal operator of the 0\ell_0 "pseudonorm" on a coefficient-by-coefficient basis.

Parameters

xTensor
Input tensor.
lambdfloat= 0.5
Threshold λ0\lambda \ge 0. Default 0.5.

Returns

Tensor

Shrunk tensor with the same shape as x.

Notes

HardShrink(x)={xx>λ0otherwise\text{HardShrink}(x) = \begin{cases} x & |x| > \lambda \\ 0 & \text{otherwise} \end{cases}

Discontinuous at x=±λx = \pm\lambda — for an everywhere-continuous alternative use softshrink (the proximal operator of the 1\ell_1 norm).

Examples

>>> import lucid
>>> from lucid.nn.functional import hardshrink
>>> x = lucid.tensor([-1.0, -0.3, 0.0, 0.3, 1.0])
>>> hardshrink(x, lambd=0.5)
Tensor([-1.0000,  0.0000,  0.0000,  0.0000,  1.0000])