Tensor Operations Worth Mastering: permute(1,0)

Tensor Operations Worth Mastering: permute(1,0)
Photo by ZENG YILI / Unsplash

This series is for machine learning engineers just getting started: it helps you build a clear mental picture of the tensor operations you use all the time in PyTorch and NumPy.

Rather than a reference that aims for strict formal rigor, we will learn through the kinds of code you actually see in real-world source.

Today's topic is permute(1,0)

permuteThis operation is commonly used to change the order of a tensor's dimensions.permuteThe method's arguments specify the new ordering.

For a 2-dimensional tensor, permute(1,0) produces the transposed tensor. Let's walk through why, step by step.

Let's start with a 2×3 tensor like the one below

Since this tensor is 2-dimensional, we can represent it as a table.

In PyTorch, this tensor can be defined as follows.

import torch

x = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])

As noted above, the shape of this tensor is 2 × 3.
In code, we write this as (2,3) or [2,3].

You can get a tensor's shape with .shape, like this

print(f"Shape: {x.shape}")

The output looks like this

Shape: torch.Size([2, 3])

Now, this [2,3] is the size of each dimension.

What lets you change the positions of these dimensions is permute

In our current example,

  • the dimension at position 0 (the rows) has size 2
  • the dimension at position 1 (the columns) has size 3

If we think of each dimension as a "person,"

the grammar of permute is

.

So,

means the following.

Sample code for permute(1,0)

import torch
import numpy as np

x = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])
print("Original tensor:")
print(x)
print(f"Shape: {x.shape}")

# permute: change the order of dimensions
print("\n1. Permute")
print(f"Before: {x.shape}")
y = x.permute(1, 0)
print(f"After permute(1, 0): {y.shape}")
print(y)

The result is a [3,2] tensor, as shown below.

Original tensor:
tensor([[1, 2, 3],
        [4, 5, 6]])
Shape: torch.Size([2, 3])

Permute
Before: torch.Size([2, 3])
After permute(1, 0): torch.Size([3, 2])
tensor([[1, 4],
        [2, 5],
        [3, 6]])

In other words, it has been transposed.

So for a 2-dimensional tensor, permute(1,0) is exactly the "transpose" operation.

Read more