Search code examples
luatorch

torch / lua: retrieving n-best subset from Tensor


I have following code now, which stores the indices with the maximum score for each question in pred, and convert it to string.

I want to do the same for n-best indices for each question, not just single index with the maximum score, and convert them to string. I also want to display the score for each index (or each converted string).

So scores will have to be sorted, and pred will have to be multiple rows/columns instead of 1 x nqs. And corresponding score value for each entry in pred must be retrievable.

I am clueless as to lua/torch syntax, and any help would be greatly appreciated.

nqs=dataset['question']:size(1);
scores=torch.Tensor(nqs,noutput);
qids=torch.LongTensor(nqs);
for i=1,nqs,batch_size do
    xlua.progress(i, nqs)
    r=math.min(i+batch_size-1,nqs);
    scores[{{i,r},{}}],qids[{{i,r}}]=forward(i,r);
end

tmp,pred=torch.max(scores,2);

answer=json_file['ix_to_ans'][tostring(pred[{i,1}])]
print(answer)

Solution

  • Here is my attempt, I demonstrate its behavior using a simple random scores tensor:

    > scores=torch.floor(torch.rand(4,10)*100)
    > =scores
     9   1  90  12  62   1  62  86  46  27
     7   4   7   4  71  99  33  48  98  63
     82   5  73  84  61  92  81  99  65   9
     33  93  64  77  36  68  89  44  19  25
    [torch.DoubleTensor of size 4x10]
    

    Now, since you want the N best indexes for each question (row), let's sort each row of the tensor:

    > values,indexes=scores:sort(2)
    

    Now, let's look at what the return tensors contain:

    > =values
      1   1   9  12  27  46  62  62  86  90
      4   4   7   7  33  48  63  71  98  99
      5   9  61  65  73  81  82  84  92  99
      19  25  33  36  44  64  68  77  89  93
      [torch.DoubleTensor of size 4x10]
    
    > =indexes
      2   6   1   4  10   9   5   7   8   3
      2   4   1   3   7   8  10   5   9   6
      2  10   5   9   3   7   1   4   6   8
      9  10   1   5   8   3   6   4   7   2
      [torch.LongTensor of size 4x10]
    

    As you see, the i-th row of values is the sorted version (in increasing order) of the i-th row of scores, and each row in indexes gives you the corresponding indexes.

    You can get the N best values/indexes for each question (i.e. row) with

    > N_best_indexes=indexes[{{},{indexes:size(2)-N+1,indexes:size(2)}}]
    > N_best_values=values[{{},{values:size(2)-N+1,values:size(2)}}]
    

    Let's see their values for the given example, with N=3:

    > return N_best_indexes
     7  8  3
     5  9  6
     4  6  8
     4  7  2
    [torch.LongTensor of size 4x3]
    
    > return N_best_values
     62  86  90
     71  98  99
     84  92  99
     77  89  93
    [torch.DoubleTensor of size 4x3]
    

    So, the k-th best value for question j is N_best_values[{{j},{values:size(2)-k+1}]], and its corresponding index in the scores matrix is given by this row, column values:

    row=j
    column=N_best_indexes[{{j},indexes:size(2)-k+1}}]. 
    

    For example, the first best value (k=1) for the second question is 99, which lies at the 2nd row and 6th column in scores. And you can see that values[{{2},values:size(2)}}] is 99, and that indexes[{{2},{indexes:size(2)}}] gives you 6, which is the column index in the scores matrix.

    Hope that I explained my solution well.