mindspore.nn.Tanhshrink

View Source On Gitee
class mindspore.nn.Tanhshrink[source]

Applies Tanhshrink activation function element-wise and returns a new tensor.

Tanh function is defined as:

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

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

Inputs:
  • x (Tensor) - Tensor of any dimension.

Outputs:

Tensor, with the same shape as the x.

Raises

TypeError – If x is not a Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> x = Tensor(np.array([1, 2, 3, 2, 1]), ms.float16)
>>> tanhshrink = nn.Tanhshrink()
>>> output = tanhshrink(x)
>>> print(output)
[0.2383 1.036  2.004  1.036  0.2383]