What does the -1 mean in tensor.size(-1)?
I have seen something like this in the Pytorch documentation,

import torch

a = torch.tensor([1, 2])
a.size() # torch.Size([2])
a.size(-1) # 2


How does this work? I didn't find a description. Thanks,

a.size(-1) refers to the last dimension. For example, if the shape of x were (10,20) then x.size(-1) refers to second dimension i.e. 20.
Take a look at the following examples:

import torch
a= torch.zeros((2,5)) # a is matrix of 2 rows and 5 columns all elements are 0
#size gives a 1d tensor containing the shapes
a.size(-1)# refers to the last element in the tensor


This is equivalent to:

a_size= a.size()
a_size(-1)


Hope this helps you.
