Skip to content
class

Hardshrink

extendsModule
Hardshrink(lambd: float = 0.5)

Hard Shrinkage activation function.

Applies element-wise:

Hardshrink(x)={xif ∣x∣>λ0otherwise\text{Hardshrink}(x) = \begin{cases} x & \text{if } |x| > \lambda \\ 0 & \text{otherwise} \end{cases}

Values within the band [−λ,λ][-\lambda, \lambda] are zeroed out ("shrunk" to zero), while large values pass through unchanged. Hard shrinkage encourages sparse representations and is commonly used in sparse coding and wavelet-based signal processing.

Parameters

lambdfloat= 0.5
Threshold λ≥0\lambda \geq 0. Default: 0.5.

Notes

  • Input: (∗)(*) — any shape.
  • Output: (∗)(*) — same shape as input.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Hardshrink(lambd=0.5)
>>> x = lucid.tensor([-1.0, -0.4, 0.0, 0.4, 1.0])
>>> m(x)
tensor([-1.,  0.,  0.,  0.,  1.])
>>> # Sparsifying activation for dictionary learning
>>> m = nn.Hardshrink(lambd=1.0)
>>> x = lucid.randn(8, 64)
>>> out = m(x)
>>> out.shape
(8, 64)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(lambd: float = 0.5)

Initialise the Hardshrink module. See the class docstring for parameter semantics.

Instance methods

2
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.