What does dim=-1 mean in torch.cat?

The output of
torch.cat((x, x, x), -1) and torch.cat((x, x, x), 1) seems to be the same but what does it mean to have a negative dimension. It is not mentioned in pytorch documentation that int needs to be non-negative.

Can someone explain what does it mean?

1 Like

Sure.

I guess your x tensor has a shape of length 2.

In torch, dim = -1 means that the operation has to be performed along last dimension, and I think that is why torch.cat((x, x, x,) -1) == torch.cat((x, x, x,), 1)
(not strictly because it’s links and something but you got the idea) in your example.

For better understanding :

x = torch.Tensor([[1, 2, 3]])
x.shape
> torch.Size([1, 3])

torch.cat((x, x, x), 1).shape
> torch.Size([1, 9])

torch.cat((x, x, x), -1).shape
> torch.Size([1, 9])

Hope it makes things clear for you.

Thanks, that’s helpful.

Adding more explanation to @dazzle-me 's answer. I learned -ive dimension like this:
-ive dimension can come in handy when we are too lazy to figure out tensor’s dimension and then
-1 is last dimension and -2 is the second last dimension and then so on