megengine.functional.flatten#

flatten(inp, start_axis=0, end_axis=- 1)[源代码]#

通过将子张量从 start_axis 维展平到 end_axis 维,实现对张量的重塑(reshape)。

参数:
  • inp (Tensor) – 输入张量。

  • start_axis (int) – 子张量被展平时的初始维数。 默认: 0

  • end_axis (int) – 子张量被展平时的最终维数。 默认: -1

返回类型:

Tensor

返回:

输出张量。

实际案例

>>> import numpy as np
>>> inp_shape = (2, 2, 3, 3)
>>> x = Tensor(
...     np.arange(36, dtype=np.int32).reshape(inp_shape),
... )
>>> out = F.flatten(x, 2)
>>> x.numpy().shape
(2, 2, 3, 3)
>>> out.numpy().shape
(2, 2, 9)