mindspore.nn.Hardtanh

查看源文件
class mindspore.nn.Hardtanh(min_val=- 1.0, max_val=1.0)[源代码]

按元素计算Hardtanh函数。Hardtanh函数定义为:

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

线性区域范围 \([-1, 1]\) 可以使用 min_valmax_val 进行调整。

Hardtanh函数图:

../../_images/Hardtanh.png

说明

在Ascend硬件上,float16数据类型场景下会有偶现的精度误差较大的问题。

参数:
  • min_val (Union[int, float]) - 线性区域范围的最小值。默认值: -1.0

  • max_val (Union[int, float]) - 线性区域范围的最大值。默认值: 1.0

输入:
  • x (Tensor) - 数据类型为float16或float32的Tensor。在CPU和Ascend平台上支持零到七维。在GPU平台上支持零到四维。

输出:

Tensor,数据类型和shape与 x 的相同。

异常:
  • TypeError - x 不是Tensor。

  • TypeError - x 的数据类型既不是float16也不是float32。

  • TypeError - min_val 的数据类型既不是int也不是float。

  • TypeError - max_val 的数据类型既不是int也不是float。

  • ValueError - min_val 不小于 max_val

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16)
>>> hardtanh = nn.Hardtanh(min_val=-1.0, max_val=1.0)
>>> output = hardtanh(x)
>>> print(output)
[-1. -1.  0.  1.  1.]