Out-of-Distribution Detection through Soft Clustering with Non-Negative Kernel Regression (2024)

Abstract: As language models become more general purpose, increased attention needs to be paid to detecting out-of-distribution (OOD) instances, i.e., those not belonging to any of the distributions seen during training. Existing methods for detecting OOD data are computationally complex and storage-intensive. We propose a novel soft clustering approach for OOD detection based on non-negative kernel regression. Our approach greatly reduces computational and space complexities (up to 11\times improvement in inference time and 87% reduction in storage requirements). It outperforms existing approaches by up to 4 AUROC points on four benchmarks. We also introduce an entropy-constrained version of our algorithm, leading to further reductions in storage requirements (up to 97% lower than comparable approaches) while retaining competitive performance. Our soft clustering approach for OOD detection highlights its potential for detecting tail-end phenomena in extreme-scale data settings. Our source code is available on Github.

## introduction despite the successes of generalized models of natural language, the challenge of generalization to out-of-distribution (ood) data-data that differs from the training data distribution-remains @xcite . this can be a limiting obstacle in known, sensitive domains like medicine and finance @xcite , or even in "domains" which are unknown or imperceptible to humans @xcite . ood shifts are also important in detecting long tail phenomena @xcite , which are critical to ensure robust and reliable application of modern language models. @xcite . we show how distance-based methods (e.g. knn, top) may incorrectly classify ood instances as id if they are close to an outlier in the id training set. similarly, hard clustering algorithms (e.g., kmeans) may incorrectly classify id data as ood if it lies at the edge of a cluster. the use of soft-clustering in our method overcomes these limitations and better captures the underlying data geometry, enabling more accurate ood detection. while ood detection has been extensively studied ( §2), most approaches have limitations preventing them from being applied broadly. existing distance-based approaches for ood detection @xcite @xcite are often not scalable as they rely on storing the entire in-distribution (id) training set. this is particularly challenging given the size of training data for llms. approaches that improve scalability make strong assumptions about the distribution of data (e.g., the id data does not have small clusters @xcite ) or are applicable only when the data is labeled @xcite . while requiring lower storage and computation, classification-based approaches for ood detection are typically limited to cases where labeled data is available @xcite . moreover, they perform worse than distance-based approaches @xcite . in this work, we present a clustering approach for ood detection that (i) makes no assumptions about the underlying data distribution, (ii) applies to both labeled and unlabeled data, (iii) is scalable, and (iv) is compute and storage efficient. our ood detection method builds on a dictionary-based approach that leverages a non-negative kernel regression (nnk)-based soft clustering technique called nnk-means @xcite (see figure 1 ). compared to hard clustering, soft clustering, i.e., associating each sample with multiple cluster centers in the data manifold, leads to a better approximation of the id data and, consequently, improved ood detection. it also requires fewer clusters and is, therefore, more storage-efficient. to the best of our knowledge, we are the first to leverage soft clustering for text ood detection. we are also the first to extend the usage of nnk-means to the text domain-introducing two novelties to do so. first, we use the approximation error as a criterion for ood detection. additionally, to avoid dependence on the number of cluster centers-the critical limitation in most clustering algorithms-we introduce a new, improved formulation of nnk-means, proposing an entropy-constrained data-driven selection process. this entropy-constrained nnk-means (ec-nnk-means) also significantly reduces memory usage and inference time while maintaining comparable ood detection performance. we empirically validate the performance of nnk-means for ood detection on 4 benchmark datasets. we show that it consistently achieves superior or comparable performance relative to stateof-the-art approaches @xcite while requiring over an order of magnitude lower storage and inference time. we also find that our approach is applicable across a variety of settings, effectively leveraging id labels when they are present but providing competitive performance when they are not and maintaining high performance when using different types of embeddings. overall, we find that our soft-clusteringbased approach yields state-of-the-art ood detection performance while improving memory and computational efficiency-particularly when using our improved formulation with entropy constraints. ## related work ood detection methods in nlp broadly fall into two categories: (i) post-hoc methods that detect ood instances after deriving their representations from pre-trained language models (plms) and (ii) works focused on learning representations that improve ood detection. post-hoc ood detection these methods are typically applied to the dataset representations, which can either be pre-trained representations obtained directly from plms or fine-tuned representations obtained after fine-tuning the plms for a particular task. post-hoc methods can be further divided into two categories. first, distancebased methods compute the minimum distance to new data from id training data as the ood score. for example, @xcite computes the class-wise mahalanobis distance between class centroids and a query point to obtain an ood score. @xcite proposed gaussian discriminant analysis (gda), which leverages euclidean and mahalanobis distances with generative classifiers to identify ood instances. @xcite directly uses the distance to the kth nearest neighbor (knn). however, these approaches require storing the entire id training set, significantly increasing memory requirements. alternatively, based on the intuition that a classifier output distribution tends to reflect training distribution, classifier-based methods leverage the output logits to get a confidence score for ood detection. the most frequently used and simple such method uses the maximum softmax probability (msp) of the classifier as confidence, as introduced by @xcite and later improved by odin @xcite by adding temperature scaling and input preprocessing. to tackle the over-confidence problem of msp, @xcite introduces energy, an energy-based scoring function to better detect ood data. @xcite instead proposes distance-to-uniform (d2u) to find the ood data whose output distribution is closer to a uniform distribution. ## nnk-means and variants we briefly present the background on soft clustering via nnk-means @xcite for modeling a data distribution ( §3.1). next, we present our extension of the method via the introduction of an entropy constraint ( §3.2). ## nnk-means for ood detection in this section, we formally formulate the ood detection problem ( §4.1) and describe how to use nnk-means for ood detection ( §4.2). ## ood detection results and analysis ## conclusion we address the problem of ood detection using nnk-means, a soft-clustering algorithm. nnk-means achieves state-of-the-art performance across 4 benchmark datasets, while requiring lower storage and improving computational efficiency relative to previous approaches that perform comparably. we introduce ec-nnk-means, an extension of nnk-means, and show that it can lead to further improvements in efficiency while matching or improving ood detection performance. our methods provide competitive performance regardless of the availability of labels or the type of embeddings used, and yield intuitive clustering of input data. future work will explore applying our algorithms to analyze large pre-training datasets.