Skip to content
fn

tanh

→Tensor
tanh(input: Tensor)

Implementing kernel

C++ engine symbols that back this Python API.

Hyperbolic tangent activation.

A rescaled, recentred sigmoid that maps the real line to (−1,1)(-1, 1). Because outputs are zero-centred, tanh is generally preferred over sigmoid for hidden layers in shallow networks and is the canonical squashing function in RNN cells.

Parameters

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

Returns

Tensor

Activated tensor with the same shape as input, values in (−1,1)(-1, 1).

Notes

tanh⁡(x)=ex−e−xex+e−x=2σ(2x)−1\tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} = 2\sigma(2x) - 1

Derivative 1−tanh⁡2(x)1 - \tanh^2(x), maximum value 11 at the origin. Saturates for ∣x∣≳3|x| \gtrsim 3; in deep networks this leads to vanishing gradients, so modern feed-forward stacks prefer ReLU-family activations.

Examples

>>> import lucid
>>> from lucid.nn.functional import tanh
>>> x = lucid.tensor([-2.0, 0.0, 2.0])
>>> tanh(x)
tensor([-0.964, 0., 0.964])

Used by 4