tf.keras.ops.multi_hot

Encodes integer labels as multi-hot vectors.

This function encodes integer labels as multi-hot vectors, where each label is mapped to a binary value in the resulting vector.

inputsTensor of integer labels to be converted to multi-hot vectors.
num_classesInteger, the total number of unique classes.
axis(optional) Axis along which the multi-hot encoding should be added. Defaults to -1, which corresponds to the last dimension.
dtype(optional) The data type of the resulting tensor. Default is backend's float type.
sparseWhether to return a sparse tensor; for backends that support sparse tensors.

TensorThe multi-hot encoded tensor.

Example:

data = keras.ops.convert_to_tensor([0, 4])
keras.ops.multi_hot(data, num_classes=5)
array([1.0, 0.0, 0.0, 0.0, 1.0], dtype=float32)