Skip to content
fn

softshrink

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

Soft shrinkage operator — proximal operator of λ∥⋅∥1\lambda \|\cdot\|_1.

Translates the input toward zero by λ\lambda and zeros out everything in the dead zone [−λ,λ][-\lambda, \lambda]. Used in iterative-shrinkage solvers (ISTA / FISTA) and in sparse autoencoder decoders.

Parameters

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

Returns

Tensor

Shrunk tensor with the same shape as input.

Notes

SoftShrink(x)={x−λx>λx+λx<−λ0otherwise\text{SoftShrink}(x) = \begin{cases} x - \lambda & x > \lambda \\ x + \lambda & x < -\lambda \\ 0 & \text{otherwise} \end{cases}

Continuous everywhere (unlike hardshrink), with derivative 11 outside the dead zone and 00 inside it. Coincides with the closed-form solution of arg⁡min⁡y12(y−x)2+λ∣y∣\arg\min_y \tfrac12 (y - x)^2 + \lambda |y|.

Examples

>>> import lucid
>>> from lucid.nn.functional import softshrink
>>> x = lucid.tensor([-1.0, -0.3, 0.0, 0.3, 1.0])
>>> softshrink(x, lambd=0.5)
tensor([-0.5, 0., 0., 0., 0.5])

Used by 1