从火炬中删除项目.张肌



我在lua中有以下代码。

我想从scores获得 N 个最大分数的索引及其相应的分数。

看起来我将不得不迭代地从scores中删除当前最大值并再次检索最大值,但找不到合适的方法来做到这一点。

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);
--    print(scores)
end
tmp,pred=torch.max(scores,2);

我希望我没有误解,因为您显示的代码(尤其是foor循环(似乎与您想要做的并不真正相关。无论如何,这就是我会怎么做的。

sr=scores:view(-1,scores:size(1)*scores:size(2))
val,id=sr:sort()
--val is a row vector with the values stored in increasing order
--id will be the corresponding index in sr
--now you can slice val and id from the end to find the N values you want, then you can recover the original index in the scores matrix simply with
col=(index-1)%scores:size(2)+1
row=math.ceil(index/scores:size(2))

希望这有帮助。

最新更新