oneflow.nn.Hardswish

class oneflow.nn.Hardswish(inplace: bool = False)

Applies the hardswish function, element-wise, as described in the paper Searching for MobileNetV3.

\[\begin{split}\text{Hardswish}(x) = \begin{cases} 0 & \text{ if } x \le -3 \\ x & \text{ if } x \ge +3 \\ x*(x+3)/6 & \text{ otherwise } \\ \end{cases}\end{split}\]
Parameters

inplace – can optionally do the operation in-place. Default: False

Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Output: \((N, *)\), same shape as the input

>>> import numpy as np
>>> import oneflow as flow

>>> x = np.array([-0.5, 0, 0.5]).astype(np.float32)
>>> input = flow.Tensor(x)
>>> hardswish = flow.nn.Hardswish()

>>> out = hardswish(input)
>>> out
tensor([-0.2083,  0.0000,  0.2917], dtype=oneflow.float32)