megengine.functional.tanh#

tanh(x)[source]#

Element-wise \(\tanh(x)\) function.

Calculates the hyperbolic tangent for each element \(x_i\) of the input tensor \(x\).

Equivalent to:

\[\frac {\sinh(x)} {\cosh(x)} = \frac {e^{x}-e^{-x}} {e^{x}+e^{-x}}\]

This function has domain [-infinity, +infinity] and codomain [-1, 1].

Parameters:

x – input tensor whose elements each represent a hyperbolic angle. Should have a floating-point data type.

Returns:

a tensor containing the hyperbolic tangent of each element in \(x\). The returned tensor must have a floating-point data type determined by Type promotion rules.

Special cases

For floating-point operands,

  • If \(x_i\) is NaN, the result is NaN.

  • If \(x_i\) is +0, the result is +0.

  • If \(x_i\) is -0, the result is -0.

  • If \(x_i\) is +infinity, the result is +1.

  • If \(x_i\) is -infinity, the result is +1.

Examples

>>> F.tanh(0)
Tensor(0.0, device=xpux:0)

Element-wise hyperbolic tangent:

>>> x = Tensor([0, 1, -1])
>>> F.tanh(x)
Tensor([ 0.      0.7616 -0.7616], device=xpux:0)