MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / flatten

Function flatten

imperative/python/megengine/functional/tensor.py:1023–1046  ·  view source on GitHub ↗

r"""Reshapes the tensor by flattening the sub-tensor from dimension ``start_axis`` to dimension ``end_axis``. Args: inp: input tensor. start_axis: start dimension that the sub-tensor to be flattened. Default: 0 end_axis: end dimension that the sub-tensor to be flattened.

(inp: Tensor, start_axis: int = 0, end_axis: int = -1)

Source from the content-addressed store, hash-verified

1021
1022
1023def flatten(inp: Tensor, start_axis: int = 0, end_axis: int = -1) -> Tensor:
1024 r"""Reshapes the tensor by flattening the sub-tensor from dimension ``start_axis`` to dimension ``end_axis``.
1025
1026 Args:
1027 inp: input tensor.
1028 start_axis: start dimension that the sub-tensor to be flattened. Default: 0
1029 end_axis: end dimension that the sub-tensor to be flattened. Default: -1
1030
1031 Returns:
1032 output tensor.
1033
1034 Examples:
1035 >>> import numpy as np
1036 >>> inp_shape = (2, 2, 3, 3)
1037 >>> x = Tensor(
1038 ... np.arange(36, dtype=np.int32).reshape(inp_shape),
1039 ... )
1040 >>> out = F.flatten(x, 2)
1041 >>> x.numpy().shape
1042 (2, 2, 3, 3)
1043 >>> out.numpy().shape
1044 (2, 2, 9)
1045 """
1046 return inp.flatten(start_axis, end_axis)
1047
1048
1049def expand_dims(inp: Tensor, axis: Union[int, Sequence[int]]) -> Tensor:

Callers 1

forwardMethod · 0.85

Calls 1

flattenMethod · 0.45

Tested by

no test coverage detected