Skip to content
fn

tanhshrink

→Tensor
tanhshrink(input: Tensor)

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

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

Returns

Tensor

Activated tensor with the same shape as input.

Notes

TanhShrink(x)=x−tanh⁡(x)\text{TanhShrink}(x) = x - \tanh(x)

For ∣x∣≪1|x| \ll 1, tanh⁡(x)≈x−x3/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 ∣x∣≫1|x| \gg 1, tanh⁡\tanh saturates to ±1\pm 1 so the output behaves like x∓1x \mp 1. C∞C^\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.036, -0.03788, 0., 0.03788, 1.036])

Used by 1