Transpose/permute

Transpose/permute#

This is a type of transformation that simply switches the indexing of elements in the original array.

import torch

Transpose#

This is tranformation that switches order of the dimentions in the elements. If we had element with diemitonality \((d_1, d_2, ..., d_i, ..., d_j, ..., d_n)\) in tensor where dimentions \(i\) and \(j\) it’ll have dimentionality \((d_1, d_2, ..., d_j, ..., d_i, ..., d_n)\).


We’ll try to switch the dimensions for the tensor defined in the following elements.

original_tensor = torch.arange(24).reshape(2, 3, 4)
original_tensor
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])

This tensor can be written as \(X = \left[x_{ijk}\right]_{2,3,4}\). So it’s idmentionality is \((d_1, d_2, d_3)\).

Consider tensor \(X'=\left[x'_{jik}\right]_{3,2,4}\) - switch of the \(d_1\) and \(d_2\).

original_tensor.transpose(0,1)
tensor([[[ 0,  1,  2,  3],
         [12, 13, 14, 15]],

        [[ 4,  5,  6,  7],
         [16, 17, 18, 19]],

        [[ 8,  9, 10, 11],
         [20, 21, 22, 23]]])

In particular: \(x_{2,3,1}=x'_{3,2,1}=20\)

Consider tensor \(X''=\left[x''_{kji}\right]_{4,3,2}\) - switch of the \(d_1\) and \(d_3\).

original_tensor.transpose(0,2)
tensor([[[ 0,  8],
         [ 4, 12]],

        [[ 1,  9],
         [ 5, 13]],

        [[ 2, 10],
         [ 6, 14]],

        [[ 3, 11],
         [ 7, 15]]])

In particular: \(x_{1,2,3}=x''_{3,2,1}=6\)

Consider tensor \(X'''=\left[x'''_{2,4,3}\right]\) - switch of the \(d_2\) and \(d_3\).

original_tensor.transpose(1,2)
tensor([[[ 0,  4,  8],
         [ 1,  5,  9],
         [ 2,  6, 10],
         [ 3,  7, 11]],

        [[12, 16, 20],
         [13, 17, 21],
         [14, 18, 22],
         [15, 19, 23]]])

In particular: \(x_{2,2,3}=x'''_{2,3,2}=18\).

T attribute#

For two-dimensional tensors, you can use the T attribute to obtain the transposed tensor. This method works in older versions of PyTorch, but it is recommended to use the T attribute only for two-dimensional tensors.


The following example shows the use of the T attribute for the first matrix of the tensor original_tensor.

original_tensor = torch.arange(24).reshape(2, 3, 4)
original_tensor
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])
original_tensor[0].T
tensor([[ 0,  4,  8],
        [ 1,  5,  9],
        [ 2,  6, 10],
        [ 3,  7, 11]])

Permute#

This is a transformation that reorders the dimensions of a tensor according to a specified sequence. If we have a tensor with dimensionality \((d_1, d_2, \dots, d_n)\) and apply permute with the order \((p_1, p_2, \dots, p_n)\), the resulting tensor will have dimensionality \((d_{p_1}, d_{p_2}, \dots, d_{p_n})\).


For example, consider the three-dimensional tensor defined in the following cell:

original_tensor = torch.arange(8).reshape(2, 2, 2)
original_tensor
tensor([[[0, 1],
         [2, 3]],

        [[4, 5],
         [6, 7]]])

The following cells demonstrate all possible permutations of the example tensor:

original_tensor.permute(0, 2, 1)
tensor([[[0, 2],
         [1, 3]],

        [[4, 6],
         [5, 7]]])
original_tensor.permute(1, 0, 2)
tensor([[[0, 1],
         [4, 5]],

        [[2, 3],
         [6, 7]]])
original_tensor.permute(2, 0, 1)
tensor([[[0, 2],
         [4, 6]],

        [[1, 3],
         [5, 7]]])
original_tensor.permute(1, 2, 0)
tensor([[[0, 4],
         [1, 5]],

        [[2, 6],
         [3, 7]]])
original_tensor.permute(2, 1, 0)
tensor([[[0, 4],
         [2, 6]],

        [[1, 5],
         [3, 7]]])