Skip to content
fn

hardshrink

→Tensor
hardshrink(input: Tensor, lambd: float = 0.5)

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

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

Returns

Tensor

input with every entry in [−λ,λ][-\lambda, \lambda] set to zero.

Notes

HardShrink(x)={x∣x∣>λ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., 0., 0., 0., 1.])

Used by 1