Torch.cat for negative dimension

In the following,

 x_6 = torch.cat((x_1, x_2_1, x_3_1, x_5_1), dim=-3)
 Sizes of tensors x_1, x_2_1, x_3_1, x_5_1 are
 torch.Size([1, 256, 7, 7])
 torch.Size([1, 256, 7, 7]) 
 torch.Size([1, 256, 7, 7])
 torch.Size([1, 256, 7, 7]) respectively.
        
 The size of x_6 turns out to be torch.Size([1, 1024, 7, 7])

I couldn’t understand this concatenation along a negative dimension(-3 in this case).
What exactly is happening here?
How does the same go if dim = 3?
Is there any constraint on dim for a given set of tensors?

Negative dimensions start from the end, so -1 would be the last dimension, -2 the one before etc.:

        [1, 256, 7, 7]
pos dim: 0,   1,  2, 3
neg dim: -4, -3, -2, -1

If you are passing dimensions outside of this range, you should get an error:

IndexError: Dimension out of range (expected to be in range of [-4, 3], but got -5)

dim=3 would correspond to dim=-1.

1 Like