pythonfunctionpytorchtensor

What does `gather()` do in PyTorch in layman terms?


What does gather() do? This answer is hard to understand.


Solution

  • The torch.gather function (or torch.Tensor.gather) is a multi-index selection method. Look at the following example from the official docs:

    t = torch.tensor([[1,2],[3,4]])
    r = torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))
    # r now holds:
    # tensor([[ 1,  1],
    #        [ 4,  3]])
    

    Let's start with going through the semantics of the different arguments: The first argument, input, is the source tensor that we want to select elements from. The second, dim, is the dimension (or axis in tensorflow/numpy) that we want to collect along. And finally, index are the indices to index input. As for the semantics of the operation, this is how the official docs explain it:

    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
    

    So let's go through the example.

    the input tensor is [[1, 2], [3, 4]], and the dim argument is 1, i.e. we want to collect from the second dimension. The indices for the second dimension are given as [0, 0] and [1, 0].

    As we "skip" the first dimension (the dimension we want to collect along is 1), the first dimension of the result is implicitly given as the first dimension of the index. That means that the indices hold the second dimension, or the column indices, but not the row indices. Those are given by the indices of the index tensor itself. For the example, this means that the output will have in its first row a selection of the elements of the input tensor's first row as well, as given by the first row of the index tensor's first row. As the column-indices are given by [0, 0], we therefore select the first element of the first row of the input twice, resulting in [1, 1]. Similarly, the elements of the second row of the result are a result of indexing the second row of the input tensor by the elements of the second row of the index tensor, resulting in [4, 3].

    To illustrate this even further, let's swap the dimension in the example:

    t = torch.tensor([[1,2],[3,4]])
    r = torch.gather(t, 0, torch.tensor([[0,0],[1,0]]))
    # r now holds:
    # tensor([[ 1,  2],
    #        [ 3,  2]])
    

    As you can see, the indices are now collected along the first dimension.

    For the example you referred,

    current_Q_values = Q(obs_batch).gather(1, act_batch.unsqueeze(1))
    

    gather will index the rows of the q-values (i.e. the per-sample q-values in a batch of q-values) by the batch-list of actions. The result will be the same as if you had done the following (though it will be much faster than a loop):

    q_vals = []
    for qv, ac in zip(Q(obs_batch), act_batch):
        q_vals.append(qv[ac])
    q_vals = torch.cat(q_vals, dim=0)