class
Hardshrink
extends
ModuleHardshrink(lambd: float = 0.5)Hard Shrinkage activation function.
Applies element-wise:
Values within the band 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.5Threshold . 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)Initialise the Hardshrink module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor)Apply the activation function element-wise.
Parameters
inputTensorInput tensor of arbitrary shape.
Returns
TensorOutput tensor of the same shape as input.
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.