<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#L798-L823) |

Applies the HardTanh function element-wise.

#### View aliases


**Main aliases**

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

<br />

    tf.keras.ops.hard_tanh(
        x
    )

#### It is defined as:

`f(x) = -1 for x < -1`, `f(x) = x for -1 <= x <= 1`, `f(x) = 1 for x > 1`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | Input tensor. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Output tensor of same shape as `x` where values are clamped between -1 and 1. ||

<br />

#### Example:

    x = np.array([-2., -1., 0., 1., 2.])
    x_hard_tanh = keras.ops.hard_tanh(x)
    print(x_hard_tanh)
    array([-1. -1.  0.  1.  1.], shape=(5,), dtype=float64)