# 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.

If the attribute ‘allowzero’ is set, it is invalid for the specified shape to contain both a zero value and -1, as the value of the dimension corresponding to -1 cannot be determined uniquely.

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(, 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

 `0` `0` `Reshape the input tensor similar to numpy.reshape.` `Reshape the input tensor similar to numpy.reshape.` `1` `1` `First 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.` `2` `2` `At most one dimension of the new shape can be -1. In this case, the value is` `At most one dimension of the new shape can be -1. In this case, the value is` `3` `3` `inferred from the size of the tensor and the remaining dimensions. A dimension` `inferred from the size of the tensor and the remaining dimensions. A dimension` `4` `4` `could also be 0, in which case the actual dimension value is unchanged (i.e. taken` `could also be 0, in which case the actual dimension value is unchanged (i.e. taken` `5` `from the input tensor). If 'allowzero' is set, and the new shape includes 0, the` `6` `dimension will be set explicitly to zero (i.e. not taken from input tensor).` `5` `7` `from 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.` `6` `8` `The 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.` `7` `9` `10` `If the attribute 'allowzero' is set, it is invalid for the specified shape to` `11` `contain both a zero value and -1, as the value of the dimension corresponding` `12` `to -1 cannot be determined uniquely.` `13` `14` `**Attributes**` `15` `16` `* **allowzero**:` `17` ` (Optional) By default, when any value in the 'shape' input is equal` `18` ` to zero the corresponding dimension value is copied from the input` `19` ` tensor dynamically. allowzero=1 indicates that if any value in the` `20` ` 'shape' input is set to zero, the zero value is honored, similar to` `21` ` NumPy. Default value is 0.` `22` `8` `23` `**Inputs**` `**Inputs**` `9` `24` `10` `25` `* **data** (heterogeneous) - **T**:` `* **data** (heterogeneous) - **T**:` `11` `26` ` An input tensor.` ` An input tensor.` `12` `27` `* **shape** (heterogeneous) - **tensor(int64)**:` `* **shape** (heterogeneous) - **tensor(int64)**:` `13` `28` ` Specified shape for output.` ` Specified shape for output.` `14` `29` `15` `30` `**Outputs**` `**Outputs**` `16` `31` `17` `32` `* **reshaped** (heterogeneous) - **T**:` `* **reshaped** (heterogeneous) - **T**:` `18` `33` ` Reshaped data.` ` Reshaped data.` `19` `34` `20` `35` `**Type Constraints**` `**Type Constraints**` `21` `36` `22` `37` `* **T** in (` `* **T** in (` `23` `38` ` tensor(bfloat16),` ` tensor(bfloat16),` `24` `39` ` tensor(bool),` ` tensor(bool),` `25` `40` ` tensor(complex128),` ` tensor(complex128),` `26` `41` ` tensor(complex64),` ` tensor(complex64),` `27` `42` ` tensor(double),` ` tensor(double),` `28` `43` ` tensor(float),` ` tensor(float),` `29` `44` ` tensor(float16),` ` tensor(float16),` `30` `45` ` tensor(int16),` ` tensor(int16),` `31` `46` ` tensor(int32),` ` tensor(int32),` `32` `47` ` tensor(int64),` ` tensor(int64),` `33` `48` ` tensor(int8),` ` tensor(int8),` `34` `49` ` tensor(string),` ` tensor(string),` `35` `50` ` tensor(uint16),` ` tensor(uint16),` `36` `51` ` tensor(uint32),` ` tensor(uint32),` `37` `52` ` tensor(uint64),` ` tensor(uint64),` `38` `53` ` tensor(uint8)` ` tensor(uint8)` `39` `54` ` ):` ` ):` `40` `55` ` 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

 `0` `0` `Reshape the input tensor similar to numpy.reshape.` `Reshape the input tensor similar to numpy.reshape.` `1` `1` `First 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.` `2` `2` `At most one dimension of the new shape can be -1. In this case, the value is` `At most one dimension of the new shape can be -1. In this case, the value is` `3` `3` `inferred from the size of the tensor and the remaining dimensions. A dimension` `inferred from the size of the tensor and the remaining dimensions. A dimension` `4` `4` `could also be 0, in which case the actual dimension value is unchanged (i.e. taken` `could also be 0, in which case the actual dimension value is unchanged (i.e. taken` `5` `5` `from 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.` `6` `6` `The 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.` `7` `7` `8` `8` `**Inputs**` `**Inputs**` `9` `9` `10` `10` `* **data** (heterogeneous) - **T**:` `* **data** (heterogeneous) - **T**:` `11` `11` ` An input tensor.` ` An input tensor.` `12` `12` `* **shape** (heterogeneous) - **tensor(int64)**:` `* **shape** (heterogeneous) - **tensor(int64)**:` `13` `13` ` Specified shape for output.` ` Specified shape for output.` `14` `14` `15` `15` `**Outputs**` `**Outputs**` `16` `16` `17` `17` `* **reshaped** (heterogeneous) - **T**:` `* **reshaped** (heterogeneous) - **T**:` `18` `18` ` Reshaped data.` ` Reshaped data.` `19` `19` `20` `20` `**Type Constraints**` `**Type Constraints**` `21` `21` `22` `22` `* **T** in (` `* **T** in (` `23` ` tensor(bfloat16),` `23` `24` ` tensor(bool),` ` tensor(bool),` `24` `25` ` tensor(complex128),` ` tensor(complex128),` `25` `26` ` tensor(complex64),` ` tensor(complex64),` `26` `27` ` tensor(double),` ` tensor(double),` `27` `28` ` tensor(float),` ` tensor(float),` `28` `29` ` tensor(float16),` ` tensor(float16),` `29` `30` ` tensor(int16),` ` tensor(int16),` `30` `31` ` tensor(int32),` ` tensor(int32),` `31` `32` ` tensor(int64),` ` tensor(int64),` `32` `33` ` tensor(int8),` ` tensor(int8),` `33` `34` ` tensor(string),` ` tensor(string),` `34` `35` ` tensor(uint16),` ` tensor(uint16),` `35` `36` ` tensor(uint32),` ` tensor(uint32),` `36` `37` ` tensor(uint64),` ` tensor(uint64),` `37` `38` ` tensor(uint8)` ` tensor(uint8)` `38` `39` ` ):` ` ):` `39` `40` ` 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

 `0` `0` `Reshape the input tensor similar to numpy.reshape.` `Reshape the input tensor similar to numpy.reshape.` `1` `1` `It 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.` `2` `2` `At most one dimension of the new shape can be -1. In this case, the value is` `At most one dimension of the new shape can be -1. In this case, the value is` `3` `3` `inferred from the size of the tensor and the remaining dimensions. A dimension` `inferred from the size of the tensor and the remaining dimensions. A dimension` `4` `4` `could also be 0, in which case the actual dimension value is unchanged (i.e. taken` `could also be 0, in which case the actual dimension value is unchanged (i.e. taken` `5` `5` `from 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.` `6` `6` `The 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.` `7` `7` `8` `**Attributes**` `9` `10` `* **consumed_inputs**:` `11` ` legacy optimization attribute.` `12` `* **shape**:` `13` ` New shape` `14` `15` `8` `**Inputs**` `**Inputs**` `16` `9` `17` `10` `* **data** (heterogeneous) - **T**:` `* **data** (heterogeneous) - **T**:` `11` ` An input tensor.` `18` `12` ` An input tensor.` `* **shape** (heterogeneous) - **tensor(int64)**:` `13` ` Specified shape for output.` `19` `14` `20` `15` `**Outputs**` `**Outputs**` `21` `16` `22` `17` `* **reshaped** (heterogeneous) - **T**:` `* **reshaped** (heterogeneous) - **T**:` `23` `18` ` Reshaped data.` ` Reshaped data.` `24` `19` `25` `20` `**Type Constraints**` `**Type Constraints**` `26` `21` `27` `22` `* **T** in (` `* **T** in (` `23` ` tensor(bool),` `24` ` tensor(complex128),` `25` ` tensor(complex64),` `28` `26` ` tensor(double),` ` tensor(double),` `29` `27` ` tensor(float),` ` tensor(float),` `30` `28` ` 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)` `31` `38` ` ):` ` ):` `32` `39` ` 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.