transpose

paddle. transpose ( x, perm, name=None ) [source]

Permute the data dimensions of input according to perm.

The i-th dimension of the returned tensor will correspond to the perm[i]-th dimension of input.

Parameters
  • x (Tensor) – The input Tensor. It is a N-D Tensor of data types bool, float16, bfloat16, float32, float64, int8, int16, int32, int64, uint8, uint16, complex64, complex128.

  • perm (list|tuple) – Permute the input according to the data of perm.

  • name (str, optional) – The name of this layer. For more information, please refer to Name. Default is None.

Returns

A transposed n-D Tensor, with data type being bool, float32, float64, int32, int64.

Return type

Tensor

Examples

# The following codes in this code block are pseudocode, designed to show the execution logic and results of the function.

x = to_tensor([[[ 1  2  3  4] [ 5  6  7  8] [ 9 10 11 12]]
               [[13 14 15 16] [17 18 19 20] [21 22 23 24]]])
shape(x): return [2,3,4]

# Example 1
perm0 = [1,0,2]
y_perm0 = transpose(x, perm0) # Permute x by perm0

# dim:0 of y_perm0 is dim:1 of x
# dim:1 of y_perm0 is dim:0 of x
# dim:2 of y_perm0 is dim:2 of x
# The above two lines can also be understood as exchanging the zeroth and first dimensions of x

y_perm0.data = [[[ 1  2  3  4]  [13 14 15 16]]
                [[ 5  6  7  8]  [17 18 19 20]]
                [[ 9 10 11 12]  [21 22 23 24]]]
shape(y_perm0): return [3,2,4]

# Example 2
perm1 = [2,1,0]
y_perm1 = transpose(x, perm1) # Permute x by perm1

# dim:0 of y_perm1 is dim:2 of x
# dim:1 of y_perm1 is dim:1 of x
# dim:2 of y_perm1 is dim:0 of x
# The above two lines can also be understood as exchanging the zeroth and second dimensions of x

y_perm1.data = [[[ 1 13]  [ 5 17]  [ 9 21]]
                [[ 2 14]  [ 6 18]  [10 22]]
                [[ 3 15]  [ 7 19]  [11 23]]
                [[ 4 16]  [ 8 20]  [12 24]]]
shape(y_perm1): return [4,3,2]

Examples

>>> import paddle

>>> x = paddle.randn([2, 3, 4])
>>> x_transposed = paddle.transpose(x, perm=[1, 0, 2])
>>> print(x_transposed.shape)
[3, 2, 4]