Softshrink — PyTorch 2.7 documentation (original) (raw)

class torch.nn.Softshrink(lambd=0.5)[source][source]

Applies the soft shrinkage function element-wise.

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

Parameters

lambd (float) – the λ\lambda (must be no less than zero) value for the Softshrink formulation. Default: 0.5

Shape:

../_images/Softshrink.png

Examples:

m = nn.Softshrink() input = torch.randn(2) output = m(input)