Reshape#

Reshape - 14#

Version

  • name: Reshape (GitHub)

  • domain: main

  • since_version: 14

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 14.

Summary

Reshape the input tensor similar to numpy.reshape. First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor. At most one dimension of the new shape can be -1. In this case, the value is inferred from the size of the tensor and the remaining dimensions. A dimension could also be 0, in which case the actual dimension value is unchanged (i.e. taken from the input tensor). If ‘allowzero’ is set, and the new shape includes 0, the dimension will be set explicitly to zero (i.e. not taken from input tensor). Shape (second input) could be an empty shape, which means converting to a scalar. The input tensor’s shape and the output tensor’s shape are required to have the same number of elements.

Attributes

  • allowzero: (Optional) By default, when any value in the ‘shape’ input is equal to zero the corresponding dimension value is copied from the input tensor dynamically. allowzero=1 indicates that if any value in the ‘shape’ input is set to zero, the zero value is honored, similar to NumPy. Default value is 0.

Inputs

  • data (heterogeneous) - T: An input tensor.

  • shape (heterogeneous) - tensor(int64): Specified shape for output.

Outputs

  • reshaped (heterogeneous) - T: Reshaped data.

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

Examples

reshape

original_shape = [2, 3, 4]
test_cases = {
    'reordered_all_dims': np.array([4, 2, 3], dtype=np.int64),
    'reordered_last_dims': np.array([2, 4, 3], dtype=np.int64),
    'reduced_dims': np.array([2, 12], dtype=np.int64),
    'extended_dims': np.array([2, 3, 2, 2], dtype=np.int64),
    'one_dim': np.array([24], dtype=np.int64),
    'negative_dim': np.array([2, -1, 2], dtype=np.int64),
    'negative_extended_dims': np.array([-1, 2, 3, 4], dtype=np.int64),
    'zero_dim': np.array([2, 0, 4, 1], dtype=np.int64),
    'zero_and_negative_dim': np.array([2, 0, 1, -1], dtype=np.int64),
}
data = np.random.random_sample(original_shape).astype(np.float32)

for test_name, shape in test_cases.items():
    node = onnx.helper.make_node(
        'Reshape',
        inputs=['data', 'shape'],
        outputs=['reshaped'],
    )

    reshaped = reshape_reference_implementation(data, shape)

    expect(node, inputs=[data, shape], outputs=[reshaped],
           name='test_reshape_' + test_name)

allowzero

original_shape = [0, 3, 4]
test_cases = {
    'allowzero_reordered': np.array([3, 4, 0], dtype=np.int64),
}
data = np.random.random_sample(original_shape).astype(np.float32)

for test_name, shape in test_cases.items():
    node = onnx.helper.make_node(
        'Reshape',
        inputs=['data', 'shape'],
        outputs=['reshaped'],
        allowzero=1,  # if allowzero=1, final shape = (3, 4, 0)
                      # if allowzero=0, final shape = (3, 4, 4)
    )

    reshaped = reshape_reference_implementation(data, shape, allowzero=1)

    expect(node, inputs=[data, shape], outputs=[reshaped],
           name='test_reshape_' + test_name)

Differences

