<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/ops/nn.py#L838-L864) |

Hard Shrink activation function.

#### View aliases


**Main aliases**

[`tf.keras.ops.nn.hard_shrink`](https://www.tensorflow.org/api_docs/python/tf/keras/ops/hard_shrink)

<br />

    tf.keras.ops.hard_shrink(
        x, threshold=0.5
    )

The Hard Shrink function is a thresholding operation defined as:

`f(x) = x` if `|x| > threshold`,
`f(x) = 0` otherwise.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | Input tensor. |
| `threshold` | Threshold value. Defaults to 0.5. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A tensor with the same shape as `x`. ||

<br />

#### Example:

    x = np.array([-0.5, 0., 1.])
    x_hard_shrink = keras.ops.hard_shrink(x)
    print(x_hard_shrink)
    array([0. 0. 1.], shape=(3,), dtype=float64)