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)
Here is my attempt, I demonstrate its behavior using a simple random
scores
tensor:Now, since you want the
N
best indexes for each question (row), let's sort each row of the tensor:Now, let's look at what the return tensors contain:
As you see, the
i-th
row ofvalues
is the sorted version (in increasing order) of thei-th
row ofscores
, and each row inindexes
gives you the corresponding indexes.You can get the
N
best values/indexes for each question (i.e. row) withLet's see their values for the given example, with
N=3
:So, the
k-th
best value for questionj
isN_best_values[{{j},{values:size(2)-k+1}]]
, and its corresponding index in thescores
matrix is given by thisrow, column
values:For example, the first best value (
k=1
) for the second question is99
, which lies at the2nd
row and6th
column inscores
. And you can see thatvalues[{{2},values:size(2)}}]
is99
, and thatindexes[{{2},{indexes:size(2)}}]
gives you6
, which is the column index in thescores
matrix.Hope that I explained my solution well.