<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#L1067-L1094) |

Sparsemax activation function.

#### View aliases


**Main aliases**

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

<br />

    tf.keras.ops.sparsemax(
        x, axis=-1
    )

For each batch `i`, and class `j`,
sparsemax activation function is defined as:

`sparsemax(x)[i, j] = max(x[i, j] - τ(x[i, :]), 0).`

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | Input tensor. |
| `axis` | `int`, axis along which the sparsemax operation is applied. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A tensor, output of sparsemax transformation. Has the same type and shape as `x`. ||

<br />

#### Example:

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