What does "Gathers values along an axis specified by dim." mean?
I want to understand what does "Gathers values along an axis specified by dim." mean in the below code. How to structure the operation of function on data in my head. What is this function doing to data and how ?

Please refer this link https://pytorch.org/docs/stable/torch.html#torch.gather


torch.gather(input, dim, index, out=None, sparse_grad=False)


Gathers values along an axis specified by dim.

For a 3-D tensor the output is specified by:

out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2



Yes, it goes through the given dim (dimension) of the tensor, and collects into a new tensor the values specified by the index provided. So if I had a 1D tensor (is that allowed?) as 

MyValues = torch.tensor([0,2,4,6,8])


and did 

torch.gather(MyValues, 0, torch.tensor([0,1,3])) 


I'd expect to return a 1D tensor containing [0,2,6]. i.e. the values located at positions 0, 1 and 3.

So it's just picking out the contents using the index tensor as a pointer to the locations of the contents to be extracted from the input tensor.  

The dim is the dimension along which you want to index. So for 2D that'd give you the option of indexing by rows or columns, and you can extrapolate that out into as many dimensions as you like. 
