megengine.functional.reshape

reshape(inp, target_shape)[source]

Reshapes a tensor without changing its data.

Parameters
  • inp (Tensor) – input tensor to reshape.

  • target_shape (Iterable[int]) – target shape compatible with the original shape. One shape dimension is allowed to be -1 . When a shape dimension is -1 , the corresponding output tensor shape dimension must be inferred from the length of the tensor and the remaining dimensions.

Return type

Tensor

Returns

an output tensor having the same data type, elements, and underlying element order as inp .

Examples

>>> x = F.arange(12)
>>> x
Tensor([ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.], device=xpux:0)
>>> F.reshape(x, (3, 4))
Tensor([[ 0.  1.  2.  3.]
 [ 4.  5.  6.  7.]
 [ 8.  9. 10. 11.]], device=xpux:0)
>>> F.reshape(x, (2, -1))
Tensor([[ 0.  1.  2.  3.  4.  5.]
 [ 6.  7.  8.  9. 10. 11.]], device=xpux:0)