mindspore.ops.softshrink

查看源文件
mindspore.ops.softshrink(x, lambd=0.5)[源代码]

逐元素计算Soft Shrink激活函数。

\[\begin{split}\text{SoftShrink}(x) = \begin{cases} x - \lambda, & \text{ if } x > \lambda \\ x + \lambda, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases}\end{split}\]
参数:
  • x (Tensor) - Soft Shrink的输入,数据类型为float16或float32。

  • lambd (float) - \(\lambda\) ,应大于等于0。默认值: 0.5

返回:

Tensor,shape和数据类型与输入相同。

异常:
  • TypeError - lambd 不是float。

  • TypeError - x 不是Tensor。

  • TypeError - x 的dtype既不是float16也不是float32。

  • ValueError - lambd 小于0。

支持平台:

Ascend GPU CPU

样例:

>>> from mindspore import Tensor
>>> from mindspore import ops
>>> import numpy as np
>>> x = Tensor(np.array([[ 0.5297,  0.7871,  1.1754], [ 0.7836,  0.6218, -1.1542]]), mindspore.float32)
>>> output = ops.softshrink(x)
>>> print(output)
[[ 0.02979  0.287    0.676  ]
 [ 0.2837   0.1216  -0.6543 ]]