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
0 | 0 | Produces a one-hot tensor based on inputs. | Produces a one-hot tensor based on inputs. |
1 | 1 | The 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' |
2 | 2 | and 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' |
3 | 3 | are specified as part of required input argument 'values', which is a two-element tensor of format | are specified as part of required input argument 'values', which is a two-element tensor of format |
4 | 4 | [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 |
5 | 5 | input tensor. The additional dimension is for one-hot representation. The additional dimension will | input tensor. The additional dimension is for one-hot representation. The additional dimension will |
6 | 6 | be inserted at the position specified by 'axis'. If 'axis' is not specified then then additional | be inserted at the position specified by 'axis'. If 'axis' is not specified then then additional |
7 | 7 | dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional | dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional |
8 | 8 | dimension is specified by required scalar input 'depth'. The type of the output tensor is the same | dimension is specified by required scalar input 'depth'. The type of the output tensor is the same |
9 | 9 | as the type of the 'values' input. Any entries in the 'indices' input tensor with values outside | as the type of the 'values' input. Any entries in the 'indices' input tensor with values outside |
10 | 10 | the range [0, depth) will result in one-hot representation with all 'off_value' values in the |
|
11 | 11 | output tensor. | output tensor. |
12 | 12 |
|
|
13 | when axis = 0: | ||
14 | output[input[i, j, k], i, j, k] = 1 for all i, j, k and 0 otherwise. | ||
15 |
| ||
16 | when axis = -1: | ||
17 | output[i, j, k, input[i, j, k]] = 1 for all i, j, k and 0 otherwise. | ||
18 |
| ||
13 | 19 | **Attributes** | **Attributes** |
14 | 20 |
|
|
15 | 21 | * **axis**: | * **axis**: |
16 | 22 | (Optional) Axis along which one-hot representation in added. | (Optional) Axis along which one-hot representation in added. |
17 | 23 | Default: axis=-1. axis=-1 means that the additional dimension will | Default: axis=-1. axis=-1 means that the additional dimension will |
18 | 24 | be inserted as the innermost/last dimension in the output tensor. Default value is -1. |
|
25 | Negative value means counting dimensions from the back. Accepted | ||
26 | range is [-r-1, r] where r = rank(indices). Default value is -1. | ||
19 | 27 |
|
|
20 | 28 | **Inputs** | **Inputs** |
21 | 29 |
|
|
22 | 30 | * **indices** (heterogeneous) - **T1**: | * **indices** (heterogeneous) - **T1**: |
23 | 31 | Input tensor containing indices. The values must be non-negative |
|
24 | integers. Any entries in the 'indices' input tensor with values | ||
25 | 32 | outside the range [0, depth) will result in one-hot representation |
|
26 | 33 | with all 'off_value' values in the output tensor.In case 'indices' |
|
34 | tensor.In case 'indices' is of non-integer type, the values will be | ||
27 | 35 | is of non-integer type, the values will be casted to int64 before |
|
28 | use. | ||
29 | 36 | * **depth** (heterogeneous) - **T2**: | * **depth** (heterogeneous) - **T2**: |
30 | 37 | Scalar specifying the number of classes in one-hot tensor. This is | Scalar specifying the number of classes in one-hot tensor. This is |
31 | 38 | also the size of the one-hot dimension (specified by 'axis' | also the size of the one-hot dimension (specified by 'axis' |
32 | 39 | attribute) added on in the output tensor. The values in the | attribute) added on in the output tensor. The values in the |
33 | 40 | 'indices' input tensor are expected to be in the range [0, depth). |
|
34 | 41 | In case 'depth' is of non-integer type, it will be casted to int64 |
|
35 | 42 | before use. |
|
36 | 43 | * **values** (heterogeneous) - **T3**: | * **values** (heterogeneous) - **T3**: |
37 | 44 | Rank 1 tensor containing exactly two elements, in the format | Rank 1 tensor containing exactly two elements, in the format |
38 | 45 | [off_value, on_value], where 'on_value' is the value used for | [off_value, on_value], where 'on_value' is the value used for |
39 | 46 | filling locations specified in 'indices' input tensor, and | filling locations specified in 'indices' input tensor, and |
40 | 47 | 'off_value' is the value used for filling locations other than those | 'off_value' is the value used for filling locations other than those |
41 | 48 | specified in 'indices' input tensor. | specified in 'indices' input tensor. |
42 | 49 |
|
|
43 | 50 | **Outputs** | **Outputs** |
44 | 51 |
|
|
45 | 52 | * **output** (heterogeneous) - **T3**: | * **output** (heterogeneous) - **T3**: |
46 | 53 | Tensor of rank one greater than input tensor 'indices', i.e. | Tensor of rank one greater than input tensor 'indices', i.e. |
47 | 54 | rank(output) = rank(indices) + 1. The data type for the elements of | rank(output) = rank(indices) + 1. The data type for the elements of |
48 | 55 | 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. |
49 | 56 |
|
|
50 | 57 | **Type Constraints** | **Type Constraints** |
51 | 58 |
|
|
52 | 59 | * **T1** in ( | * **T1** in ( |
53 | 60 | tensor(double), | tensor(double), |
54 | 61 | tensor(float), | tensor(float), |
55 | 62 | tensor(float16), | tensor(float16), |
56 | 63 | tensor(int16), | tensor(int16), |
57 | 64 | tensor(int32), | tensor(int32), |
58 | 65 | tensor(int64), | tensor(int64), |
59 | 66 | tensor(int8), | tensor(int8), |
60 | 67 | tensor(uint16), | tensor(uint16), |
61 | 68 | tensor(uint32), | tensor(uint32), |
62 | 69 | tensor(uint64), | tensor(uint64), |
63 | 70 | tensor(uint8) | tensor(uint8) |
64 | 71 | ): | ): |
65 | 72 | Constrains input to only numeric types. | Constrains input to only numeric types. |
66 | 73 | * **T2** in ( | * **T2** in ( |
67 | 74 | tensor(double), | tensor(double), |
68 | 75 | tensor(float), | tensor(float), |
69 | 76 | tensor(float16), | tensor(float16), |
70 | 77 | tensor(int16), | tensor(int16), |
71 | 78 | tensor(int32), | tensor(int32), |
72 | 79 | tensor(int64), | tensor(int64), |
73 | 80 | tensor(int8), | tensor(int8), |
74 | 81 | tensor(uint16), | tensor(uint16), |
75 | 82 | tensor(uint32), | tensor(uint32), |
76 | 83 | tensor(uint64), | tensor(uint64), |
77 | 84 | tensor(uint8) | tensor(uint8) |
78 | 85 | ): | ): |
79 | 86 | Constrains input to only numeric types. | Constrains input to only numeric types. |
80 | 87 | * **T3** in ( | * **T3** in ( |
81 | 88 | tensor(bool), | tensor(bool), |
82 | 89 | tensor(complex128), | tensor(complex128), |
83 | 90 | tensor(complex64), | tensor(complex64), |
84 | 91 | tensor(double), | tensor(double), |
85 | 92 | tensor(float), | tensor(float), |
86 | 93 | tensor(float16), | tensor(float16), |
87 | 94 | tensor(int16), | tensor(int16), |
88 | 95 | tensor(int32), | tensor(int32), |
89 | 96 | tensor(int64), | tensor(int64), |
90 | 97 | tensor(int8), | tensor(int8), |
91 | 98 | tensor(string), | tensor(string), |
92 | 99 | tensor(uint16), | tensor(uint16), |
93 | 100 | tensor(uint32), | tensor(uint32), |
94 | 101 | tensor(uint64), | tensor(uint64), |
95 | 102 | tensor(uint8) | tensor(uint8) |
96 | 103 | ): | ): |
97 | 104 | 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.