Hardmax#
Hardmax  13#
Version
name: Hardmax (GitHub)
domain: main
since_version: 13
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 13.
Summary
The operator computes the hardmax values for the given input:
Hardmax(element in input, axis) = 1 if the element is the first maximum value along the specified axis, 0 otherwise
The “axis” attribute indicates the dimension along which Hardmax will be performed. The output tensor has the same shape and contains the Hardmax values of the corresponding input.
Attributes
 axis:
Describes the dimension Hardmax will be performed on. Negative
value means counting dimensions from the back. Accepted range is [r, r1] where r = rank(input). Default value is
1
.
Inputs
input (heterogeneous)  T: The input tensor of rank >= axis.
Outputs
output (heterogeneous)  T: The output values with the same shape as the input tensor.
Type Constraints
T in ( tensor(bfloat16), tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
Examples
default
node = onnx.helper.make_node(
"Hardmax",
inputs=["x"],
outputs=["y"],
)
x = np.array([[3, 0, 1, 2], [2, 5, 1, 0], [0, 1, 3, 2], [0, 1, 2, 3]]).astype(
np.float32
)
# expect result:
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]]
y = hardmax(x)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_example")
# For multiple occurrences of the maximal values, the first occurrence is selected for onehot output
x = np.array([[3, 3, 3, 1]]).astype(np.float32)
# expect result:
# [[1, 0, 0, 0]]
y = hardmax(x)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_one_hot")
_hardmax_axis
x = np.random.randn(3, 4, 5).astype(np.float32)
node = onnx.helper.make_node(
"Hardmax",
inputs=["x"],
outputs=["y"],
axis=0,
)
y = hardmax(x, axis=0)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_axis_0")
node = onnx.helper.make_node(
"Hardmax",
inputs=["x"],
outputs=["y"],
axis=1,
)
y = hardmax(x, axis=1)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_axis_1")
node = onnx.helper.make_node(
"Hardmax",
inputs=["x"],
outputs=["y"],
axis=2,
)
y = hardmax(x, axis=2)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_axis_2")
node = onnx.helper.make_node(
"Hardmax",
inputs=["x"],
outputs=["y"],
axis=1,
)
y = hardmax(x, axis=1)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_negative_axis")
# default axis is 1
node = onnx.helper.make_node(
"Hardmax",
inputs=["x"],
outputs=["y"],
)
expect(node, inputs=[x], outputs=[y], name="test_hardmax_default_axis")
Differences
0  The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch  
1  0  of the given input. 

2  1 


3  The input does not need to explicitly be a 2D vector; rather, it will be  
4  2  coerced into one. For an arbitrary ndimensional tensor 

5  input \in [a_0, a_1, ..., a_{k1}, a_k, ..., a_{n1}] and k is  
3 
 
6  4  the axis provided, then input will be coerced into a 2dimensional tensor with 

7  dimensions [a_0 * ... * a_{k1}, a_k * ... * a_{n1}]. For the default  
8  case where axis=1, this means the input tensor will be coerced into a 2D tensor  
9  of dimensions [a_0, a_1 * ... * a_{n1}], where a_0 is often the batch size.  
10  In this situation, we must have a_0 = N and a_1 * ... * a_{n1} = D.  
11  Each of these dimensions must be matched correctly, or else the operator  
12  5  will throw errors. The output tensor has the same shape 

13  6  and contains the hardmax values of the corresponding input. 

14  7 


15  8  **Attributes**  **Attributes** 
16  9 


17  10  * **axis**:  * **axis**: 
18  11  Describes the axis of the inputs when coerced to 2D; defaults to one 

19  because the 0th axis most likely describes the batch_size. Negative  
20  12  value means counting dimensions from the back. Accepted range is  value means counting dimensions from the back. Accepted range is 
21  13  [r, r1] where r = rank(input). Default value is 1. 

22  14 


23  15  **Inputs**  **Inputs** 
24  16 


25  17  * **input** (heterogeneous)  **T**:  * **input** (heterogeneous)  **T**: 
26  18  The input tensor that's coerced into a 2D matrix of size (NxD) as 

27  described above.  
28  19 


29  20  **Outputs**  **Outputs** 
30  21 


31  22  * **output** (heterogeneous)  **T**:  * **output** (heterogeneous)  **T**: 
32  23  The output values with the same shape as input tensor (the original 

33  size without coercion).  
34  24 


35  25  **Type Constraints**  **Type Constraints** 
36  26 


