How do you calculate the dimension of a tensor?

I was looking at torch.split() and apparently it splits a tensor into chunks along a given dimension, so if I do

a = torch.tensor([1,2])
torch.split(a, 2, 1)

it gives me an error that says IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1).

How can dimension be negative? I thought dimension here means 2-> 0 dimension, [1,2] -> 1 dimension, how ever many [] how ever many dimensions there are. What is this -1 dimension doing here and how does that make sense in the example of torch.split()?

Negative Dimension (-1) is just negative indexing

  • Here it would just mean the last dimension i.e 2 for a 3D tensor, like python list indexing

Some things in dimensions

  • Scalar has no dimensions
  • A 1D tensor will have 1 dimension but it’ll be numbered from 0 i.e 0th dimension, similarly a 2D tensor will have 0th and 1st dimensions as 2 dimensions. (As per your example which is 1D tensor will accept only -1 or 0 as values for dim arg)
  • You can use .shape and .ndim when you checking for the size and no. of dimensions
    image

So the correct code should be
image

1 Like

a = torch.tensor([[1,2]])
torch.split(a, 1, 1)

have a look.

1 Like