Compress#
Compress - 11#
Version
name: Compress (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
Selects slices from an input tensor along a given axis where condition evaluates to True for each axis index. In case axis is not provided, input is flattened before elements are selected. Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html
Attributes
axis: (Optional) Axis along which to take slices. If not specified, input is flattened before elements being selected. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input).
Inputs
input (heterogeneous) - T: Tensor of rank r >= 1.
condition (heterogeneous) - T1: Rank 1 tensor of booleans to indicate which slices or data elements to be selected. Its length can be less than the input length along the axis or the flattened input size if axis is not specified. In such cases data slices or elements exceeding the condition length are discarded.
Outputs
output (heterogeneous) - T: Tensor of rank r if axis is specified. Otherwise output is a Tensor of rank 1.
Type Constraints
T 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 input and output types to all tensor types.
T1 in ( tensor(bool) ): Constrains to boolean tensors.
Examples
compress_0
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
axis=0,
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1, 1])
output = np.compress(condition, input, axis=0)
#print(output)
#[[ 3. 4.]
# [ 5. 6.]]
expect(node, inputs=[input, condition.astype(bool)], outputs=[output],
name='test_compress_0')
compress_1
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
axis=1,
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1])
output = np.compress(condition, input, axis=1)
#print(output)
#[[ 2.]
# [ 4.]
# [ 6.]]
expect(node, inputs=[input, condition.astype(bool)], outputs=[output],
name='test_compress_1')
compress_default_axis
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1, 0, 0, 1])
output = np.compress(condition, input)
#print(output)
#[ 2., 5.]
expect(node, inputs=[input, condition.astype(bool)], outputs=[output],
name='test_compress_default_axis')
compress_negative_axis
node = onnx.helper.make_node(
'Compress',
inputs=['input', 'condition'],
outputs=['output'],
axis=-1,
)
input = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)
condition = np.array([0, 1])
output = np.compress(condition, input, axis=-1)
# print(output)
#[[ 2.]
# [ 4.]
# [ 6.]]
expect(node, inputs=[input, condition.astype(bool)], outputs=[output],
name='test_compress_negative_axis')
Differences
0 | 0 | Selects slices from an input tensor along a given axis where condition evaluates to True for each axis index. | Selects slices from an input tensor along a given axis where condition evaluates to True for each axis index. |
1 | 1 | In case axis is not provided, input is flattened before elements are selected. | In case axis is not provided, input is flattened before elements are selected. |
2 | 2 | Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html | Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html |
3 | 3 |
|
|
4 | 4 | **Attributes** | **Attributes** |
5 | 5 |
|
|
6 | 6 | * **axis**: | * **axis**: |
7 | 7 | (Optional) Axis along which to take slices. If not specified, input | (Optional) Axis along which to take slices. If not specified, input |
8 | 8 | is flattened before elements being selected. |
|
9 | counting dimensions from the back. Accepted range is [-r, r-1] where | ||
10 | r = rank(input). | ||
9 | 11 |
|
|
10 | 12 | **Inputs** | **Inputs** |
11 | 13 |
|
|
12 | 14 | * **input** (heterogeneous) - **T**: | * **input** (heterogeneous) - **T**: |
13 | 15 | Tensor of rank r >= 1. | Tensor of rank r >= 1. |
14 | 16 | * **condition** (heterogeneous) - **T1**: | * **condition** (heterogeneous) - **T1**: |
15 | 17 | Rank 1 tensor of booleans to indicate which slices or data elements | Rank 1 tensor of booleans to indicate which slices or data elements |
16 | 18 | to be selected. Its length can be less than the input length alone |
|
17 | 19 | the axis or the flattened input size if axis is not specified. In | the axis or the flattened input size if axis is not specified. In |
18 | 20 | such cases data slices or elements exceeding the condition length | such cases data slices or elements exceeding the condition length |
19 | 21 | are discarded. | are discarded. |
20 | 22 |
|
|
21 | 23 | **Outputs** | **Outputs** |
22 | 24 |
|
|
23 | 25 | * **output** (heterogeneous) - **T**: | * **output** (heterogeneous) - **T**: |
24 | 26 | Tensor of rank r if axis is specified. Otherwise output is a Tensor | Tensor of rank r if axis is specified. Otherwise output is a Tensor |
25 | 27 | of rank 1. | of rank 1. |
26 | 28 |
|
|
27 | 29 | **Type Constraints** | **Type Constraints** |
28 | 30 |
|
|
29 | 31 | * **T** in ( | * **T** in ( |
30 | 32 | tensor(bool), | tensor(bool), |
31 | 33 | tensor(complex128), | tensor(complex128), |
32 | 34 | tensor(complex64), | tensor(complex64), |
33 | 35 | tensor(double), | tensor(double), |
34 | 36 | tensor(float), | tensor(float), |
35 | 37 | tensor(float16), | tensor(float16), |
36 | 38 | tensor(int16), | tensor(int16), |
37 | 39 | tensor(int32), | tensor(int32), |
38 | 40 | tensor(int64), | tensor(int64), |
39 | 41 | tensor(int8), | tensor(int8), |
40 | 42 | tensor(string), | tensor(string), |
41 | 43 | tensor(uint16), | tensor(uint16), |
42 | 44 | tensor(uint32), | tensor(uint32), |
43 | 45 | tensor(uint64), | tensor(uint64), |
44 | 46 | tensor(uint8) | tensor(uint8) |
45 | 47 | ): | ): |
46 | 48 | Constrain input and output types to all tensor types. | Constrain input and output types to all tensor types. |
47 | 49 | * **T1** in ( | * **T1** in ( |
48 | 50 | tensor(bool) | tensor(bool) |
49 | 51 | ): | ): |
50 | 52 | Constrains to boolean tensors. | Constrains to boolean tensors. |
Compress - 9#
Version
name: Compress (GitHub)
domain: main
since_version: 9
function: False
support_level: SupportType.COMMON
shape inference: False
This version of the operator has been available since version 9.
Summary
Selects slices from an input tensor along a given axis where condition evaluates to True for each axis index. In case axis is not provided, input is flattened before elements are selected. Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html
Attributes
axis: (Optional) Axis along which to take slices. If not specified, input is flattened before elements being selected.
Inputs
input (heterogeneous) - T: Tensor of rank r >= 1.
condition (heterogeneous) - T1: Rank 1 tensor of booleans to indicate which slices or data elements to be selected. Its length can be less than the input length alone the axis or the flattened input size if axis is not specified. In such cases data slices or elements exceeding the condition length are discarded.
Outputs
output (heterogeneous) - T: Tensor of rank r if axis is specified. Otherwise output is a Tensor of rank 1.
Type Constraints
T 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 input and output types to all tensor types.
T1 in ( tensor(bool) ): Constrains to boolean tensors.