fn

tanhshrink

Tensor
tanhshrink(x: Tensor)
source

Tanh shrinkage activation.

Behaves like a smooth high-pass filter: subtracts the tanh-saturated component of the input, leaving small linear residuals near zero and near-linear behaviour for large magnitudes.

Parameters

xTensor
Input tensor of any shape; activation is element-wise.

Returns

Tensor

Activated tensor with the same shape as x.

Notes

TanhShrink(x)=xtanh(x)\text{TanhShrink}(x) = x - \tanh(x)

For x1|x| \ll 1, tanh(x)xx3/3\tanh(x) \approx x - x^3/3, so TanhShrink(x)x3/3\text{TanhShrink}(x) \approx x^3/3 — small signals are cubically suppressed. For x1|x| \gg 1, tanh\tanh saturates to ±1\pm 1 so the output behaves like x1x \mp 1. CC^\infty-smooth and monotonic.

Examples

>>> import lucid
>>> from lucid.nn.functional import tanhshrink
>>> x = lucid.tensor([-2.0, -0.5, 0.0, 0.5, 2.0])
>>> tanhshrink(x)
Tensor([-1.0360, -0.0379,  0.0000,  0.0379,  1.0360])