class
Tanhshrink
extends
ModuleTanhshrink()Tanhshrink activation function.
Applies element-wise:
Subtracts the tanh of the input from the input itself. Near zero the output is approximately (the tanh Taylor residual), so the function is smooth and cubic near the origin. For large inputs, , so the output approaches , behaving asymptotically like the identity.
Notes
- Input: — any shape.
- Output: — same shape as input.
Tanhshrink is used as a soft thresholding operator in some sparse representation and compressed-sensing networks.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Tanhshrink()
>>> x = lucid.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
>>> m(x)
tensor([-1.0360, -0.2384, 0. , 0.2384, 1.0360])
>>> # Smooth sparse regularisation in an encoder
>>> x = lucid.randn(4, 256)
>>> out = m(x)
>>> out.shape
(4, 256)Methods (1)
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.