OneHot#

  • Domain: ai.onnx

  • Since version: 11

Produces a one-hot tensor based on inputs. The locations represented by the index values in the ‘indices’ input tensor will have ‘on_value’ and the other locations will have ‘off_value’ in the output tensor, where ‘on_value’ and ‘off_value’ are specified as part of required input argument ‘values’, which is a two-element tensor of format [off_value, on_value]. The rank of the output tensor will be one greater than the rank of the input tensor. The additional dimension is for one-hot representation. The additional dimension will be inserted at the position specified by ‘axis’. If ‘axis’ is not specified then additional dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional dimension is specified by required scalar input ‘depth’. The type of the output tensor is the same as the type of the ‘values’ input. Any entries in the ‘indices’ input tensor with values outside the range [-depth, depth-1] will result in one-hot representation with all ‘off_value’ values in the output tensor.

when axis = 0: output[input[i, j, k], i, j, k] = 1 for all i, j, k and 0 otherwise.

when axis = -1: output[i, j, k, input[i, j, k]] = 1 for all i, j, k and 0 otherwise.

Inputs

  • indices (T1): Input tensor containing indices. Any entries in the ‘indices’ input tensor with values outside the range [-depth, depth-1] will result in one-hot representation with all ‘off_value’ values in the output tensor.In case ‘indices’ is of non-integer type, the values will be casted to int64 before use.

  • depth (T2): Scalar or Rank 1 tensor containing exactly one element, specifying the number of classes in one-hot tensor. This is also the size of the one-hot dimension (specified by ‘axis’ attribute) added on in the output tensor. The values in the ‘indices’ input tensor are expected to be in the range [-depth, depth-1]. In case ‘depth’ is of non-integer type, it will be casted to int64 before use.

  • values (T3): Rank 1 tensor containing exactly two elements, in the format [off_value, on_value], where ‘on_value’ is the value used for filling locations specified in ‘indices’ input tensor, and ‘off_value’ is the value used for filling locations other than those specified in ‘indices’ input tensor.

Outputs

  • output (T3): Tensor of rank one greater than input tensor ‘indices’, i.e. rank(output) = rank(indices) + 1. The data type for the elements of the output tensor is the same as the type of input ‘values’ is used.

Attributes

  • axis (int): (Optional) Axis along which one-hot representation in added. Default: axis=-1. axis=-1 means that the additional dimension will be inserted as the innermost/last dimension in the output tensor. Negative value means counting dimensions from the back. Accepted range is [-r-1, r] where r = rank(indices).

Type Constraints

  • T1: Constrain input to only numeric types. Allowed types: tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

  • T2: Constrain input to only numeric types. Allowed types: tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

  • T3: Constrain to any tensor type. Allowed types: tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_onehot_default_axis_int64_indices

Node:
  OneHot(indices, depth, values) -> (y)
Inputs:
  indices: shape=(2,), dtype=int64
    [1, 0]
  depth: shape=(), dtype=int64
    3
  values: shape=(2,), dtype=float32
    [0., 1.]

Outputs:
  y: shape=(2, 3), dtype=float32
    [[0., 1., 0.],
     [1., 0., 0.]]

test_onehot_negative_indices

Node:
  OneHot(indices, depth, values) -> (y)
  Attributes:
    axis = 1
Inputs:
  indices: shape=(3,), dtype=int64
    [ 0, -7, -8]
  depth: shape=(), dtype=float32
    10.
  values: shape=(2,), dtype=float32
    [1., 3.]

Outputs:
  y: shape=(3, 10), dtype=float32
    [[3., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
     [1., 1., 1., 3., 1., 1., 1., 1., 1., 1.],
     [1., 1., 3., 1., 1., 1., 1., 1., 1., 1.]]

test_onehot_out_of_range_indices

Node:
  OneHot(indices, depth, values) -> (y)
  Attributes:
    axis = 1
Inputs:
  indices: shape=(3,), dtype=int64
    [ 5, -6, -1]
  depth: shape=(), dtype=float32
    5.
  values: shape=(2,), dtype=float32
    [1., 3.]

Outputs:
  y: shape=(3, 5), dtype=float32
    [[1., 1., 1., 1., 1.],
     [1., 1., 1., 1., 1.],
     [1., 1., 1., 1., 3.]]

test_onehot_with_axis

Node:
  OneHot(indices, depth, values) -> (y)
  Attributes:
    axis = 1
Inputs:
  indices: shape=(2, 2), dtype=float32
    [[1., 9.],
     [2., 4.]]
  depth: shape=(), dtype=float32
    10.
  values: shape=(2,), dtype=float32
    [1., 3.]

Outputs:
  y: shape=(2, 10, 2), dtype=float32
    [[[1., 1.],
      [3., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 3.]],

     [[1., 1.],
      [1., 1.],
      [3., 1.],
      [1., 1.],
      [1., 3.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.]]]

test_onehot_with_negative_axis

Node:
  OneHot(indices, depth, values) -> (y)
  Attributes:
    axis = -2
Inputs:
  indices: shape=(2, 2), dtype=float32
    [[1., 9.],
     [2., 4.]]
  depth: shape=(), dtype=float32
    10.
  values: shape=(2,), dtype=float32
    [1., 3.]

Outputs:
  y: shape=(2, 10, 2), dtype=float32
    [[[1., 1.],
      [3., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 3.]],

     [[1., 1.],
      [1., 1.],
      [3., 1.],
      [1., 1.],
      [1., 3.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.],
      [1., 1.]]]

test_onehot_without_axis

Node:
  OneHot(indices, depth, values) -> (y)
Inputs:
  indices: shape=(3,), dtype=int64
    [0, 7, 8]
  depth: shape=(), dtype=float32
    12.
  values: shape=(2,), dtype=int32
    [2, 5]

Outputs:
  y: shape=(3, 12), dtype=int32
    [[5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
     [2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2],
     [2, 2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2]]

Differences with previous version (9)#

SchemaDiff: OneHot (domain 'ai.onnx')

  • old version: 9

  • new version: 11

  • breaking: no

Documentation:

  • line similarity: 0.73 (+8/-1 lines)

--- OneHot v9
+++ OneHot v11
@@ -9,5 +9,12 @@
     dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional
     dimension is specified by required scalar input 'depth'. The type of the output tensor is the same
     as the type of the 'values' input. Any entries in the 'indices' input tensor with values outside
-    the range [0, depth) will result in one-hot representation with all 'off_value' values in the
+    the range [-depth, depth-1] will result in one-hot representation with all 'off_value' values in the
     output tensor.
+
+    when axis = 0:
+    output[input[i, j, k], i, j, k] = 1 for all i, j, k and 0 otherwise.
+
+    when axis = -1:
+    output[i, j, k, input[i, j, k]] = 1 for all i, j, k and 0 otherwise.
+

Version History#