Skip to content
class

Tanh

extendsModule
Tanh()

Implementing kernel

C++ engine symbols that back this Python API.

Hyperbolic tangent activation function.

Applies element-wise:

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

Maps all real inputs to the open interval (−1,1)(-1, 1). Zero-centred, unlike sigmoid, which makes it the preferred gate/output activation in many recurrent architectures (LSTM, GRU).

Notes

  • Input: (∗)(*) — any shape.
  • Output: (∗)(*) — same shape as input, values in (−1,1)(-1, 1).

Like sigmoid, tanh saturates for large inputs. Its zero-centred output reduces the bias shift problem in successive layers, but the vanishing gradient issue still applies for very deep networks.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Tanh()
>>> x = lucid.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
>>> m(x)
tensor([-0.964, -0.7616, 0., 0.7616, 0.964])
>>> # Hidden state output in a simple recurrent cell
>>> h = lucid.randn(32, 128)
>>> h_next = m(h)
>>> h_next.shape
(32, 128)

Used by 1

Instance methods

1
fn

forward

→Tensor
forward(input: Tensor)

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.