Understand PyTorch Tensor None Index – PyTorch Tutorial

By | July 11, 2023

Sometimes, we may find some None index in a pytorch tensor. What do it mean? In this tutorial, we will discuss it.

For example:

f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2)  # bs,n,t

In this example, we can find a None index. f0[:, None]

What is None index in tensor?

In pytorch, None index means to insert a dimension.

For example:

  • x[None] == x.unsqueeze(0)
  • x[:,None] == x.unsqueeze(1)
  • x[:,:,None] = x.unsqueeze(2)

We can use an example to show its effect.

import torch

x = torch.randn(3,2)
y = x.unsqueeze(1)
print(y.shape)

z = x[None]
print(z.shape)
z = x[:,None]
print(z.shape)
z = x[:,:,None]
print(z.shape)

Run this code, we will see:

torch.Size([3, 1, 2])
torch.Size([1, 3, 2])
torch.Size([3, 1, 2])
torch.Size([3, 2, 1])