PyTorch method for returning element with highest count in a 1D tensor?

Viewed 3253

Is there a built-in PyTorch method that takes a 1D tensor, and returns the element in the tensor with the highest count?

For example, if we input torch.tensor([2,2,2,3,4,5]), the method should return 2 as it occurs the most. In case of a tie in frequency, the element with the lower value should be returned; inputting torch.tensor([1,1,2,2,4,5]) should return 1.

Just to be clear, I only wish to know if there's an existing built-in PyTorch method that does exactly this. If there's no such method, please refrain from posting the solution, as I'd like to try solving it on my own.

1 Answers

yes torch.mode() is builtin function(read here) which handles both of your conditions.

torch.mode(alpha,0) #alpha being the name of tensor

Related