算子 | ONNX | TVM |
---|---|---|
NMS | none | none |
topk | TopK | topk (data[, k, axis, ret_type, is_ascend, dtype]) |
Scatter | Scatter (deprecated) | scatter (data, indices, updates, axis) |
RoiAlign | RoiAlign | none |
Resize | Resize | **resize** (data, size[, layout, method, …]) |
NonZero | NonZero | none |
Loop | Loop | none |
NMS
Not found
TopK
Retrieve the top-K largest or smallest elements along a specified axis.
Given an input tensor of shape [a1, a_2, …, a_n, r] and integer argument k, return two outputs:
-Value tensor of shape [a_1, a_2, …, a{axis-1}, k, a{axis+1}, … a_n] which contains the values of the top k elements along the specified axis
-Index tensor of shape [a_1, a_2, …, a{axis-1}, k, a_{axis+1}, … a_n] which contains the indices of the top k elements (original indices from the input tensor).
If “largest” is 1 (the default value) then the k largest elements are returned.
If “sorted” is 1 (the default value) then the resulting k elements will be sorted.
If “sorted” is 0, order of returned ‘Values’ and ‘Indices’ are undefined.
Given two equivalent values, this operator uses the indices along the axis as a tiebreaker. That is, the element with the lower index will appear first.
Version
This version of the operator has been available since version 11 of the default ONNX operator set.
Other versions of this operator: 1, 10
Attributes
axis : int (default is -1)
Dimension on which to do the sort. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input).
largest : int (default is 1)
Whether to return the top-K largest or smallest elements.
sorted : int (default is 1)
Whether to return the elements in sorted order.
Inputs
X : TTensor of shape [a_1, a_2, …, a_n, r]
K : tensor(int64)A 1-D tensor containing a single positive value corresponding to the number of top elements to retrieve
Outputs
Values : TTensor of shape [a1, a_2, …, a{axis-1}, k, a{axis+1}, … a_n] containing top K values from the input tensor
Indices : ITensor of shape [a_1, a_2, …, a{axis-1}, k, a_{axis+1}, … a_n] containing the corresponding input tensor indices for the top K values.
Type Constraints
T : tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(float16), tensor(float), tensor(double)Constrain input and output types to numeric tensors.
I : tensor(int64)Constrain index tensor to int64
Examples
top_k
axis = 1
largest = 1
k = 3
node = onnx.helper.make_node(
'TopK',
inputs=['x', 'k'],
outputs=['values', 'indices'],
axis=axis
)
X = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
], dtype=np.float32)
K = np.array([k], dtype=np.int64)
values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
#print(values_ref)
#[[ 3. 2. 1.]
# [ 7. 6. 5.]
# [11. 10. 9.]]
#print(indices_ref)
#[[3 2 1]
# [3 2 1]
# [3 2 1]]
expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
name='test_top_k')
top_k_negative_axis
axis = -1
largest = 1
k = 3
node = onnx.helper.make_node(
'TopK',
inputs=['x', 'k'],
outputs=['values', 'indices'],
axis=axis
)
X = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
], dtype=np.float32)
K = np.array([k], dtype=np.int64)
values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
# print(values_ref)
#[[ 3. 2. 1.]
# [ 7. 6. 5.]
# [11. 10. 9.]]
# print(indices_ref)
#[[3 2 1]
# [3 2 1]
# [3 2 1]]
expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
name='test_top_k_negative_axis')
top_k_smallest
axis = 1
largest = 0
sorted = 1
k = 3
node = onnx.helper.make_node(
'TopK',
inputs=['x', 'k'],
outputs=['values', 'indices'],
axis=axis,
largest=largest,
sorted=sorted
)
X = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[11, 10, 9, 8],
], dtype=np.float32)
K = np.array([k], dtype=np.int64)
values_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)
#print(values_ref)
#[[ 0. 1. 2.]
# [ 4. 5. 6.]
# [ 8. 9. 10.]]
#print(indices_ref)
#[[0 1 2]
# [0 1 2]
# [3 2 1]]
expect(node, inputs=[X, K], outputs=[values_ref, indices_ref],
name='test_top_k_smallest')
Scatter
This operator is deprecated. Please use ScatterElements, which provides the same functionality.
Scatter takes three inputs data, updates, and indices of the same rank r >= 1 and an optional attribute axis that identifies an axis of data (by default, the outer-most axis, that is axis 0). The output of the operation is produced by creating a copy of the input data, and then updating its value to values specified by updates at specific index positions specified by indices. Its output shape is the same as the shape of data.
For each entry in updates, the target index in data is obtained by combining the corresponding entry in indices with the index of the entry itself: the index-value for dimension = axis is obtained from the value of the corresponding entry in indices and the index-value for dimension != axis is obtained from the index of the entry itself.
For instance, in a 2-D tensor case, the update corresponding to the [i][j] entry is performed as below:
output[indices[i][j]][j] = updates[i][j] if axis = 0,
output[i][indices[i][j]] = updates[i][j] if axis = 1,
This operator is the inverse of GatherElements. It is similar to Torch’s Scatter operation.
Example 1:
data = [
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
]
indices = [
[1, 0, 2],
[0, 2, 1],
]
updates = [
[1.0, 1.1, 1.2],
[2.0, 2.1, 2.2],
]
output = [
[2.0, 1.1, 0.0]
[1.0, 0.0, 2.2]
[0.0, 2.1, 1.2]
]
Example 2:
data = [[1.0, 2.0, 3.0, 4.0, 5.0]]
indices = [[1, 3]]
updates = [[1.1, 2.1]]
axis = 1
output = [[1.0, 1.1, 3.0, 2.1, 5.0]]
Version
This version of the operator has been deprecated since version 13 of the default ONNX operator set.
Other versions of this operator: 9, 11
Examples
scatter_with_axis
axis = 1
node = onnx.helper.make_node(
'Scatter',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
axis=axis,
)
data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
indices = np.array([[1, 3]], dtype=np.int64)
updates = np.array([[1.1, 2.1]], dtype=np.float32)
y = scatter(data, indices, updates, axis=axis)
# print(y) produces
# [[1.0, 1.1, 3.0, 2.1, 5.0]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_with_axis', opset_imports=[helper.make_opsetid("", 10)])
scatter_without_axis
node = onnx.helper.make_node(
'Scatter',
inputs=['data', 'indices', 'updates'],
outputs=['y'],
)
data = np.zeros((3, 3), dtype=np.float32)
indices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)
updates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)
y = scatter(data, indices, updates)
# print(y) produces
# [[2.0, 1.1, 0.0],
# [1.0, 0.0, 2.2],
# [0.0, 2.1, 1.2]]
expect(node, inputs=[data, indices, updates], outputs=[y],
name='test_scatter_without_axis', opset_imports=[helper.make_opsetid("", 10)])