[Maxpooling] what should the indices be when there're multiple max values?

What to do when there are multiple values in the kernel that equals to the max? For example, for these values when we use torch.argmax :

torch.tensor([[1., 1.],
       [1., 1.]])

The max is simply 1. What should max indices look like? should it be True’s for all occurrence of max:

torch.tensor([[ True,  True],
       [ True,  True]])

Or the first occurrence of the max:

torch.tenso([[ True, False],
       [False, False]])

It is possible to simply retrieves all the maximum indices in the equality maximum value case using Pytorch ?

torch.argmax will return the index of the first max. value when duplicates are seen as described in the docs.

nn.MaxPool2d will return the max. value, so the index doesn’t matter unless you use return_indices in which case also the first index should be returned:

x = torch.ones(1, 1, 2, 2)
pool = nn.MaxPool2d(2, return_indices=True)

out = pool(x)
# (tensor([[[[1.]]]]), tensor([[[[0]]]]))

Thanks for your answer. But it is possible to simply retrieves all the maximum indices ? Not only the first occurrence ?

Yes, you should be able to get the max. value and get the indices of all duplicates afterwards:

x = torch.ones(1, 1, 2, 2)
x[0, 0, 0, 1] = 0.

max_val = x.max()
print((x == max_val))
# tensor([[[[ True, False],
#           [ True,  True]]]])

print((x == max_val).nonzero())
# tensor([[0, 0, 0, 0],
#         [0, 0, 1, 0],
#         [0, 0, 1, 1]])