class
Softshrink
extends
ModuleSoftshrink(lambd: float = 0.5)Soft Shrinkage activation function.
Applies element-wise:
Shrinks all values towards zero by : values outside the dead band are shifted, while values inside are zeroed. This is the proximal operator of the norm and appears in LASSO and iterative soft-thresholding algorithms (ISTA / FISTA).
Parameters
lambdfloat= 0.5Threshold . Default:
0.5.Notes
- Input: — any shape.
- Output: — same shape as input.
Unlike Hardshrink, Softshrink shifts surviving values toward
zero rather than preserving them exactly. This makes it a continuous
function and gives it a well-defined subgradient everywhere.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Softshrink(lambd=0.5)
>>> x = lucid.tensor([-1.5, -0.3, 0.0, 0.3, 1.5])
>>> m(x)
tensor([-1. , 0. , 0. , 0. , 1. ])
>>> # L1-proximal layer in an unrolled ISTA network
>>> m = nn.Softshrink(lambd=0.1)
>>> x = lucid.randn(8, 128)
>>> out = m(x)
>>> out.shape
(8, 128)Methods (3)
dunder
__init__
→None__init__(lambd: float = 0.5)Initialise the Softshrink 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.