Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a dimensional range of [-1,0] in Pytorch?

So I'm struggling to understand some terminology about collections in Pytorch. I keep running into the same kinds of errors about the range of my tensors being incorrect, and when I try to Google for a solution often the explanations are further confusing.

Here is an example:

m = torch.nn.LogSoftmax(dim=1)
input = torch.tensor([0.3300, 0.3937, -0.3113, -0.2880])
output = m(input)

I don't see anything wrong with the above code, and I've defined my LogSoftmax to accept a 1 dimensional input. So according to my experience with other programming languages the collection [0.3300, 0.3937, -0.3113, -0.2880] is a single dimension.

The above triggers the following error for m(input):

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

What does that mean?

I passed in a one dimensional tensor, but then it tells me that it was expecting a range of [-1, 0], but got 1.

  • A range of what?
  • Why is the error comparing a dimension of 1 to [-1, 0]?
  • What do the two numbers [-1, 0] mean?

I searched for an explanation for this error, and I find things like this link which make no sense to me as a programmer:

https://github.com/pytorch/pytorch/issues/5554#issuecomment-370456868

So I was able to fix the above code by adding another dimension to my tensor data.

m = torch.nn.LogSoftmax(dim=1)
input = torch.tensor([[-0.3300, 0.3937, -0.3113, -0.2880]])
output = m(input)

So that works, but I don't understand how [-1,0] explains a nested collection.

Further experiments showed that the following also works:

m = torch.nn.LogSoftmax(dim=1)
input = torch.tensor([[0.0, 0.1], [1.0, 0.1], [2.0, 0.1]])
output = m(input)

So dim=1 means a collection of collections, but I don't understand how that means [-1, 0].

When I try using LogSoftmax(dim=2)

m = torch.nn.LogSoftmax(dim=2)
input = torch.tensor([[0.0, 0.1], [1.0, 0.1], [2.0, 0.1]])
output = m(input)

The above gives me the following error:

IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)

Confusion again that dim=2 equals [-2, 1], because where did the 1 value come from?

I can fix the error above by nesting collections another level, but at this point I don't understand what values LogSoftmax is expecting.

m = torch.nn.LogSoftmax(dim=2)
input = torch.tensor([[[0.0, 0.1]], [[1.0, 0.1]], [[2.0, 0.1]]])
output = m(input)

I am super confused by this terminology [-1, 0] and [-2, 1]?

If the first value is the nested depth, then why is it negative and what could the second number mean?

There is no error code associated with this error. So it's been difficult to find documentation on the subject. It appears to be an extremely common error people get confused by and nothing that I can find in the Pytorch documentation that talks specifically about it.

like image 863
Reactgular Avatar asked Jan 12 '20 13:01

Reactgular


People also ask

What does view (- 1 do in PyTorch?

It'll modify the tensor metadata and will not create a copy of it.

What is dimension in a tensor PyTorch?

The tensor itself is 2-dimensional, having 3 rows and 4 columns. The type of the object returned is torch. Tensor , which is an alias for torch. FloatTensor ; by default, PyTorch tensors are populated with 32-bit floating point numbers.

How do you find the dimensions of a tensor in PyTorch?

In PyTorch, there are two ways of checking the dimension of a tensor: . size() and . shape . Note that the former is a function call, whereas the later is a property.

How do you rescale a tensor in the range 0 1 and sum to 1 in PyTorch?

We can rescale an n-dimensional input Tensor such that the elements lie within the range [0,1] and sum to 1. To do this, we can apply the Softmax() function. We can rescale the n-dimensional input tensor along a particular dimension. The size of the output tensor is the same as the input tensor.


1 Answers

When specifying a tensor's dimension as an argument for a function (e.g. m = torch.nn.LogSoftmax(dim=1)) you can either use positive dimension indexing starting with 0 for the first dimension, 1 for the second etc.
Alternatively, you can use negative dimension indexing to start from the last dimension to the first: -1 indicate the last dimension, -2 the second from last etc.

Example:
If you have a 4D tensor of dimensions b-by-c-by-h-by-w then

  • The "batch" dimension (the first) can be accessed as either dim=0 or dim=-4.
  • The "channel" dimension (the second) can be accessed as either dim=1 or dim=-3.
  • The "height"/"vertical" dimension (the third) can be accessed as either dim=2 or dim=-2.
  • The "width"/"horizontal" dimension (the fourth) can be accessed as either dim=3 or dim=-1.

Therefore, if you have a 4D tensor dim argument can take values in the range [-4, 3].

In your case you have a 1D tensor and therefore dim argument can be wither 0 or -1 (which in this deprecate case amounts to the same dimension).

like image 66
Shai Avatar answered Oct 12 '22 18:10

Shai