Skip to content
fn

tanh

→Tensor
tanh(input: Tensor)

Implementing kernel

C++ engine symbols that back this Python API.

Element-wise hyperbolic tangent.

Saturating activation mapping the real line into (−1,1)(-1, 1).

Parameters

inputTensor
Input tensor.

Returns

Tensor

Element-wise result with the same shape as input.

Notes

Mathematical definition:

outi=tanh⁡(xi)=exi−e−xiexi+e−xi\text{out}_i = \tanh(x_i) = \frac{e^{x_i} - e^{-x_i}}{e^{x_i} + e^{-x_i}}

Gradient: 1−tanh⁡2(x)1 - \tanh^{2}(x). Common activation in RNNs.

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> lucid.tanh(x)
Tensor([...])