megengine.functional.reshape

reshape(inp, target_shape)[源代码]

Reshapes a tensor without changing its data.

参数
  • 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.

返回类型

Tensor

返回

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

实际案例

>>> 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)