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

Function swapaxes

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

r"""Interchange two axes of a tensor. Args: inp: input tensor to swapaxes. axis1: first axis. axis2: second axis. Returns: a tensor after swapping the two axes of 'inp'. Examples: >>> x = Tensor(np.array([[[0,1],[2,3]],[[4,5],[6,7]]], dtype=np.i

(inp: Tensor, axis1: int, axis2: int)

Source from the content-addressed store, hash-verified

968
969
970def swapaxes(inp: Tensor, axis1: int, axis2: int) -> Tensor:
971 r"""Interchange two axes of a tensor.
972
973 Args:
974 inp: input tensor to swapaxes.
975 axis1: first axis.
976 axis2: second axis.
977
978 Returns:
979 a tensor after swapping the two axes of 'inp'.
980
981 Examples:
982 >>> x = Tensor(np.array([[[0,1],[2,3]],[[4,5],[6,7]]], dtype=np.int32))
983 >>> F.swapaxes(x, 0, 2)
984 Tensor([[[0 4]
985 [2 6]]
986 [[1 5]
987 [3 7]]], dtype=int32, device=xpux:0)
988 """
989 pattern = list(range(inp.ndim))
990 tempAxis = pattern[axis1]
991 pattern[axis1] = pattern[axis2]
992 pattern[axis2] = tempAxis
993 return inp.transpose(pattern)
994
995
996def reshape(inp: Tensor, target_shape: Iterable[int]) -> Tensor:

Callers 2

sortFunction · 0.85
restoreFunction · 0.85

Calls 2

listFunction · 0.85
transposeMethod · 0.45

Tested by

no test coverage detected