从 torch.Tensor 中删除项目

delete item from torch.Tensor

我在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))

希望这对您有所帮助。