class

Hardshrink

extendsModule
Hardshrink(lambd: float = 0.5)
source

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)

Methods (3)

dunder

__init__

None
__init__(lambd: float = 0.5)
source

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

fn

forward

Tensor
forward(x: Tensor)
source

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.