mindspore.nn.HShrink

class mindspore.nn.HShrink(lambd=0.5)[source]

Applies Hard Shrink activation function element-wise.

The formula is defined as follows:

HardShrink(x)={x, if x>λx, if x<λ0, otherwise 

HShrink Activation Function Graph:

../../_images/HShrink.png
Parameters

lambd (float) – The threshold λ defined by the Hard Shrink formula. Default: 0.5 .

Inputs:
  • input_x (Tensor) - The input of Hard Shrink with data type of float16 or float32.

Outputs:

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

Raises
  • TypeError – If lambd is not a float.

  • TypeError – If dtype of input_x is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, nn
>>> import numpy as np
>>> input_x = Tensor(np.array([[ 0.5,  1,  2.0], [0.0533,0.0776,-2.1233]]), mindspore.float32)
>>> hshrink = nn.HShrink()
>>> output = hshrink(input_x)
>>> print(output)
[[ 0.      1.      2.    ]
[ 0.      0.     -2.1233]]