00Reshape the input tensor similar to numpy.reshape.Reshape the input tensor similar to numpy.reshape.
11First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.
22At most one dimension of the new shape can be -1. In this case, the value isAt most one dimension of the new shape can be -1. In this case, the value is
33inferred from the size of the tensor and the remaining dimensions. A dimensioninferred from the size of the tensor and the remaining dimensions. A dimension
44could also be 0, in which case the actual dimension value is unchanged (i.e. takencould also be 0, in which case the actual dimension value is unchanged (i.e. taken
5from the input tensor). If 'allowzero' is set, and the new shape includes 0, the
6dimension will be set explicitly to zero (i.e. not taken from input tensor).
57from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.Shape (second input) could be an empty shape, which means converting to a scalar.
68The input tensor's shape and the output tensor's shape are required to have the same number of elements.The input tensor's shape and the output tensor's shape are required to have the same number of elements.
79
10**Attributes**
11
12* **allowzero**:
13 (Optional) By default, when any value in the 'shape' input is equal
14 to zero the corresponding dimension value is copied from the input
15 tensor dynamically. allowzero=1 indicates that if any value in the
16 'shape' input is set to zero, the zero value is honored, similar to
17 NumPy. Default value is 0.
18
819**Inputs****Inputs**
920
1021* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
1122 An input tensor. An input tensor.
1223* **shape** (heterogeneous) - **tensor(int64)**:* **shape** (heterogeneous) - **tensor(int64)**:
1324 Specified shape for output. Specified shape for output.
1425
1526**Outputs****Outputs**
1627
1728* **reshaped** (heterogeneous) - **T**:* **reshaped** (heterogeneous) - **T**:
1829 Reshaped data. Reshaped data.
1930
2031**Type Constraints****Type Constraints**
2132
2233* **T** in (* **T** in (
2334 tensor(bfloat16), tensor(bfloat16),
2435 tensor(bool), tensor(bool),
2536 tensor(complex128), tensor(complex128),
2637 tensor(complex64), tensor(complex64),
2738 tensor(double), tensor(double),
2839 tensor(float), tensor(float),
2940 tensor(float16), tensor(float16),
3041 tensor(int16), tensor(int16),
3142 tensor(int32), tensor(int32),
3243 tensor(int64), tensor(int64),
3344 tensor(int8), tensor(int8),
3445 tensor(string), tensor(string),
3546 tensor(uint16), tensor(uint16),
3647 tensor(uint32), tensor(uint32),
3748 tensor(uint64), tensor(uint64),
3849 tensor(uint8) tensor(uint8)
3950 ): ):
4051 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.

Reshape - 13#

Version

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

Reshape the input tensor similar to numpy.reshape. First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor. At most one dimension of the new shape can be -1. In this case, the value is inferred from the size of the tensor and the remaining dimensions. A dimension could also be 0, in which case the actual dimension value is unchanged (i.e. taken from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar. The input tensor’s shape and the output tensor’s shape are required to have the same number of elements.

Inputs

  • data (heterogeneous) - T: An input tensor.

  • shape (heterogeneous) - tensor(int64): Specified shape for output.

Outputs

  • reshaped (heterogeneous) - T: Reshaped data.

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

Differences

00Reshape the input tensor similar to numpy.reshape.Reshape the input tensor similar to numpy.reshape.
11First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.
22At most one dimension of the new shape can be -1. In this case, the value isAt most one dimension of the new shape can be -1. In this case, the value is
33inferred from the size of the tensor and the remaining dimensions. A dimensioninferred from the size of the tensor and the remaining dimensions. A dimension
44could also be 0, in which case the actual dimension value is unchanged (i.e. takencould also be 0, in which case the actual dimension value is unchanged (i.e. taken
55from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.
66The input tensor's shape and the output tensor's shape are required to have the same number of elements.The input tensor's shape and the output tensor's shape are required to have the same number of elements.
77
88**Inputs****Inputs**
99
1010* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
1111 An input tensor. An input tensor.
1212* **shape** (heterogeneous) - **tensor(int64)**:* **shape** (heterogeneous) - **tensor(int64)**:
1313 Specified shape for output. Specified shape for output.
1414
1515**Outputs****Outputs**
1616
1717* **reshaped** (heterogeneous) - **T**:* **reshaped** (heterogeneous) - **T**:
1818 Reshaped data. Reshaped data.
1919
2020**Type Constraints****Type Constraints**
2121
2222* **T** in (* **T** in (
23 tensor(bfloat16),
2324 tensor(bool), tensor(bool),
2425 tensor(complex128), tensor(complex128),
2526 tensor(complex64), tensor(complex64),
2627 tensor(double), tensor(double),
2728 tensor(float), tensor(float),
2829 tensor(float16), tensor(float16),
2930 tensor(int16), tensor(int16),
3031 tensor(int32), tensor(int32),
3132 tensor(int64), tensor(int64),
3233 tensor(int8), tensor(int8),
3334 tensor(string), tensor(string),
3435 tensor(uint16), tensor(uint16),
3536 tensor(uint32), tensor(uint32),
3637 tensor(uint64), tensor(uint64),
3738 tensor(uint8) tensor(uint8)
3839 ): ):
3940 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.

Reshape - 5#

Version

  • name: Reshape (GitHub)

  • domain: main

  • since_version: 5

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 5.

Summary

Reshape the input tensor similar to numpy.reshape. First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor. At most one dimension of the new shape can be -1. In this case, the value is inferred from the size of the tensor and the remaining dimensions. A dimension could also be 0, in which case the actual dimension value is unchanged (i.e. taken from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar. The input tensor’s shape and the output tensor’s shape are required to have the same number of elements.

Inputs

  • data (heterogeneous) - T: An input tensor.

  • shape (heterogeneous) - tensor(int64): Specified shape for output.

Outputs

  • reshaped (heterogeneous) - T: Reshaped data.

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.

Differences

00Reshape the input tensor similar to numpy.reshape.Reshape the input tensor similar to numpy.reshape.
11It takes a tensor as input and an argument shape. It outputs the reshaped tensor.First input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.
22At most one dimension of the new shape can be -1. In this case, the value isAt most one dimension of the new shape can be -1. In this case, the value is
33inferred from the size of the tensor and the remaining dimensions. A dimensioninferred from the size of the tensor and the remaining dimensions. A dimension
44could also be 0, in which case the actual dimension value is unchanged (i.e. takencould also be 0, in which case the actual dimension value is unchanged (i.e. taken
55from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.
66The input tensor's shape and the output tensor's shape are required to have the same number of elements.The input tensor's shape and the output tensor's shape are required to have the same number of elements.
77
8**Attributes**
9
10* **consumed_inputs**:
11 legacy optimization attribute.
12* **shape**:
13 New shape
14
158**Inputs****Inputs**
169
1710* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
11 An input tensor.
1812 An input tensor.* **shape** (heterogeneous) - **tensor(int64)**:
13 Specified shape for output.
1914
2015**Outputs****Outputs**
2116
2217* **reshaped** (heterogeneous) - **T**:* **reshaped** (heterogeneous) - **T**:
2318 Reshaped data. Reshaped data.
2419
2520**Type Constraints****Type Constraints**
2621
2722* **T** in (* **T** in (
23 tensor(bool),
24 tensor(complex128),
25 tensor(complex64),
2826 tensor(double), tensor(double),
2927 tensor(float), tensor(float),
3028 tensor(float16) tensor(float16),
29 tensor(int16),
30 tensor(int32),
31 tensor(int64),
32 tensor(int8),
33 tensor(string),
34 tensor(uint16),
35 tensor(uint32),
36 tensor(uint64),
37 tensor(uint8)
3138 ): ):
3239 Constrain input and output types to float tensors. Constrain input and output types to all tensor types.

Reshape - 1#

Version

  • name: Reshape (GitHub)

  • domain: main

  • since_version: 1

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: False

This version of the operator has been available since version 1.

Summary

Reshape the input tensor similar to numpy.reshape. It takes a tensor as input and an argument shape. It outputs the reshaped tensor. At most one dimension of the new shape can be -1. In this case, the value is inferred from the size of the tensor and the remaining dimensions. A dimension could also be 0, in which case the actual dimension value is unchanged (i.e. taken from the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar. The input tensor’s shape and the output tensor’s shape are required to have the same number of elements.

Attributes

  • consumed_inputs: legacy optimization attribute.

  • shape: New shape

Inputs

  • data (heterogeneous) - T: An input tensor.

Outputs

  • reshaped (heterogeneous) - T: Reshaped data.

Type Constraints

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