OneHot#

OneHot - 11#

Version

  • name: OneHot (GitHub)

  • domain: main

  • since_version: 11

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 11.

Summary

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 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.

Attributes

  • axis: (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). Default value is -1.

Inputs

  • indices (heterogeneous) - 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 (heterogeneous) - T2: Scalar 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 (heterogeneous) - 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 (heterogeneous) - 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.

Type Constraints

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

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

  • T3 in ( 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) ): Constrain to any tensor type.

Examples

without_axis

on_value = 5
off_value = 2
output_type = np.int32
node = onnx.helper.make_node(
    'OneHot',
    inputs=['indices', 'depth', 'values'],
    outputs=['y']
)
indices = np.array([0, 7, 8], dtype=np.int64)
depth = np.float32(12)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_without_axis')

with_axis

axisValue = 1
on_value = 3
off_value = 1
output_type = np.float32
node = onnx.helper.make_node(
    'OneHot',
    inputs=['indices', 'depth', 'values'],
    outputs=['y'],
    axis=axisValue
)
indices = np.array([[1, 9],
                    [2, 4]], dtype=np.float32)
depth = np.float32(10)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_axis')

with_negative_indices

axisValue = 1
on_value = 3
off_value = 1
output_type = np.float32
node = onnx.helper.make_node(
    'OneHot',
    inputs=['indices', 'depth', 'values'],
    outputs=['y'],
    axis=axisValue
)
indices = np.array([0, -7, -8], dtype=np.int64)

# print(y)
# [[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.]]

depth = np.float32(10)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_negative_indices')

with_negative_axis

axisValue = -2
on_value = 3
off_value = 1
output_type = np.float32
node = onnx.helper.make_node(
    'OneHot',
    inputs=['indices', 'depth', 'values'],
    outputs=['y'],
    axis=axisValue
)
indices = np.array([[1, 9],
                    [2, 4]], dtype=np.float32)
depth = np.float32(10)
values = np.array([off_value, on_value], dtype=output_type)
y = one_hot(indices, depth, axis=axisValue, dtype=output_type)
y = y * (on_value - off_value) + off_value
expect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_negative_axis')

Differences

