假设我有来自 Pytorch 或 Keras 预测的概率,结果是 softmax 函数
from scipy.special import softmax
probs = softmax(np.random.randn(20,10),1) # 20 instances and 10 class probabilities
probs
我想从这个 numpy 数组中找到前 5 个索引。我想做的就是在结果上运行一个循环,例如:
for index in top_5_indices:
if index in result:
print('Found')
如果我的结果在前 5 个结果中,我会得到。
Pytorch有top-k功能,我已经看到numpy.argpartition了,但我不知道如何完成这项工作?