<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#L1932-L1978) |

Converts integer tensor `x` into a one-hot tensor.

#### View aliases


**Main aliases**

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

<br />

    tf.keras.ops.one_hot(
        x, num_classes, axis=-1, dtype=None, sparse=False
    )

The one-hot encoding is a representation where each integer value is
converted into a binary vector with a length equal to `num_classes`,
and the index corresponding to the integer value is marked as 1, while
all other indices are marked as 0.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | Integer tensor to be encoded. The shape can be arbitrary, but the dtype should be integer. |
| `num_classes` | Number of classes for the one-hot encoding. |
| `axis` | Axis along which the encoding is performed. `-1` represents the last axis. Defaults to `-1`. |
| `dtype` | (Optional) Data type of the output tensor. If not provided, it defaults to the default data type of the backend. |
| `sparse` | Whether to return a sparse tensor; for backends that support sparse tensors. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Integer tensor: One-hot encoded tensor with the same shape as `x` except for the specified `axis` dimension, which will have a length of `num_classes`. The dtype of the output tensor is determined by `dtype` or the default data type of the backend. ||

<br />

#### Example:

    x = keras.ops.convert_to_tensor([1, 3, 2, 0])
    one_hot(x, num_classes=4)
    array([[0. 1. 0. 0.]
           [0. 0. 0. 1.]
           [0. 0. 1. 0.]
           [1. 0. 0. 0.]], shape=(4, 4), dtype=float32)