mindspore.ops.reshape

View Source On Gitee
mindspore.ops.reshape(input, shape)[source]

Rearranges the input Tensor based on the given shape.

The shape can only have one -1 at most, in which case it's inferred from the remaining dimensions and the number of elements in the input.

Parameters
  • input (Tensor) – The shape of tensor is \((x_1, x_2, ..., x_R)\).

  • shape (Union[tuple[int], list[int], Tensor[int]]) – If shape is a tuple or list, its elements should be integers, and only constant value is allowed. i.e., \((y_1, y_2, ..., y_S)\). If shape is a Tensor, data type should be int32 or int64, and only one-dimensional tensor is supported.

Returns

Tensor, If the given shape does not contain -1, the shape of tensor is \((y_1, y_2, ..., y_S)\). If the k-th position in the given shape is -1, the shape of tensor is \((y_1, ..., y_{k-1}, \frac{\prod_{i=1}^{R}x_{i}}{y_1\times ...\times y_{k-1}\times y_{k+1}\times...\times y_S} , y_{k+1}, ..., y_S)\)

Raises
  • ValueError – The given shape contains more than one -1.

  • ValueError – The given shape contains elements less than -1.

  • ValueError – For scenarios where the given shape does not contain -1, the product of elements of the given shape is not equal to the product of the input's shape, \(\prod_{i=1}^{R}x_{i} \ne \prod_{i=1}^{S}y_{i}\), (Namely, it does not match the input's array size). And for scenarios where the given shape contains -1, the product of elements other than -1 of the given shape is an aliquant part of the product of the input's shape \(\prod_{i=1}^{R}x_{i}\).

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> output = ops.reshape(input, (3, 2))
>>> print(output)
[[-0.1  0.3]
 [ 3.6  0.4]
 [ 0.5 -3.2]]