tf.keras.ops.multi_hot  |  TensorFlow v2.16.1 (original) (raw)

tf.keras.ops.multi_hot

Encodes integer labels as multi-hot vectors.

View aliases

Main aliases

tf.keras.ops.nn.multi_hot

tf.keras.ops.multi_hot(
    inputs, num_classes=None, axis=-1, dtype=None, sparse=False, **kwargs
)

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

Args
inputs Tensor of integer labels to be converted to multi-hot vectors.
num_classes Integer, 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.
sparse Whether to return a sparse tensor; for backends that support sparse tensors.
Returns
Tensor The 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)

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2024-06-07 UTC.