oneflow.nn.Hardtanh

class oneflow.nn.Hardtanh(min_val: float = - 1, max_val: float = 1, inplace: bool = False, min_value: Optional[float] = None, max_value: Optional[float] = None)

Applies the HardTanh function element-wise

HardTanh is defined as:

\[\begin{split}\text{HardTanh}(x) = \begin{cases} 1 & \text{ if } x > 1 \\ -1 & \text{ if } x < -1 \\ x & \text{ otherwise } \\ \end{cases}\end{split}\]

The range of the linear region \([-1, 1]\) can be adjusted using min_val and max_val.

Parameters
  • min_val – minimum value of the linear region range. Default: -1

  • max_val – maximum value of the linear region range. Default: 1

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

Keyword arguments min_value and max_value have been deprecated in favor of min_val and max_val.

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

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

For example:

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

>>> m = flow.nn.Hardtanh()
>>> arr = np.array([0.2, 0.3, 3.0, 4.0])
>>> x = flow.Tensor(arr)
>>> out = m(x)
>>> out
tensor([0.2000, 0.3000, 1.0000, 1.0000], dtype=oneflow.float32)