Flatten#

Flatten - 13#

Version

  • name: Flatten (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

Flattens the input tensor into a 2D matrix. If input tensor has shape (d_0, d_1, … d_n) then the output will have shape (d_0 X d_1 … d_(axis-1), d_axis X d_(axis+1) … X dn).

Attributes

  • axis: Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [-r, r], where r is the rank of the input tensor. Negative value means counting dimensions from the back. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 … d_n), where the shape of the input tensor is (d_0, d_1, … d_n). Default value is 1.

Inputs

  • input (heterogeneous) - T: A tensor of rank >= axis.

Outputs

  • output (heterogeneous) - T: A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output.

Type Constraints

  • T in ( tensor(bfloat16), 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 to all tensor types.

Examples

default

shape = (2, 3, 4, 5)
a = np.random.random_sample(shape).astype(np.float32)

for i in range(len(shape)):
    node = onnx.helper.make_node(
        'Flatten',
        inputs=['a'],
        outputs=['b'],
        axis=i,
    )

    new_shape = (1, -1) if i == 0 else (np.prod(shape[0:i]).astype(int), -1)
    b = np.reshape(a, new_shape)
    expect(node, inputs=[a], outputs=[b],
           name='test_flatten_axis' + str(i))

_flatten_with_default_axis

node = onnx.helper.make_node(
    'Flatten',
    inputs=['a'],
    outputs=['b'],  # Default value for axis: axis=1
)

shape = (5, 4, 3, 2)
a = np.random.random_sample(shape).astype(np.float32)
new_shape = (5, 24)
b = np.reshape(a, new_shape)
expect(node, inputs=[a], outputs=[b],
       name='test_flatten_default_axis')

_flatten_negative_axis

shape = (2, 3, 4, 5)
a = np.random.random_sample(shape).astype(np.float32)

for i in range(-len(shape), 0):
    node = onnx.helper.make_node(
        'Flatten',
        inputs=['a'],
        outputs=['b'],
        axis=i,
    )

    new_shape = (np.prod(shape[0:i]).astype(int), -1)
    b = np.reshape(a, new_shape)
    expect(node, inputs=[a], outputs=[b],
           name='test_flatten_negative_axis' + str(abs(i)))

Differences

00Flattens the input tensor into a 2D matrix. If input tensor has shapeFlattens the input tensor into a 2D matrix. If input tensor has shape
11(d_0, d_1, ... d_n) then the output will have shape(d_0, d_1, ... d_n) then the output will have shape
22(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).
33
44**Attributes****Attributes**
55
66* **axis**:* **axis**:
77 Indicate up to which input dimensions (exclusive) should be Indicate up to which input dimensions (exclusive) should be
88 flattened to the outer dimension of the output. The value for axis flattened to the outer dimension of the output. The value for axis
99 must be in the range [-r, r], where r is the rank of the input must be in the range [-r, r], where r is the rank of the input
1010 tensor. Negative value means counting dimensions from the back. When tensor. Negative value means counting dimensions from the back. When
1111 axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n), axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n),
1212 where the shape of the input tensor is (d_0, d_1, ... d_n). Default value is 1. where the shape of the input tensor is (d_0, d_1, ... d_n). Default value is 1.
1313
1414**Inputs****Inputs**
1515
1616* **input** (heterogeneous) - **T**:* **input** (heterogeneous) - **T**:
1717 A tensor of rank >= axis. A tensor of rank >= axis.
1818
1919**Outputs****Outputs**
2020
2121* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
2222 A 2D tensor with the contents of the input tensor, with input A 2D tensor with the contents of the input tensor, with input
2323 dimensions up to axis flattened to the outer dimension of the output dimensions up to axis flattened to the outer dimension of the output
2424 and remaining input dimensions flattened into the inner dimension of and remaining input dimensions flattened into the inner dimension of
2525 the output. the output.
2626
2727**Type Constraints****Type Constraints**
2828
2929* **T** in (* **T** in (
30 tensor(bfloat16),
3031 tensor(bool), tensor(bool),
3132 tensor(complex128), tensor(complex128),
3233 tensor(complex64), tensor(complex64),
3334 tensor(double), tensor(double),
3435 tensor(float), tensor(float),
3536 tensor(float16), tensor(float16),
3637 tensor(int16), tensor(int16),
3738 tensor(int32), tensor(int32),
3839 tensor(int64), tensor(int64),
3940 tensor(int8), tensor(int8),
4041 tensor(string), tensor(string),
4142 tensor(uint16), tensor(uint16),
4243 tensor(uint32), tensor(uint32),
4344 tensor(uint64), tensor(uint64),
4445 tensor(uint8) tensor(uint8)
4546 ): ):
4647 Constrain input and output to all tensor types. Constrain input and output to all tensor types.

Flatten - 11#

Version

  • name: Flatten (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

Flattens the input tensor into a 2D matrix. If input tensor has shape (d_0, d_1, … d_n) then the output will have shape (d_0 X d_1 … d_(axis-1), d_axis X d_(axis+1) … X dn).

Attributes

  • axis: Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [-r, r], where r is the rank of the input tensor. Negative value means counting dimensions from the back. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 … d_n), where the shape of the input tensor is (d_0, d_1, … d_n). Default value is 1.

Inputs

  • input (heterogeneous) - T: A tensor of rank >= axis.

Outputs

  • output (heterogeneous) - T: A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output.

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 to all tensor types.

Differences

