mindspore.nn.Tanh

class mindspore.nn.Tanh[source]

Tanh activation function.

Applies the Tanh function element-wise, returns a new tensor with the hyperbolic tangent of the elements of input, The input is a Tensor with any valid shape.

Tanh function is defined as:

\[tanh(x_i) = \frac{\exp(x_i) - \exp(-x_i)}{\exp(x_i) + \exp(-x_i)} = \frac{\exp(2x_i) - 1}{\exp(2x_i) + 1},\]

where \(x_i\) is an element of the input Tensor.

Inputs:
  • input_data (Tensor) - The input of Tanh with data type of float16 or float32.

Outputs:

Tensor, with the same type and shape as the input_data.

Raises

TypeError – If dtype of input_data is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

Examples

>>> input_x = Tensor(np.array([1, 2, 3, 2, 1]), mindspore.float16)
>>> tanh = nn.Tanh()
>>> output = tanh(input_x)
>>> print(output)
[0.7617 0.964  0.995  0.964  0.7617]