mindspore.Tensor.hardshrink

Tensor.hardshrink(lambd=0.5)[source]

Apply the Hard Shrink function for tensor. Calculates the output according to the input elements.

The formula is defined as follows:

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

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

Returns

Tensor, has the same shape and data type as self.

Raises
  • TypeError – If lambd is not a float.

  • TypeError – If dtype of the input tensor is neither float16 nor float32.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[0.5,  1,  2.0], [0.0533, 0.0776, -2.1233]]), ms.float32)
>>> print(x.hardshrink())
[[ 0.      1.      2.    ]
[ 0.      0.     -2.1233]]