torch / torch7

http://torch.ch
Other
9k stars 2.38k forks source link

Seems bug in torch.mode #1012

Closed lambdawill closed 7 years ago

lambdawill commented 7 years ago
th> a = torch.rand(10):round()

th> a
 0
 1
 1
 1
 0
 1
 1
 0
 0
 1

th> y,i = torch.mode(a)

th> y
 1
[torch.DoubleTensor of size 1]

                                                                      [0.0001s]
th> i
 10
[torch.LongTensor of size 1]

When using 2D tensor

th> a = torch.rand(4,5):round()
                                                                      [0.0001s]
th> y,i = torch.mode(a)
                                                                      [0.0000s]

th> a
 0  1  0  1  1
 1  0  1  0  0
 0  0  0  1  0
 0  1  0  1  0
[torch.DoubleTensor of size 4x5]
                                                                      [0.0002s]

th> y
 1
 0
 0
 0
[torch.DoubleTensor of size 4x1]
                                                                      [0.0001s]

th> i
 5
 5
 5
 5
[torch.LongTensor of size 4x1]
pavanky commented 7 years ago

What is the bug here?

lambdawill commented 7 years ago

@pavanky I hope the answer to the first example should be

2
3
4
6
7
10

For the second example, all equals '5' ? It is quite strange! Maybe it should output

5
3
4
1

I cannot understand what the variable 'i' really means according to the output.

pavanky commented 7 years ago

@TYLVY It looks like it is returning the last instance of the mode inside the tensor. It so happens both of your examples have mode as the last element. May be try a few other examples?

wickedfoo commented 7 years ago

The choice of the index is completely arbitrary but correct. The guarantee it is providing is "some index whose value in the original tensor is equal to the mode", not the first index etc.