mindspore.nn.HSwish

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

Applies Hard Swish activation function element-wise.

Hard swish is defined as:

\[\begin{split}\text{Hardswish}(input) = \begin{cases} 0, & \text{ if } input \leq -3, \\ input, & \text{ if } input \geq +3, \\ input*(input + 3)/6, & \text{ otherwise } \end{cases}\end{split}\]

HSwish Activation Function Graph:

../../_images/HSwish.png
Inputs:
  • input (Tensor) - The input of HSwish.

Outputs:

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

Raises
  • TypeError – If input is not a tensor.

  • TypeError – If input is neither int nor float.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> input = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> hswish = nn.HSwish()
>>> result = hswish(input)
>>> print(result)
[-0.3333 -0.3333  0.      1.667   0.6665]