Torch tensor equivalent for numpy.argmax()

5,700 views
Skip to first unread message

Mohit Jain

unread,
Mar 25, 2016, 2:12:06 PM3/25/16
to torch7
Hello,
    I want to know what is the equivalent of the numpy.argmax() function in torch tensors. From the link of conversions (https://github.com/torch/torch7/wiki/Torch-for-Numpy-users) I see that it is simply torch.max(). However, it doesn't seem to work the way I want it to.

maxs, indices = torch.max(torch.exp(predictions))
print(maxs,indices)
print(torch.exp(predictions))

--OUTPUT--
0.9843675088387	nil
 0.0004
 0.0000
 0.0018
 0.0034
 0.0041
 0.0021
 0.0001
 0.9844
 0.0000
 0.0036
[torch.DoubleTensor of size 10]

So, the max is being returned as expected. However, the indices get returned a nil value. Am I doing something wrong here or is that link deprecated?

Regards,
Mohit

Mohit Jain

unread,
Mar 25, 2016, 2:24:10 PM3/25/16
to torch7
This chat among other users also seems to be pointing to some anomaly in the max()/min() functions. Can someone comment?

Gitter chat : https://gitter.im/torch/torch7/archives/2015/12/18

Regards,
Mohit

Mohit Jain

unread,
Mar 25, 2016, 2:26:43 PM3/25/16
to torch7
Found the solution!!

We have to pass the dimension parameter as 1 in order to return the index (I feel this is a bit confusing personally :/ ).

Basically, do this.

maxs, indices = torch.max(torch.exp(adv_pred), 1)

For anyone else who gets stuck.


Regards,
Mohit

On Friday, March 25, 2016 at 11:42:06 PM UTC+5:30, Mohit Jain wrote:

Koustav Mullick

unread,
Mar 26, 2016, 2:52:15 AM3/26/16
to torch7
Hi Mohit,

This is the expected behavior. Actually the second argument is the index along which you want to find the max. So, performing torch.max(a, n) finds the maximum values of a along n-th dimension and returns the corresponding value and it's index.

Whereas simply performing torch.max(a), without specifying any index, simply returns the maximum value in the entire tensor. It does not return any index since it does not know along which dimension to return the value.

Say, I define a tensor 

a = torch.randn(4, 4)

th
> a
-0.6962  0.0353  0.5913  1.0324
 
1.6723 -1.4893  0.1162  0.2568
-0.6003 -0.0142 -0.9288 -0.4990
 
2.0170 -1.3219  0.2130 -0.4947

Now if I perform 

val, idx = torch.max(a, 2)

th
> val
 
1.0324
 
1.6723
-0.0142
 
2.0170
[torch.DoubleTensor of size 4x1]

th
> idx

 
4
 
1
 
2
 
1
[torch.LongTensor of size 4x1]

It returns things as expected. But if I simply do torch.max(a) it will simply return the max value as shown below.

torch.max(a)
2.0169710614992

It is just a number, not associated with any index. The reason for this is it does not know along which dimension to index. Will it be along the row (which is 4) or column (which is 1) or both row and column (4 X 2). Things will get even more complicated for higher dimension.

Hence such a behavior.

Mohit Jain

unread,
Mar 26, 2016, 7:54:32 AM3/26/16
to torch7 on behalf of Koustav Mullick
Agreed. For multi-dimensional cases, this does make sense. :)

--
You received this message because you are subscribed to a topic in the Google Groups "torch7" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/torch7/a1EAEwLn15g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to torch7+un...@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages