Principle:Tencent Ncnn Top K Classification
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Classification |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Algorithm for selecting the K highest-scoring predictions from a classification network's output probability distribution.
Description
After a classification network produces a vector of per-class scores (typically via a softmax output layer), the Top-K selection algorithm identifies the K classes with the highest confidence scores. This is the standard method for interpreting classification results, commonly used to report Top-1 accuracy (single best prediction) or Top-5 accuracy (correct class appears in the five best predictions).
The algorithm uses partial sorting, which is more efficient than full sorting when K is much smaller than the total number of classes. For ImageNet-scale classification with 1000 classes and K=5, partial sort performs significantly fewer comparisons than a complete sort.
Usage
Use this principle as the final step of an image classification pipeline, after the forward pass produces per-class score output. Apply whenever interpreting classification network output into human-readable predictions with confidence scores.
Theoretical Basis
Partial sort algorithm:
Given a vector of N class scores and parameter K:
- Pair each score with its class index
- Perform partial sort to move the K largest elements to the front
- The first K elements are the top predictions, sorted by confidence
Time complexity: O(N + K log K) vs O(N log N) for full sort.
Pseudo-code:
// Abstract Top-K selection
pairs = zip(scores, indices) // [(score, class_idx), ...]
partial_sort(pairs, K, descending) // K largest first
for i in 0..K:
print(pairs[i].class_idx, pairs[i].score)