Improving classifier decision boundaries and interpretability using nearest neighbors

Johannes Schneider, Arianna Casanova

Abstract

Deep neural networks (DNNs) often exhibit brittleness near decision boundaries, particularly in sparse regions of the feature space. We present Latent-kNN, a "plug-and-play" augmentation that improves accuracy, robustness, and interpretability without requiring model retraining. By integrating a query sample's latent representation with its $k$-nearest neighbors from the training set, Latent-kNN smooths decision boundaries and provides a mechanism for error correction and explanation.

Introduction

Deep neural networks are incredibly powerful, but they are often "brittle" near their decision boundaries—the mathematical "fences" a model builds to separate classes. These boundaries frequently reside in regions of low data density. Because there are few training samples in these gaps, the model can overfit, creating jagged or suboptimal boundaries that are easily fooled by adversarial attacks or noisy labels.

Conceptual representation of a query sample (black) relative to training data (colored) in the latent space.

Furthermore, DNNs are notorious "black boxes." When a model misclassifies an image, it is often hard to determine the underlying cause. Existing solutions typically require retraining or architectural changes, which often leads to a "robustness-accuracy trade-off" where security gains come at the cost of raw performance.

Methods

The authors propose the Latent k-Nearest Neighbor Augmented Classifier (Latent-kNN). This algorithm works as a post-processing step during inference and requires no modifications to the network architecture or training process. The process follows four steps:

  1. Embedding: Pass a query sample through the network to a specific intermediate layer to extract its latent representation.
  2. Neighbor Search: Find the $k$-nearest neighbors of this vector within the original training set using metrics like Cosine similarity.
  3. Weighted Averaging: Calculate a weighted average of the model's raw output and the outputs of its $k$ neighbors.
  4. Final Prediction: Select the class with the highest score from the combined vector.
The Latent-kNN architecture: extracting features from intermediate layers to produce a weighted prediction based on neighbors.

The best results are typically achieved using layers near the end of the network, which capture high-level features while retaining enough information to distinguish similar samples.

Results and Insights

The researchers evaluated Latent-kNN across standard benchmarks including CIFAR-10, CIFAR-100, and ImageNet, using architectures ranging from ResNet to SWIN Transformers.

  • Accuracy and Robustness: Latent-kNN consistently provided gains in top-1 accuracy (0.1% to 3%) without retraining. It showed significant resilience to label noise and improved adversarial defense (e.g., increasing VGG13/CIFAR-10 accuracy under attack from 20.2% to 23.5%).
  • The Sparse Region Hypothesis: The study confirms that misclassifications often occur in low-density regions. Latent-kNN effectively "pulls" the prediction toward the consensus of nearby training points, smoothing the decision boundary.
  • Interpretability: By examining the neighbors that influence a prediction, researchers can identify why a model failed. For instance, neighbors can help correct a model that misclassifies an object based on background color rather than shape.

Conclusion

Latent-kNN offers a simple, effective way to enhance pre-trained models. While it introduces computational overhead during neighbor retrieval, this can be mitigated using optimized vector databases like FAISS. Future work includes extending the method to text and audio domains and exploring its application in generative models.

Summary updated

BibTeX
@article{schneider2025improving,
  author = {Johannes Schneider and Arianna Casanova},
  title = {{Improving classifier decision boundaries and interpretability using nearest neighbors}},
  journal = {Discov. Artif. Intell.},
  year = {2025},
  doi = {10.1007/s44163-025-00369-8},
}
Figure