FL at S : Principled Out-of-Distribution Detection with Feature-Based Likelihood Ratio Score (2023)

Abstract: Detecting out-of-distribution (OOD) instances is crucial for NLP models in practical applications. Although numerous OOD detection methods exist, most of them are empirical. Backed by theoretical analysis, this paper advocates for the measurement of the “OOD-ness” of a test case \boldsymbol{x} through the likelihood ratio between out-distribution \mathcal P_{\textit{out}} and in-distribution \mathcal P_{\textit{in}} . We argue that the state-of-the-art (SOTA) feature-based OOD detection methods, such as Maha and KNN, are suboptimal since they only estimate in-distribution density p_{\textit{in}}(\boldsymbol{x}) . To address this issue, we propose FLATS , a principled solution for OOD detection based on likelihood ratio. Moreover, we demonstrate that FLATS can serve as a general framework capable of enhancing other OOD detection methods by incorporating out-distribution density p_{\textit{out}}(\boldsymbol{x}) estimation. Experiments show that FLATS establishes a new SOTA on popular benchmarks.

## introduction natural language processing systems deployed in real-world scenarios frequently encounter out-ofdistribution (ood) instances that fall outside the training corpus distribution. for instance, it is hard to cover all potential user intents during the training of a task-oriented dialogue model. therefore, it becomes crucial for practical systems to detect these ood intents or classes during the testing phase. the ability to detect ood instances enables appropriate future handling, including additional labeling and utilization for system updates, ensuring the system's continued improvement @xcite . a rich line of work has been proposed to tackle ood detection. among them, the best-performing methods exploit the information of feature / hidden @xcite . the model extracts the feature of input text, and then outputs the ood score s flats (x) that takes the form of likelihood ratio between out-distribution p out and in-distribution p in . representation h(x; θ) of test case x encoded by the tested nlp model. for example, maha @xcite estimates the mahalanobis distance between h(x; θ) to the in-distribution (ind), while knn @xcite estimates the distance to the k-nearest ind neighbor. these techniques have demonstrated remarkable performance in recent benchmark studies @xcite . however, these methods were proposed without principled guidance. to address this, our paper first formulates ood detection as a binary hypothesis test problem and derives that the principled solution towards ood detection is to estimate the likelihood ratio p out (x)/p in (x). under this framework, we show that maha and knn only estimates ind density p in (x) and assumes ood distribution p out to be uniform distribution, which is sub-optimal. this paper then proposes a principled solution for ood detection with feature-based likelihood ratio score, namely flats. in flats, the ind density p in (x) is also estimated with knn on the training corpus, while the ood density p out (x) is estimated with knn on ood data. though we are not access to the real ood data, we leverage public corpus (e.g., wiki, bookcorpus) as auxiliary ood data. apart from knn, we further demonstrate that the idea of flats to incorporate ood distribution information is applicable to other ood detection techniques. experiments demonstrate the effectiveness of the proposed flats. ## background this paper focuses on supervised multi-class classification, a widely studied setting in ood detection. the formal definition is given as follows: definition 1 (ood detection) given an input space x ⊂ r d and a label space @xmath0, ..., k}, given a training set @xmath1, y j )} n @xmath2.i.d. from p x y , ood detection aims to decide whether a test case @xmath3. ood detection has been studied extensively. for example, using the maximum softmax probability (msp) @xcite to measure ind-ness is popular in literature. there are more advanced methods like maximum logit @xcite and energy score @xcite . among the existing ood detection methods, distance-based mahalanobis (maha) score and knearest neighbor (knn) score achieve remarkable performance on common ood detection benchmarks. these methods first extract latent feature @xmath4; θ) of test case x with the pre-trained language model θ. for maha and knn, the ood-ness of x are measured by the two scores foot_0 : in equation ( 1 ), µ c is the class centroid for class c and σ is the global covariance matrix, which are estimated on ind training corpus d. in equation (2), || • || 2 is euclidean norm, z * = z/||z|| 2 denotes the normalized feature z, and d * denotes the set of normalized features from training set d. knn(z * ; d * ) denotes the k-nearest neighbor of z * in set d * . more details are given in appendix b. ## results and analysis flats establishes a new sota. as shown in flats enhances other baselines. figure 2 shows the fpr@95 results on clinc150 under ablation setting 1. we observe that all the baselines achieve lower fpr@95 results by incorporating ood density estimation. therefore, flats is not only a single method, but can serve as a general framework to improve other sota ood methods. ## discussion in the derivation of our flats framework, we exploit the energy-based models (ebms) for parameterization. ebms are known for their flexibility with sacrifice to their tractability. but in our case, we leverage their flexibility to derive principled ood scores (following theorem 1) while keep the tractability via approximation with traditional ood scores (e.g., knn) in real-world applications. the detailed explanation is shown as follows. flexibility: since theorem 1 suggests that we should design ood scores under the form of likelihood ratio between p out and p in , we adopt ebms to model the two probability distributions p out and p in due to the flexibility of ebms. thanks to ebms, we transform the computation of likelihood-ratio into two unnormalized energy functions e out (x) and e in (x) as shown in section 3.2. tractability: contrary to the traditional works that directly optimize ebms via mcmc @xcite which may face the problem of computational inefficiency, we approximate the energy functions using traditional feature-based ood scores (knn or maha). the efficiency of knn in real-world applications has been proved in previous works @xcite . therefore, our method flats that adopts knn is scalable and efficient in realworld applications. also, though we use public corpus for the estimation of p out in the experiments, flats is compatible with any desired ood data when they are available. as flats does not require model re-training, it has great potential in test-time adaptation to tackle distribution shifting in real-world scenarios. ## conclusion this paper proposes to solve ood detection with feature-based likelihood ratio score, which is principled (justified by theorem 1). the proposed flats is simple and effective, which not only establishes a new sota, but can serve as a general framework to improve other ood detection methods.