nvidia.dali.fn.one_hot

nvidia.dali.fn.one_hot(__input, /, *, axis=-1, axis_name=None, bytes_per_sample_hint=[0], dtype=DALIDataType.FLOAT, num_classes=0, off_value=0.0, on_value=1.0, preserve=False, seed=-1, device=None, name=None)

Produces a one-hot encoding of the input.

Adds a new axis or converts scalar input into an axis of num_classes elements.

For given input coordinate (x0, x1, ..., xn), and axis = k, the output sample is specified as:

cls = input[x0, x1, ..., xn]
output[x0, x1, ..., xk-1, i, xk, ..., xn] = on_value if i == cls else off_value

for all i in range [0, num_classes).

For scalars, the output is set to on_value at the index taken from input and off_value elsewhere:

output[i] = on_value if i == input else off_value

For backward compatibility, any input in which all tensors have only one element (regardless of the number of dimensions) is considered scalar. Legacy interpretation of tensors as scalars is not supported if axis argument is specified.

Supported backends
  • ‘cpu’

  • ‘gpu’

Parameters:

__input (TensorList) – Input to the operator.

Keyword Arguments:
  • axis (int, optional, default = -1) – Dimension to place the one-hot encoding axis of num_classes size. By default it’s appended as the last dimension for non-scalar inputs. For scalar inputs, it becomes the only dimension.

  • axis_name (str, optional) – Single character that will be used as a name for the newly added dimension in the output layout. If no character is provided, the output layout will be empty.

  • bytes_per_sample_hint (int or list of int, optional, default = [0]) –

    Output size hint, in bytes per sample.

    If specified, the operator’s outputs residing in GPU or page-locked host memory will be preallocated to accommodate a batch of samples of this size.

  • dtype (nvidia.dali.types.DALIDataType, optional, default = DALIDataType.FLOAT) – Output data type.

  • num_classes (int, optional, default = 0) – Number of all classes in the data.

  • off_value (float, optional, default = 0.0) –

    Value that will be used to fill the output to indicate the lack of given class in the corresponding input coordinate.

    This value will be cast to the dtype type.

  • on_value (float, optional, default = 1.0) –

    Value that will be used to fill the output to indicate given class in the corresponding input coordinate.

    This value will be cast to the dtype type.

  • preserve (bool, optional, default = False) – Prevents the operator from being removed from the graph even if its outputs are not used.

  • seed (int, optional, default = -1) –

    Random seed.

    If not provided, it will be populated based on the global seed of the pipeline.