00Flattens the input tensor into a 2D matrix. If input tensor has shapeFlattens the input tensor into a 2D matrix. If input tensor has shape
11(d_0, d_1, ... d_n) then the output will have shape(d_0, d_1, ... d_n) then the output will have shape
22(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).
33
44**Attributes****Attributes**
55
66* **axis**:* **axis**:
77 Indicate up to which input dimensions (exclusive) should be Indicate up to which input dimensions (exclusive) should be
88 flattened to the outer dimension of the output. The value for axis flattened to the outer dimension of the output. The value for axis
99 must be in the range [0, R], where R is the rank of the input must be in the range [-r, r], where r is the rank of the input
10 tensor. Negative value means counting dimensions from the back. When
1011 tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n),
1112 d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ... where the shape of the input tensor is (d_0, d_1, ... d_n). Default value is 1.
12 d_n). Default value is 1.
1313
1414**Inputs****Inputs**
1515
1616* **input** (heterogeneous) - **T**:* **input** (heterogeneous) - **T**:
1717 A tensor of rank >= axis. A tensor of rank >= axis.
1818
1919**Outputs****Outputs**
2020
2121* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
2222 A 2D tensor with the contents of the input tensor, with input A 2D tensor with the contents of the input tensor, with input
2323 dimensions up to axis flattened to the outer dimension of the output dimensions up to axis flattened to the outer dimension of the output
2424 and remaining input dimensions flattened into the inner dimension of and remaining input dimensions flattened into the inner dimension of
2525 the output. the output.
2626
2727**Type Constraints****Type Constraints**
2828
2929* **T** in (* **T** in (
3030 tensor(bool), tensor(bool),
3131 tensor(complex128), tensor(complex128),
3232 tensor(complex64), tensor(complex64),
3333 tensor(double), tensor(double),
3434 tensor(float), tensor(float),
3535 tensor(float16), tensor(float16),
3636 tensor(int16), tensor(int16),
3737 tensor(int32), tensor(int32),
3838 tensor(int64), tensor(int64),
3939 tensor(int8), tensor(int8),
4040 tensor(string), tensor(string),
4141 tensor(uint16), tensor(uint16),
4242 tensor(uint32), tensor(uint32),
4343 tensor(uint64), tensor(uint64),
4444 tensor(uint8) tensor(uint8)
4545 ): ):
4646 Constrain input and output to all tensor types. Constrain input and output to all tensor types.

Flatten - 9#

Version

  • name: Flatten (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

Flattens the input tensor into a 2D matrix. If input tensor has shape (d_0, d_1, … d_n) then the output will have shape (d_0 X d_1 … d_(axis-1), d_axis X d_(axis+1) … X dn).

Attributes

  • axis: Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [0, R], where R is the rank of the input tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 … d_n), where the shape of the input tensor is (d_0, d_1, … d_n). Default value is 1.

Inputs

  • input (heterogeneous) - T: A tensor of rank >= axis.

Outputs

  • output (heterogeneous) - T: A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output.

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 to all tensor types.

Differences

00Flattens the input tensor into a 2D matrix. If input tensor has shapeFlattens the input tensor into a 2D matrix. If input tensor has shape
11(d_0, d_1, ... d_n) then the output will have shape(d_0, d_1, ... d_n) then the output will have shape
22(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).
33
44**Attributes****Attributes**
55
66* **axis**:* **axis**:
77 Indicate up to which input dimensions (exclusive) should be Indicate up to which input dimensions (exclusive) should be
88 flattened to the outer dimension of the output. The value for axis flattened to the outer dimension of the output. The value for axis
99 must be in the range [0, R], where R is the rank of the input must be in the range [0, R], where R is the rank of the input
1010 tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X
1111 d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ... d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ...
1212 d_n). Default value is 1. d_n). Default value is 1.
1313
1414**Inputs****Inputs**
1515
1616* **input** (heterogeneous) - **T**:* **input** (heterogeneous) - **T**:
1717 A tensor of rank >= axis. A tensor of rank >= axis.
1818
1919**Outputs****Outputs**
2020
2121* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
2222 A 2D tensor with the contents of the input tensor, with input A 2D tensor with the contents of the input tensor, with input
2323 dimensions up to axis flattened to the outer dimension of the output dimensions up to axis flattened to the outer dimension of the output
2424 and remaining input dimensions flattened into the inner dimension of and remaining input dimensions flattened into the inner dimension of
2525 the output. the output.
2626
2727**Type Constraints****Type Constraints**
2828
2929* **T** in (* **T** in (
30 tensor(bool),
31 tensor(complex128),
32 tensor(complex64),
3033 tensor(double), tensor(double),
3134 tensor(float), tensor(float),
3235 tensor(float16) tensor(float16),
36 tensor(int16),
37 tensor(int32),
38 tensor(int64),
39 tensor(int8),
40 tensor(string),
41 tensor(uint16),
42 tensor(uint32),
43 tensor(uint64),
44 tensor(uint8)
3345 ): ):
3446 Constrain input and output types to float tensors. Constrain input and output to all tensor types.

Flatten - 1#

Version

  • name: Flatten (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

Flattens the input tensor into a 2D matrix. If input tensor has shape (d_0, d_1, … d_n) then the output will have shape (d_0 X d_1 … d_(axis-1), d_axis X d_(axis+1) … X dn).

Attributes

  • axis: Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [0, R], where R is the rank of the input tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 … d_n), where the shape of the input tensor is (d_0, d_1, … d_n). Default value is 1.

Inputs

  • input (heterogeneous) - T: A tensor of rank >= axis.

Outputs

  • output (heterogeneous) - T: A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output.

Type Constraints

  • T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.