megengine.functional.transpose#

transpose(inp, pattern)[source]#

Swaps shapes and strides according to given pattern.

Parameters:
  • inp (Tensor) – input tensor.

  • pattern (Iterable[int]) –

    a list of integers including 0, 1, … , ndim-1, and any number of 'x' char in dimensions where this tensor should be broadcasted. For examples:

    • ('x') -> make a 0d (scalar) into a 1d vector

    • (0, 1) -> identity for 2d vectors

    • (1, 0) -> inverts the first and second dimensions

    • ('x', 0) -> make a row out of a 1d vector (N to 1xN)

    • (0, 'x') -> make a column out of a 1d vector (N to Nx1)

    • (2, 0, 1) -> AxBxC to CxAxB

    • (0, 'x', 1) -> AxB to Ax1xB

    • (1, 'x', 0) -> AxB to Bx1xA

    • (1,) -> this removes dimensions 0. It must be a broadcastable dimension (1xA to A)

Return type:

Tensor

Returns:

output tensor.

Examples

>>> import numpy as np
>>> x = Tensor(np.array([[1, 1], [0, 0]], dtype=np.int32))
>>> F.transpose(x, (1, 0))
Tensor([[1 0]
 [1 0]], dtype=int32, device=xpux:0)