00Produces a one-hot tensor based on inputs.Produces a one-hot tensor based on inputs.
11The locations represented by the index values in the 'indices' input tensor will have 'on_value'The locations represented by the index values in the 'indices' input tensor will have 'on_value'
22and the other locations will have 'off_value' in the output tensor, where 'on_value' and 'off_value'and the other locations will have 'off_value' in the output tensor, where 'on_value' and 'off_value'
33are specified as part of required input argument 'values', which is a two-element tensor of formatare specified as part of required input argument 'values', which is a two-element tensor of format
44[off_value, on_value]. The rank of the output tensor will be one greater than the rank of the[off_value, on_value]. The rank of the output tensor will be one greater than the rank of the
55input tensor. The additional dimension is for one-hot representation. The additional dimension willinput tensor. The additional dimension is for one-hot representation. The additional dimension will
66be inserted at the position specified by 'axis'. If 'axis' is not specified then then additionalbe inserted at the position specified by 'axis'. If 'axis' is not specified then then additional
77dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additionaldimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional
88dimension is specified by required scalar input 'depth'. The type of the output tensor is the samedimension is specified by required scalar input 'depth'. The type of the output tensor is the same
99as the type of the 'values' input. Any entries in the 'indices' input tensor with values outsideas the type of the 'values' input. Any entries in the 'indices' input tensor with values outside
1010the range [0, depth) will result in one-hot representation with all 'off_value' values in thethe range [-depth, depth-1] will result in one-hot representation with all 'off_value' values in the
1111output tensor.output tensor.
1212
13when axis = 0:
14output[input[i, j, k], i, j, k] = 1 for all i, j, k and 0 otherwise.
15
16when axis = -1:
17output[i, j, k, input[i, j, k]] = 1 for all i, j, k and 0 otherwise.
18
1319**Attributes****Attributes**
1420
1521* **axis**:* **axis**:
1622 (Optional) Axis along which one-hot representation in added. (Optional) Axis along which one-hot representation in added.
1723 Default: axis=-1. axis=-1 means that the additional dimension will Default: axis=-1. axis=-1 means that the additional dimension will
1824 be inserted as the innermost/last dimension in the output tensor. Default value is -1. be inserted as the innermost/last dimension in the output tensor.
25 Negative value means counting dimensions from the back. Accepted
26 range is [-r-1, r] where r = rank(indices). Default value is -1.
1927
2028**Inputs****Inputs**
2129
2230* **indices** (heterogeneous) - **T1**:* **indices** (heterogeneous) - **T1**:
2331 Input tensor containing indices. The values must be non-negative Input tensor containing indices. Any entries in the 'indices' input
24 integers. Any entries in the 'indices' input tensor with values
2532 outside the range [0, depth) will result in one-hot representation tensor with values outside the range [-depth, depth-1] will result
2633 with all 'off_value' values in the output tensor.In case 'indices' in one-hot representation with all 'off_value' values in the output
34 tensor.In case 'indices' is of non-integer type, the values will be
2735 is of non-integer type, the values will be casted to int64 before casted to int64 before use.
28 use.
2936* **depth** (heterogeneous) - **T2**:* **depth** (heterogeneous) - **T2**:
3037 Scalar specifying the number of classes in one-hot tensor. This is Scalar specifying the number of classes in one-hot tensor. This is
3138 also the size of the one-hot dimension (specified by 'axis' also the size of the one-hot dimension (specified by 'axis'
3239 attribute) added on in the output tensor. The values in the attribute) added on in the output tensor. The values in the
3340 'indices' input tensor are expected to be in the range [0, depth). 'indices' input tensor are expected to be in the range [-depth,
3441 In case 'depth' is of non-integer type, it will be casted to int64 depth-1]. In case 'depth' is of non-integer type, it will be casted
3542 before use. to int64 before use.
3643* **values** (heterogeneous) - **T3**:* **values** (heterogeneous) - **T3**:
3744 Rank 1 tensor containing exactly two elements, in the format Rank 1 tensor containing exactly two elements, in the format
3845 [off_value, on_value], where 'on_value' is the value used for [off_value, on_value], where 'on_value' is the value used for
3946 filling locations specified in 'indices' input tensor, and filling locations specified in 'indices' input tensor, and
4047 'off_value' is the value used for filling locations other than those 'off_value' is the value used for filling locations other than those
4148 specified in 'indices' input tensor. specified in 'indices' input tensor.
4249
4350**Outputs****Outputs**
4451
4552* **output** (heterogeneous) - **T3**:* **output** (heterogeneous) - **T3**:
4653 Tensor of rank one greater than input tensor 'indices', i.e. Tensor of rank one greater than input tensor 'indices', i.e.
4754 rank(output) = rank(indices) + 1. The data type for the elements of rank(output) = rank(indices) + 1. The data type for the elements of
4855 the output tensor is the same as the type of input 'values' is used. the output tensor is the same as the type of input 'values' is used.
4956
5057**Type Constraints****Type Constraints**
5158
5259* **T1** in (* **T1** in (
5360 tensor(double), tensor(double),
5461 tensor(float), tensor(float),
5562 tensor(float16), tensor(float16),
5663 tensor(int16), tensor(int16),
5764 tensor(int32), tensor(int32),
5865 tensor(int64), tensor(int64),
5966 tensor(int8), tensor(int8),
6067 tensor(uint16), tensor(uint16),
6168 tensor(uint32), tensor(uint32),
6269 tensor(uint64), tensor(uint64),
6370 tensor(uint8) tensor(uint8)
6471 ): ):
6572 Constrains input to only numeric types. Constrains input to only numeric types.
6673* **T2** in (* **T2** in (
6774 tensor(double), tensor(double),
6875 tensor(float), tensor(float),
6976 tensor(float16), tensor(float16),
7077 tensor(int16), tensor(int16),
7178 tensor(int32), tensor(int32),
7279 tensor(int64), tensor(int64),
7380 tensor(int8), tensor(int8),
7481 tensor(uint16), tensor(uint16),
7582 tensor(uint32), tensor(uint32),
7683 tensor(uint64), tensor(uint64),
7784 tensor(uint8) tensor(uint8)
7885 ): ):
7986 Constrains input to only numeric types. Constrains input to only numeric types.
8087* **T3** in (* **T3** in (
8188 tensor(bool), tensor(bool),
8289 tensor(complex128), tensor(complex128),
8390 tensor(complex64), tensor(complex64),
8491 tensor(double), tensor(double),
8592 tensor(float), tensor(float),
8693 tensor(float16), tensor(float16),
8794 tensor(int16), tensor(int16),
8895 tensor(int32), tensor(int32),
8996 tensor(int64), tensor(int64),
9097 tensor(int8), tensor(int8),
9198 tensor(string), tensor(string),
9299 tensor(uint16), tensor(uint16),
93100 tensor(uint32), tensor(uint32),
94101 tensor(uint64), tensor(uint64),
95102 tensor(uint8) tensor(uint8)
96103 ): ):
97104 Constrain to any tensor type. Constrain to any tensor type.

OneHot - 9#

Version

  • name: OneHot (GitHub)

  • domain: main

  • since_version: 9

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 9.

Summary

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 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 [0, depth) will result in one-hot representation with all ‘off_value’ values in the output tensor.

Attributes

  • axis: (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. Default value is -1.

Inputs

  • indices (heterogeneous) - T1: Input tensor containing indices. The values must be non-negative integers. 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 output tensor.In case ‘indices’ is of non-integer type, the values will be casted to int64 before use.

  • depth (heterogeneous) - T2: Scalar 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 [0, depth). In case ‘depth’ is of non-integer type, it will be casted to int64 before use.

  • values (heterogeneous) - 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 (heterogeneous) - 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.

Type Constraints

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

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

  • T3 in ( 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) ): Constrain to any tensor type.