37  27  * **T** in (  * **T** in ( 
28  tensor(bfloat16),  
38  29  tensor(double),  tensor(double), 
39  30  tensor(float),  tensor(float), 
40  31  tensor(float16)  tensor(float16) 
41  32  ):  ): 
42  33  Constrain input and output types to float tensors.  Constrain input and output types to float tensors. 
Hardmax  11#
Version
name: Hardmax (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
 The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch
of the given input.
The input does not need to explicitly be a 2D vector; rather, it will be coerced into one. For an arbitrary ndimensional tensor input in [a_0, a_1, …, a_{k1}, a_k, …, a_{n1}] and k is the axis provided, then input will be coerced into a 2dimensional tensor with dimensions [a_0 * … * a_{k1}, a_k * … * a_{n1}]. For the default case where axis=1, this means the input tensor will be coerced into a 2D tensor of dimensions [a_0, a_1 * … * a_{n1}], where a_0 is often the batch size. In this situation, we must have a_0 = N and a_1 * … * a_{n1} = D. Each of these dimensions must be matched correctly, or else the operator will throw errors. The output tensor has the same shape and contains the hardmax values of the corresponding input.
Attributes
axis: Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size. Negative value means counting dimensions from the back. Accepted range is [r, r1] where r = rank(input). Default value is
1
.
Inputs
input (heterogeneous)  T: The input tensor that’s coerced into a 2D matrix of size (NxD) as described above.
Outputs
output (heterogeneous)  T: The output values with the same shape as input tensor (the original size without coercion).
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
Differences
0  0  The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch  The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch 
1  1  of the given input. The input is a 2D tensor (Tensor<float>) of size 

2  (batch_size x input_feature_dimensions). The output tensor has the same shape  
3  and contains the hardmax values of the corresponding input.  
4  2 


5  3  Input does not need to explicitly be a 2D vector; rather, it will be 

6  4  coerced into one. For an arbitrary ndimensional tensor  coerced into one. For an arbitrary ndimensional tensor 
7  5  input \in [a_0, a_1, ..., a_{k1}, a_k, ..., a_{n1}] and k is  input \in [a_0, a_1, ..., a_{k1}, a_k, ..., a_{n1}] and k is 
8  6  the axis provided, then input will be coerced into a 2dimensional tensor with  the axis provided, then input will be coerced into a 2dimensional tensor with 
9  7  dimensions [a_0 * ... * a_{k1}, a_k * ... * a_{n1}]. For the default  dimensions [a_0 * ... * a_{k1}, a_k * ... * a_{n1}]. For the default 
10  8  case where axis=1, this means the input tensor will be coerced into a 2D tensor  case where axis=1, this means the input tensor will be coerced into a 2D tensor 
11  9  of dimensions [a_0, a_1 * ... * a_{n1}], where a_0 is often the batch size.  of dimensions [a_0, a_1 * ... * a_{n1}], where a_0 is often the batch size. 
12  10  In this situation, we must have a_0 = N and a_1 * ... * a_{n1} = D.  In this situation, we must have a_0 = N and a_1 * ... * a_{n1} = D. 
13  11  Each of these dimensions must be matched correctly, or else the operator  Each of these dimensions must be matched correctly, or else the operator 
14  12  will throw errors. 

13  and contains the hardmax values of the corresponding input.  
15  14 


16  15  **Attributes**  **Attributes** 
17  16 


18  17  * **axis**:  * **axis**: 
19  18  Describes the axis of the inputs when coerced to 2D; defaults to one  Describes the axis of the inputs when coerced to 2D; defaults to one 
20  19  because the 0th axis most likely describes the batch_size Default value is 1. 

20  value means counting dimensions from the back. Accepted range is  
21  [r, r1] where r = rank(input). Default value is 1.  
21  22 


22  23  **Inputs**  **Inputs** 
23  24 


24  25  * **input** (heterogeneous)  **T**:  * **input** (heterogeneous)  **T**: 
25  26  The input tensor that's coerced into a 2D matrix of size (NxD) as  The input tensor that's coerced into a 2D matrix of size (NxD) as 
26  27  described above.  described above. 
27  28 


28  29  **Outputs**  **Outputs** 
29  30 


30  31  * **output** (heterogeneous)  **T**:  * **output** (heterogeneous)  **T**: 
31  32  The output values with the same shape as input tensor (the original  The output values with the same shape as input tensor (the original 
32  33  size without coercion).  size without coercion). 
33  34 


34  35  **Type Constraints**  **Type Constraints** 
35  36 


36  37  * **T** in (  * **T** in ( 
37  38  tensor(double),  tensor(double), 
38  39  tensor(float),  tensor(float), 
39  40  tensor(float16)  tensor(float16) 
40  41  ):  ): 
41  42  Constrain input and output types to float tensors.  Constrain input and output types to float tensors. 
Hardmax  1#
Version
name: Hardmax (GitHub)
domain: main
since_version: 1
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 1.
Summary
 The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch
of the given input. The input is a 2D tensor (Tensor<float>) of size
(batch_size x input_feature_dimensions). The output tensor has the same shape and contains the hardmax values of the corresponding input.
Input does not need to explicitly be a 2D vector; rather, it will be coerced into one. For an arbitrary ndimensional tensor input in [a_0, a_1, …, a_{k1}, a_k, …, a_{n1}] and k is the axis provided, then input will be coerced into a 2dimensional tensor with dimensions [a_0 * … * a_{k1}, a_k * … * a_{n1}]. For the default case where axis=1, this means the input tensor will be coerced into a 2D tensor of dimensions [a_0, a_1 * … * a_{n1}], where a_0 is often the batch size. In this situation, we must have a_0 = N and a_1 * … * a_{n1} = D. Each of these dimensions must be matched correctly, or else the operator will throw errors.
Attributes
axis: Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size Default value is
1
.
Inputs
input (heterogeneous)  T: The input tensor that’s coerced into a 2D matrix of size (NxD) as described above.
Outputs
output (heterogeneous)  T: The output values with the same shape as input tensor (the original size without coercion).
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.