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) ): Constrain 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

00Selects 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.
11In 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.
22Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.htmlCompress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html
33
44**Attributes****Attributes**
55
66* **axis**:* **axis**:
77 (Optional) Axis along which to take slices. If not specified, input (Optional) Axis along which to take slices. If not specified, input
88 is flattened before elements being selected. is flattened before elements being selected. Negative value means
9 counting dimensions from the back. Accepted range is [-r, r-1] where
10 r = rank(input).
911
1012**Inputs****Inputs**
1113
1214* **input** (heterogeneous) - **T**:* **input** (heterogeneous) - **T**:
1315 Tensor of rank r >= 1. Tensor of rank r >= 1.
1416* **condition** (heterogeneous) - **T1**:* **condition** (heterogeneous) - **T1**:
1517 Rank 1 tensor of booleans to indicate which slices or data elements Rank 1 tensor of booleans to indicate which slices or data elements
1618 to be selected. Its length can be less than the input length alone to be selected. Its length can be less than the input length along
1719 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
1820 such cases data slices or elements exceeding the condition length such cases data slices or elements exceeding the condition length
1921 are discarded. are discarded.
2022
2123**Outputs****Outputs**
2224
2325* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
2426 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
2527 of rank 1. of rank 1.
2628
2729**Type Constraints****Type Constraints**
2830
2931* **T** in (* **T** in (
3032 tensor(bool), tensor(bool),
3133 tensor(complex128), tensor(complex128),
3234 tensor(complex64), tensor(complex64),
3335 tensor(double), tensor(double),
3436 tensor(float), tensor(float),
3537 tensor(float16), tensor(float16),
3638 tensor(int16), tensor(int16),
3739 tensor(int32), tensor(int32),
3840 tensor(int64), tensor(int64),
3941 tensor(int8), tensor(int8),
4042 tensor(string), tensor(string),
4143 tensor(uint16), tensor(uint16),
4244 tensor(uint32), tensor(uint32),
4345 tensor(uint64), tensor(uint64),
4446 tensor(uint8) tensor(uint8)
4547 ): ):
4648 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.
4749* **T1** in (* **T1** in (
4850 tensor(bool) tensor(bool)
4951 ): ):
5052 Constrain to boolean tensors. Constrain 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) ): Constrain to boolean tensors.