Predicting Out-of-Domain Generalization with Neighborhood Invariance

Nathan Hoyen Ng, Neha Hulkund, Kyunghyun Cho, Marzyeh Ghassemi

Abstract

Developing and deploying machine learning models safely depends on the ability to characterize and compare their abilities to generalize to new environments. Although recent work has proposed a variety of methods that can directly predict or theoretically bound the generalization capacity of a model, they rely on strong assumptions such as matching train/test distributions and access to model gradients. In order to characterize generalization when these assumptions are not satisfied, we propose neighborhood invariance, a measure of a classifier’s output invariance in a local transformation neighborhood. Specifically, we sample a set of transformations and given an input test point, calculate the invariance as the largest fraction of transformed points classified into the same class. Crucially, our measure is simple to calculate, does not depend on the test point’s true label, makes no assumptions about the data distribution or model, and can be applied even in out-of-domain (OOD) settings where existing methods cannot, requiring only selecting a set of appropriate data transformations. In experiments on robustness benchmarks in image classification, sentiment analysis, and natural language inference, we demonstrate a strong and robust correlation between our neighborhood invariance measure and actual OOD generalization on over 4,600 models evaluated on over 100 train/test domain pairs.

The Challenge of OOD Generalization

When machine learning models are deployed in real-world scenarios—such as autonomous vehicles navigating new cities or medical AI systems operating in different hospitals—they frequently encounter "distribution shift." In these out-of-domain (OOD) settings, model performance typically drops. Predicting the extent of this performance degradation is a critical challenge, especially since ground-truth labels for the new environment are often unavailable at test time.

Existing methods for predicting the "generalization gap" often suffer from three major limitations:

  1. Distribution Assumptions: Many assume that training and test data come from the same distribution, which is rarely the case in OOD scenarios.
  2. Model Access Requirements: Some methods require "white-box" access to model gradients or internal weights, making them unusable for third-party APIs or black-box models.
  3. Calibration Failures: Confidence-based methods (e.g., Average Threshold Confidence) often fail because OOD data can cause models to become "confidently wrong."

Neighborhood Invariance (NI)

To address these issues, we introduce Neighborhood Invariance (NI). The core intuition is that if a model has truly captured the underlying concept of a class, its prediction should remain stable across local, label-preserving transformations. For example, slightly rotating an image of a "3" or replacing a word with a synonym in a sentence should not change the model's output.

The Neighborhood Invariance pipeline for an OOD test example.

The NI Algorithm: 1. Define a Neighborhood: For a test point $x$, we define a set of transformations $G_r$ (e.g., small rotations, translations, or word replacements). 2. Sample and Predict: We generate $N$ transformed versions of the input and run them through the classifier. 3. Calculate Invariance: We identify the most frequent predicted class among the $N$ points. The NI score is the fraction of points that fall into this majority class.

Unlike previous consistency metrics, NI does not compare transformed points to the prediction of the original point. This allows the metric to capture information about the model's local complexity even if the initial prediction was incorrect.

Large-Scale Empirical Evaluation

We conducted an extensive empirical study to validate NI, involving over 4,600 models across more than 100 domain pairs. Our evaluation spanned both vision and language tasks:

  • Image Classification: Benchmarks included ImageNet and its variants (ImageNetV2, Sketch, ObjectNet, ImageNet-A/R), CIFAR-10, and MNIST.
  • Natural Language Processing: We tested sentiment analysis on Amazon reviews across 10 product categories and Natural Language Inference (NLI) using the MNLI dataset.
  • Extreme OOD: We pushed the limits by testing on highly specialized data, such as clinical notes (MedNLI) and drug reviews.

Key Findings and Insights

Our results demonstrate that NI is a powerful and robust predictor of OOD accuracy.

Comparison of decision boundaries for less invariant vs. more invariant classifiers.
  • Strong Correlation: NI shows a remarkably high correlation ($R^2$ often $> 0.8$ or $0.9$) with actual OOD accuracy.
  • Robustness to Adversarial Data: On difficult datasets like ImageNet-A, where traditional confidence-based methods fail, NI maintains its predictive power.
  • Invariance as Complexity: A model that is highly invariant in a local neighborhood effectively treats that neighborhood as a single point. This suggests a "lower complexity" representation, which correlates with better generalization.
  • Efficiency: NI is computationally efficient; ablation studies show that as few as 10 transformations per test point can provide a reliable estimate of generalization.

Conclusion

Neighborhood Invariance provides a simple, label-free, and model-agnostic way to predict how well a classifier will generalize to new domains. By focusing on local stability rather than internal model confidence, NI offers a more robust signal for performance in the face of distribution shifts. Future work may explore using NI as a per-sample signal for OOD detection, flagging specific inputs where a model's prediction is likely to be unreliable due to local instability.

Summary updated

BibTeX
@article{ng2023predicting,
  author = {Nathan Hoyen Ng and Neha Hulkund and Kyunghyun Cho and Marzyeh Ghassemi},
  title = {{Predicting Out-of-Domain Generalization with Neighborhood Invariance}},
  journal = {Accepted by TMLR},
  year = {2023},
  url = {https://openreview.net/forum?id=jYkWdJzTwn},
}
Figure