Hardshrink — PyTorch 2.7 documentation (original) (raw)

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

Applies the Hard Shrinkage (Hardshrink) function element-wise.

Hardshrink is defined as:

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

Parameters

lambd (float) – the λ\lambda value for the Hardshrink formulation. Default: 0.5

Shape:

../_images/Hardshrink.png

Examples:

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