NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

kFolden: k-Fold Ensemble for Out-Of-Distribution Detection

Xiaoya Li, Jiwei Li, Xiaofei Sun, Chun Fan, Tianwei Zhang, Fei Wu, Yuxian Meng, Jun Zhang

Abstract

Out-of-Distribution (OOD) detection is an important problem in natural language processing (NLP). In this work, we propose a simple yet effective framework $k$Folden, which mimics the behaviors of OOD detection during training without the use of any external data. For a task with $k$ training labels, $k$Folden induces $k$ sub-models, each of which is trained on a subset with $k-1$ categories with the left category masked unknown to the sub-model. Exposing an unknown label to the sub-model during training, the model is encouraged to learn to equally attribute the probability to the seen $k-1$ labels for the unknown label, enabling this framework to simultaneously resolve in- and out-distribution examples in a natural way via OOD simulations. Taking text classification as an archetype, we develop benchmarks for OOD detection using existing text classification datasets. By conducting comprehensive comparisons and analyses on the developed benchmarks, we demonstrate the superiority of $k$Folden against current methods in terms of improving OOD detection performances while maintaining improved in-domain classification accuracy.

Introduction

Deep learning models are notoriously overconfident. Even when presented with an input that belongs to a category they have never seen (Out-of-Distribution), they often assign it a high probability for one of their known classes. This is a major hurdle for building "trustworthy" AI in fields like medical diagnosis or automated customer support.

The standard way to fix this in Natural Language Processing (NLP) is to use external data during training to represent "the rest of the world." However, this approach has two significant flaws:

  1. Data Collection Burden: Finding and cleaning massive amounts of extra data is resource-intensive.
  2. Selection Bias: The performance of an OOD detector depends heavily on the choice of external data. If the "outlier" data does not resemble real-world OOD examples, the detector fails.

The $k$Folden framework addresses these issues by teaching a model to recognize what it doesn't know using only the data already available.

Methods

The core idea of $k$Folden is to mimic OOD behavior during the training phase through a $k$-fold ensemble strategy.

The $k$-Fold Ensemble

For a classification task with $k$ labels, the framework creates $k$ separate sub-models. Each sub-model $f_i$ is trained on a subset of $k-1$ labels, while the $i$-th label is "masked" and treated as an unknown OOD category.

The Training Objective

Each sub-model is trained with a dual-purpose loss function:

  1. Cross-Entropy Loss: For the $k-1$ visible labels, the model learns to predict the correct class with high confidence.
  2. KL Divergence Loss: For the "masked" label, the model is explicitly taught to be uncertain. The loss encourages the model to produce a uniform distribution (equal probability for all seen classes) when it encounters the label it was not supposed to see.

Inference

During testing, an input is passed through all $k$ sub-models. Their probability distributions are averaged to produce a final estimate. For In-Distribution (ID) inputs, the ensemble typically produces a "peaky" distribution. For OOD inputs, the ensemble produces a high-entropy, "flat" distribution, facilitating easy detection.

Benchmarks and Datasets

Existing NLP OOD benchmarks are often insufficient. To evaluate $k$Folden, a comprehensive suite of 7 datasets was developed, categorized by the type of distribution shift:

  • Non-Semantic Shift (NSS): OOD examples come from different sub-categories of the same broad topic (e.g., training on "real cars" and testing on "toy cars").
  • Semantic Shift (SS): OOD examples come from entirely different subject areas (e.g., training on "sports" and testing on "politics").
Statistics of the developed NSS and SS datasets used for benchmarking.

Experimental Results

The $k$Folden framework was tested across various architectures, including CNNs, BiLSTMs, BERT, and RoBERTa.

Comprehensive results of $k$Folden across NSS and SS benchmarks, showing improvements in AUROC and TNR.

Superior OOD Detection

On the Yahoo-AGNews-five dataset, a RoBERTa model using $k$Folden achieved an AUROC of 85.61, compared to 84.98 for the vanilla version. When combined with Mahalanobis distance, performance reached 86.92. Improvements were particularly dramatic in Semantic Shift scenarios; for example, on AGNews-FM, $k$Folden improved the True Negative Rate (TNR@95TPR) from 60.36% to 63.42%.

Accuracy and Consistency

Importantly, $k$Folden maintains or slightly improves In-Domain (ID) accuracy, avoiding the common trade-off between safety and performance. Across almost all benchmarks and metrics (AUROC, AUPR, TNR), the $k$Folden ensemble consistently outperformed standard models and other popular OOD techniques like Monte Carlo Dropout.

Conclusion

$k$Folden provides a simple yet powerful training paradigm for OOD detection that eliminates the need for external data. By forcing the model to learn the concept of an unknown category through internal simulation, it achieves state-of-the-art results while maintaining high classification accuracy. While training $k$ models introduces computational overhead, future work may focus on achieving this "leave-one-out" effect within a single model architecture.

Summary updated

BibTeX
@inproceedings{li2021kfolden,
  author = {Xiaoya Li and Jiwei Li and Xiaofei Sun and Chun Fan and Tianwei Zhang and Fei Wu and Yuxian Meng and Jun Zhang},
  title = {{kFolden: k-Fold Ensemble for Out-Of-Distribution Detection}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.248},
}
Figure