Analyzing the Robustness of Nearest Neighbors to Adversarial Examples

Yizhen Wang, Somesh Jha, Kamalika Chaudhuri

TL;DR

This paper introduces a new theoretical framework—analogous to bias-variance theory—to understand why machine learning models are vulnerable to adversarial attacks. Applying this framework to $k$-nearest neighbor ($k$-NN) classifiers, the authors reveal that while standard low-$k$ setups are inherently fragile, a fast-growing $k$ regime can match the robustness of an ideal Bayes Optimal classifier. To make these insights practical, they propose Robust_1NN, a modified 1-NN algorithm that prunes conflicting training data to guarantee robustness even with finite sample sizes.

What Problem Does This Paper Address?

Adversarial examples—small, human-imperceptible perturbations designed to intentionally fool machine learning models—pose a severe security risk in safety-critical applications like autonomous driving and healthcare. Despite intense study, the community has lacked a foundational theoretical understanding of why these vulnerabilities arise. Specifically, it has been unclear whether adversarial examples are an inevitable consequence of the underlying data distribution or simply a symptom of having too few training samples.

This paper tackles this fundamental question. The authors formalize a theoretical framework to isolate these effects and use it to analyze a classic, non-parametric baseline: the $k$-nearest neighbors ($k$-NN) classifier.

Methods

The authors approach the problem through both theoretical analysis and algorithmic design:

  1. The Robustness Framework: They decompose a classifier's lack of robustness into three distinct sources:
  2. Distributional robustness: Vulnerability inherent to the data distribution itself, even with infinite data (analogous to bias).
  3. Finite sample robustness: Vulnerability caused by a lack of training data (analogous to variance).
  4. Algorithmic robustness: Vulnerability introduced by the specific quirks of the training algorithm.
  5. Asymptotic Analysis of $k$-NN: They analyze how $k$-NN behaves in the large-sample limit under two regimes: a Low-$k$ Regime (where $k$ is constant) and a High-$k$ Regime (where $k$ grows as a function of sample size, specifically $k = \Omega(\sqrt{dn \log n})$).
  6. The Robust_1NN Algorithm: Because the high-$k$ regime requires an impractically large number of samples to be accurate in practice, the authors design a modified 1-NN classifier. This algorithm pre-processes the training data using a subroutine called Confident-Label to remove points where label confidence is low. It then computes the largest "$r$-separated" subset, effectively pruning away training points that are close to each other but have opposite labels, creating a clean "buffer zone" around the decision boundary.

Datasets

The authors evaluated their approach on three datasets chosen for their varying dimensionalities and sample sizes:

  • Halfmoon: A classic synthetic 2D dataset used for non-linear classification (2,000 training / 1,000 test samples).
  • MNIST 1v7: A 784-dimensional subset of the popular MNIST handwritten digit dataset, isolating the digits 1 and 7 (2,000 training / 1,000 test samples).
  • Abalone: A 7-dimensional dataset from the UCI machine learning repository used to predict whether an abalone shell is older than 12.5 years based on physical measurements (500 training / 100 test samples).

Key Insights and Ideas

  • The $k$-NN Robustness Paradox: In the low-$k$ regime (like standard 1-NN), a classifier can achieve high accuracy, but its adversarial robustness drops to zero in the large-sample limit wherever class boundaries are ambiguous ($0 < p(y=1|x) < 1$).
  • High $k$ Restores Robustness: If $k$ is scaled up quickly enough relative to the sample size, the $k$-NN classifier achieves uniform convergence. In this regime, its robustness successfully converges to that of the Bayes Optimal classifier.
  • Baking Robustness into Data Geometry: Instead of altering the distance metric during deployment, the authors show that you can achieve robust 1-NN classification simply by cleaning the training data. Forcing an "$r$-separation" distance between opposing classes effectively immunizes the model against small test-time perturbations.

Key Results

  • Superior White-Box Defense: Against both Direct Attacks and Kernel Substitute Attacks, the proposed Robust_1NN algorithm consistently outperformed standard 1-NN and standard adversarial training baselines (ATNN and ATNN-all) on the Halfmoon and Abalone datasets across all attack radii.
  • Black-Box Resilience: Robust_1NN demonstrated excellent resistance to black-box attacks generated by substitute neural networks and kernel classifiers, maintaining high accuracy where other models degraded.
  • The Density Effect: As predicted by their theory, the algorithm performed best on "dense" datasets (Halfmoon and Abalone) where the sample size is large relative to the dimension. On the high-dimensional, sparse MNIST dataset, Robust_1NN was slightly less accurate at very small attack radii but significantly more robust at larger radii compared to the baselines.

Limitations and Future Work

  • The Intermediate $k$ Gap: The paper leaves an open theoretical gap regarding what happens when $k$ grows at an intermediate rate (faster than a constant, but slower than $\Omega(\sqrt{dn \log n})$).
  • The Curse of Dimensionality: While Robust_1NN provides strong guarantees in the large-sample limit, its finite-sample performance degrades on sparse, high-dimensional data (like MNIST), which is a known pain point for all nearest-neighbor methods.
  • Scope Constraints: The current analysis and proposed algorithm are strictly limited to binary classification problems. Extending this framework to multi-class problems and parametric models like deep neural networks remains an open challenge.

Summary updated

BibTeX
@inproceedings{wang2018analyzing,
  author = {Yizhen Wang and Somesh Jha and Kamalika Chaudhuri},
  title = {{Analyzing the Robustness of Nearest Neighbors to Adversarial Examples}},
  booktitle = {International Conference on Machine Learning},
  year = {2018},
  url = {https://openreview.net/forum?id=HyZ1Mnb_WH},
}
Figure