Deployed real-world machine learning applications are often subject to uncontrolled and even potentially malicious inputs. Those out-of-domain inputs can lead to unpredictable outputs and sometimes catastrophic safety issues. Prior studies on out-of-domain detection require in-domain task labels and are limited to supervised classification scenarios. Our work tackles the problem of detecting out-of-domain samples with only unsupervised in-domain data. We utilize the latent representations of pre-trained transformers and propose a simple yet effective method to transform features across all layers to construct out-of-domain detectors efficiently. Two domain-specific fine-tuning approaches are further proposed to boost detection accuracy. Our empirical evaluations of related methods on two datasets validate that our method greatly improves out-of-domain detection ability in a more general scenario. 1 1 Introduction Deep neural networks, despite achieving good performance on many challenging tasks, can make overconfident predictions for completely irrelevant and out-of-domain (OOD) inputs, leading to sig-nificant AI safety issues (Hendrycks and Gimpel, 2017). Detecting out-of-domain inputs is a fundamental task for trustworthy AI applications in real-world use cases, because those applications are often subject to ill-defined queries or even potentially malicious inputs. Prior work on out-of-domain detection (e.g., Hendrycks and Gimpel, 2017; Lee et al., 2018; Liang et al., 2018; Hendrycks et al., 2019, 2020; Xu et al., 2020) mostly requires in-domain task labels, limiting its usage to supervised classification. However, deployed applica-1 Code is available at https://github.com/rivercold/ BERT-unsupervised-OOD. tions rarely receive controlled inputs and are susceptible to an ever-evolving set of user inputs that are scarcely labeled. For example, for many non-classification tasks, such as summarization or topic modeling, there are no available classifiers or task labels, which limits the practical usage of recently proposed out-of-domain detection methods. Therefore, it is natural to ask the following question: Can we detect out-of-domain samples using only unsupervised data without any in-domain labels? We regard the out-of-domain detection problem as checking whether the given test samples are drawn from the same distribution that generates the in-domain samples, which requires a weaker assumption than prior work (e.g., Lee et al., 2018; Hendrycks et al., 2020). We suppose that there are only in-domain samples, which allows us to understand the properties of data itself regardless of tasks. Therefore, methods developed for this problem are more applicable than task-specific ones and can be further adapted to tasks where no classification labels are present, such as active learning or transfer learning. To solve the problem, we utilize the latent embeddings of pre-trained transformers (e.g., Vaswani et al., 2017; Devlin et al., 2019; Liu et al., 2019) to represent the input data, which allow us to apply classical OOD detection methods such as one-class support vector machines (Schlkopf et al., 2001) or support vector data description (Tax and Duin, 2004) on them. However, the best practice on how to extract features from BERT is usually task-specific. For supervised classification, we can represent the text sequence using the hidden state of [CLS] token from the top layer. Meanwhile BERT's intermediate layers also capture rich linguistic information that may outperform the top layer for specific NLP tasks. By performing probing tasks on each layer, Jawahar et al. (2019) suggest bottom layers of BERT capture more surface features, middle layers focus more on syntax and semantic features are well represented by top ones. As no prior knowledge about OOD samples is usually provided in practice, deciding which layer of features is the most effective for OOD detection is itself non-trivial. Some OOD samples may just contain a few out-of-vocabulary words; while others are OOD due to their syntax or semantics. Based on the observations above, this paper studies how to leverage all-layer features from a pretrained transformer for OOD detection in an unsupervised manner. Our contributions are three-fold:  By analyzing all layers of (Ro)BERT(a) models, we empirically validate that it is hard to extract features from a certain layer that work well for any OOD datasets. We propose a computationally efficient way to transform all-layer features of a pre-trained transformer into a low-dimension one. We empirically validate that the proposed method outperforms baselines that use one-layer features or by simple aggregations of all layers. Assume that we have a collection of text inputs D n := { x i } ni =1 , we want to construct an out-of-domain detector that takes an unseen new input u and determines whether uuu comes from the same distribution that generates D n . We adopt a more practical setting where we have no prior knowledge of what out-of-domain inputs look like. In this case, training a domain classifier directly is not feasible. The out-of-domain detector can be described mathematically as: g ( uuu, (cid:15) ) = (cid:40) True if I ( uuu )  (cid:15) , False if I ( uuu ) > (cid:15) , where I (  ) denotes the anomaly score function, and (cid:15) is a chosen threshold to ensure that the true positive rate is at a certain level (e.g., 95% ) (Hendrycks and Gimpel, 2017; Liang et al., 2018; Lee et al., 2018). The OOD detection problem boils down to designing I (  ) such that it assigns in-domain inputs lower scores than out-of-domain inputs. There are two different scenarios, considering if we have any in-domain labels for data x i  D n . Here we define in-domain labels as any specific supervised task labels, such as sentiments, intents or topics of the text. With in-domain labels Suppose that we have multi-class label y i  [ K ] and D n = { ( xxx i , y i ) } ni =1 . Given a classifier h trained with D n , we can use maximum calibrated softmax probability with temperature scaling as the anomaly score (Liang et al., 2018; Hinton et al., 2015): I ( xxx ) :=  max i  [ K ] exp ( h i ( xxx ) /T ) (cid:80) Kj =1 exp ( h j ( xxx ) /T ) , where h i ( xxx ) is the output logits of the multi-class classifier, and T is the temperature that is selected such that the true positive rate is at a given rate (e.g., 95% in Liang et al. (2018)). This method is known as Maximum Softmax Probability (MSP), which requires multi-class labels to train a classifier and thus limits its application in practice. We argue that requiring in-domain labels is a less practical scenario for OOD detection and will not be further discussed it in this paper. Without in-domain labels The setting of no in-domain labels is our major focus. Under this as-sumptin, the models we can obtain in hand are usually not classifiers, but feature extractors instead. Then it is natural to resort to classic outlier detection methods like one-class support vector machine (Schlkopf et al., 2001), support vector data description (Tax and Duin, 2004) or kernel density estimation (KDE) for estimating the support or the density of the in-domain data distribution. When applying such methods to text data, the major focus of prior work is to design a good network structure or learning objectives (Ruff et al., 2018). Instead, in this paper we mainly focus on how to obtain good representations from pre-trained transformers and design new anomaly scores without modifying its structure, while still obtaining good OOD detection performance. BERT and its variants such as RoBERTa (e.g., Devlin et al., 2019; Liu et al., 2019) are pre-trained on large-scale public data (denoted as D pub ) using self-supervised tasks, such as language model and next sentence prediction. These models show move 100 dollars from my savings to my checking let me know how to make a vacation request Is rice ok after 3 days in the refrigerator can you tell me a joke about politicians tell the miles it will take to get to las vegasfrom san diego  Training Set D n Test Sample how are my sports teams doing  x 1 [cls] x 2 tell x 14 san x 15 diego FeedForward Self-Attention z 1 z 2 z 14 z 15    1 (  1 ,  1 ) FeedForward SelfAttention z 1 z 2 z 13 z 14   2 (  2 ,  2 ) FeedForward SelfAttention z 1 z 2 z 13 z 14   L (  L ,  L ) FeedForward SelfAttention z 1 z 2 z 13 z 14  FeedForward Self-Attention z 1 z 2 z 13 z 14   x 1 [cls] x 2 how x 6 teams x 7 doing FeedForward Self-Attention z 1 z 2 z 6 z 7  M 1 M 2 MLOC-SVM Anomaly Score  t 1 [cls] t 2 t 14 t 15 BCAD Fine-tuning IMLM Fine-tuning     Figure 1: An overview of using Mahalanobis distance features (MDF) extracted from a pre-trained transformer f to detect out-of-domain data. promising results when transferred to tasks in other domains. We aim to leverage features obtained from pre-trained transformers to construct OOD detectors in lieu of in-domain labels in D n . After pretraining, we can obtain a BERT/RoBERTa model f with L layers. We denote f (cid:96) ( x )  R d as the d -dimensional feature embeddings corresponding to the (cid:96) -th layer for input x , and f ( xxx ) is the overall representation using all layers of f . We explore the following methods to extract BERT features to construct OOD detectors. Features from the (cid:96) -th layer f (cid:96) Options to extract f l ( xxx ) include using the hidden states of [CLS] token or averaging all contextualized token embeddings at the (cid:96) -layer. Then we can directly construct an OOD detector based on features from f (cid:96) of each input xxx in D n using existing pure sample based methods, such as one-class support vector machine (OC-SVM). 2 Features from all layers Using BERT features from only one layer might not be sufficient, as prior work (Jawahar et al., 2019) has explored that different layers of BERT capture distinct linguistic properties, e.g., lower-level features capturing lexical properties, middle layers representing syntactic properties, and semantic properties surfacing in higher layers. The effects of BERT features from different layers on detecting OOD data are 2 It is also possible to use other related one-class classification methods, such as Isolation Forest. However, in practice we find OC-SVM works the best and we use it in our empirical evaluations. yet to be investigated. One straightforward way that leverages all L layers is to concatenate all layer-wise features f (cid:96) ( xxx ) , which has no information loss. However, this solution is computationally expensive and thus hard to optimize OC-SVM or kernel based methods. Another solution is to perform aggregation likes maxor mean-pooling along the feature dimension across all layers, sacrificing some information in exchange for efficiency. In this paper, we propose a simple yet effective method (described below) to use latent representations from all layers of a pre-trained transformer and can automatically decide features from which layers are important. Besides, this method is computationally efficient, only requiring us to solve a low-dimensional constrained convex optimization. Mahalanobis distance as features (MDF) for all layers Support Vector Data Description (SVDD) (Tax and Duin, 2004) is a technique related to OC-SVM where a hypersphere is used to separate the data instead of a hyperplane. However, the features provided by deep models may not be separable by hyperspheres. We focus on a generalization of the hypersphere called hyper-ellipsoid to account for such surface shapes. Suppose that we use the concatenated features from all layers ( xxx ) = [ f 1 ( xxx ) , . . . , f L ( xxx )] (cid:62)  R d  L and consider the following optimization problem to find the hyper-ellipsoid, which is similar to the optimization formula of SVDD: min R, c ,  ,  1 2 (cid:107)  (cid:107) 2 Fr + (cid:32) R 2 + 1 n (cid:88) i  i (cid:33) , s.t. (cid:107) ( x i )  c (cid:107) 2   1  R 2 +  i ,  i  0 ,  i , (1) where  is the feature map, c is the center of the hyper-ellipsoid, and  is a symmetric positive definite matrix that reflects the shape of the ellipsoid. And R reflects the volume of the hyper-ellipsoid. 3 Here we also introduce a regularization term 12 (cid:107)  (cid:107) 2 Fr to constrain the complexity of  . If  = I , then the optimization problem is identical to one-class SVDD. Solving Eq (1) exactly can be difficult, since it involves finding the optimal  of shape D  D , where D = d  L is the dimension of the features. For the concatenated features ( x ) , D can be tens of thousands or even hundreds of thousands, which makes the exact solution computationally intractable. To tackle the problem, we consider a simple and computationally efficient approximation of the solution, which can be useful in practice. First, we decompose the feature space into several subspaces, based on the features from different layers, i.e., assume  is a block diagonal matrix, and  (cid:96) reflects the shape of feature distribution at layer (cid:96) . By a straightforward calculation, we have: (cid:107) ( x )  c (cid:107) 2   1 = L (cid:88) (cid:96) =1 (cid:107) f (cid:96) ( x )  c (cid:96) (cid:107) 2   1 (cid:96) , where we decompose the center c to be the center of each layer c = [ c 1 , . . . , c L ] (cid:62) . Still, optimizing c (cid:96) and  (cid:96) can be difficult since the dimension of f (cid:96) ( x ) can be high. Based on the intuition that c (cid:96) and  (cid:96) should not deviate from the empirical mean and covariance estimation (cid:98) c (cid:96) and (cid:98)  (cid:96) from the training data, we can replace c and  (cid:96) with the following approximation: c (cid:96)  (cid:98) c (cid:96) = 1 n n (cid:88) i =1 [ f (cid:96) ( x i )] ,  (cid:96)  (cid:98)  (cid:96) w (cid:96) = 1 ( n  1) w (cid:96) n (cid:88) i =1 ( f (cid:96) ( x i )  (cid:98) c (cid:96) )( f (cid:96) ( x i )  (cid:98) c (cid:96) ) (cid:62) , where w (cid:96) is a layer-dependent constant. Now we only need to find proper { w (cid:96) } L(cid:96) =1 as well as the corresponding R and {  i } ni =1 , which is a low-dimension optimization problem that only scales linearly with the number of layer L . We further define: M (cid:96) ( x i ) = ( f (cid:96) ( x i )  (cid:98) c (cid:96) ) (cid:62) (cid:98)   1 (cid:96) ( f (cid:96) ( x i )  (cid:98) c (cid:96) ) , 3 We can further assume (cid:107)  (cid:107) = 1 , where the norm can be the operator norm or Frobenius norm, which can give the definition of the hyper-ellipsoid with unique  and R . where the square root of M (cid:96) ( xxx i ) is also referred to as the Mahalanobis distance of the features of data x i from layer (cid:96) . Assume w = [ w 1 , . . . , w L ] (cid:62)  RL and M ( x ) = [ M 1 ( x ) , . . . , ML ( x )] (cid:62)  RL , then we have: (cid:107) ( x )  c (cid:107) 2   1 = (cid:104) w , M ( x ) (cid:105) . As (cid:107)  (cid:107) 2 Fr = (cid:80) L(cid:96) =1 (cid:107) (cid:98)  l (cid:107) 2 Fr w 2 (cid:96) is not convex w.r.t w , we instead minimize  12 (cid:107) w (cid:107) 22 , which has a similar regularization effect on  (as we don't want (cid:107) w (cid:107) 2 to be small, which can make (cid:107)  (cid:107) Fr very large). So the final optimization problem to solve is: min R, w ,   1 2 (cid:107) w (cid:107) 22 + R 2 + 1 n (cid:88) i  i , s.t. (cid:104) w , M ( x i ) (cid:105)  R 2 +  i ,  i  0 ,  i , (2) which in fact is a one-class SVM with a linear kernel, with Mahalanobis distance of each layers as features (MDF), and it can be simply solved with the standard convex optimization. We illustrate our proposed algorithm in Figure 1. Remark Note that the optimization in Eq (2) is not identical as that in Eq (1), since we are using empirical sample mean { (cid:98) c (cid:96) } L(cid:96) =1 and covariance { (cid:98)  (cid:96) /w (cid:96) } L(cid:96) =1 to replace the original parameters c and  in Eq (1), which are hard to optimize when the dimension of the concatenated features ( x ) is high. Also, our approximation from Eq (1) to Eq (2) is different from the known result that when ( x ) is the infinite-dimensional feature map of the widely used Gaussian RBF kernels, OC-SVM and SVDD are equivalent and asymptotically consistent density estimators (Tsybakov et al., 1997; Vert et al., 2006). In our case, ( x ) is the concatenated features from all layers of pre-trained transformers, which makes our approximation fundamentally different from prior work. We can also fine-tune the pre-trained transformer f on the unsupervised in-domain dataset D n so that f ( xxx ) can better represent the distribution of D n . We explore two domain-specific fine-tuning approaches. In-domain masked language modeling (IMLM) Gururangan et al. (2020) find that domain-adaptive masked language modeling (Devlin et al., 2019) would improve supervised classification capability of BERT when it is transferred to that domain. Similarly, we can do MLM on D n and argue this would make the features of D n concentrate, bringing benefits to downstream OOD detection. Binary classification with auxiliary dataset (BCAD) Another way of fine-tuning the model f is to use the public dataset D pub that pretrains it. We consider the training data in D n as in-domain positive samples and data in the public dataset D pub as OOD negative samples. We add a new classification layer on top of f and update this layer together with all parameters of f by performing a binary classification task. In practice, we only need a small subset of D pub , denoted as  D pub , for fine-tuning. Since  D pub is publicly available and has no labels, we do not violate the unsupervised setting. D pub does not provide any information about the OOD samples at test time as well. Besides, the added classification layer can actually be applied for OOD detection using the MSP method, and this is exactly the setting of zero-shot classification, which we use as a baseline for comparison in our experiments. Datasets We consider two distinct datasets for experiments, where one is to regard text from unseen corpora as OOD, and the other one is to detect class-level OOD samples within the same corpus. by providing in-domain D n with the original training set of SST dataset (Socher et al., 2013) and considering samples from four other datasets (i.e., 20 Newsgroups (Lang, 1995), English-German Multi30K (Elliott et al., 2016), RTE (Dagan et al., 2005) and SNLI (Bowman et al., 2015)) as OOD data. For evaluation, we use the original test data of SST as in-domain positives and randomly pick 500 samples from each of the four datasets as OOD negatives. We do not include any sentiment labels from SST to D n for training. Cross-intent dataset (CLINIC150) This is a crowdsourced dialog dataset (Larson et al., 2019), including in-domain queries covering 150 intents and out-of-domain queries that do not fall within any of the 150 intents. We use all 15,000 queries that are originally in its training data as in-domain samples but discard their intent labels. For evaluation, we mix the 4,500 unseen in-domain test queries with 1,000 out-of-domain queries and wish to separate two sets by their anomaly scores. Examples taken from the two datasets can be found in Table 1. Note that for both datasets, only the in-domain samples are used for training, and the source/intent labels are not used in our experiments. Evaluation metrics We rank all test samples by their anomaly scores and follow Liang et al. (2018) to report four different metrics, namely, Area Under the Receiver Operating Characteristic Curve ( AUROC ), Detection Accuracy ( DTACC ), and Area under the Precision-Recall curve (AUPR) for in-domain and out-of-domain testing sentences respectively, denoted by AUIN and AUOUT . Model configurations We evaluate all methods with both BERT and RoBERTa ( base models with 768 latent dimensions and 12 layers). Choice of  D pub for BCAD We adopt the BooksCorpus (Zhu et al., 2015) and English Wikipedia, which are the sources used in common by BERT and RoBERTa for pre-training. We split paragraphs into sentences and sample  D pub to have the same size as D n for BCAD. Baselines To examine the effectiveness of our newly proposed anomaly score based on MDF that utilizes the representations of all layers, we compare it with the following baselines. (Ro)BERT(a)-Mean pooling: we construct all-layer representation by averaging all f (cid:96) ( xxx ) , which has 768 dimensions. (Ro)BERT(a)-Euclidean distance as features (EDF): we replace Mahalanobis distance with Euclidean distance and still obtain a 12-dimension vector. All of the above methods extract features as the input to OC-SVM to compute anomaly scores. BCAD + MSP: It performs zero-shot classification after BCAD fine-tuning, as discussed in Section 3. The temperature scaling is tuned to achieve the best result. This method is not applicable when no  D pub is provided. In this section, we present the results for our experiments and summarize our findings. Table 2 shows results obtained from using the [CLS] embedding or averaging token embeddings We observe that detecting cross-intent OOD samples in CLINIC150 is more challenging than that of cross-dataset OOD data in SST. This is mainly because the OOD samples in CLINIC150 are sorted by humans and the differences between intents can be subtle. We will further compare the performance of these two settings in Figure 2. The best f (cid:96) ( xxx ) for OOD is dataset-specific For the cross-corpus dataset (SST), we find that the best results come from the top layer of both (Ro)BERT(a). However, for the cross-intent dataset (CLINIC150), the middle layers perform the best when using [CLS], while the bottom layers achieve the best results with AVG. This indicates that OOD distributions are not simply based on certain types of linguistic features and the strategy of choosing f (cid:96) ( xxx ) is dataset-specific; for some dataset, semantic features play a more important role, while sometimes we need to focus on syntactic or lexical features. This validates the assumption that it is beneficial to fully utilize all layers of the hidden representations from pre-trained transformers to detect OOD instances. We find using f (cid:96) ( xxx ) of BERT is generally better SST CLINIC150 #feats AUROC DTACC AUIN AUOUT AUROC DTACC AUIN AUOUT BERT-Single layer (best) 768 92.7 85.8 93.4 91.7 64.6 60.9 88.4 26.7 RoBERTa-Single layer (best) 768 89.8 91.5 79.2 93.8 59.9 57.6 86.8 22.7 BERT + Mean-Pooling 768 81.8 76.5 77.2 82.8 62.9 59.9 87.0 27.9 BERT + Max-Pooling 768 67.2 66.1 64.2 59.4 63.0 60.0 88.0 25.8 RoBERTa + Mean-Pooling 768 91.0 92.3 80.9 94.5 57.1 56.2 85.7 20.5 RoBERTa + Max-Pooling 768 93.2 91.9 89.3 95.1 54.9 54.4 84.8 19.4 BERT + EDF 12 90.1 84.8 92.8 84.2 55.3 55.2 84.3 20.3 BERT + MDF 12 93.3 87.5 94.9 89.1 76.7 71.1 93.4 38.2 BERT + IMLM + MDF 12 93.6 88.1 97.5 89.4 77.8 72.2 93.8 39.1 BERT + BCAD + MDF 12 97.0 94.5 98.0 94.8 81.2 74.5 94.6 47.4 BERT + IMLM + BCAD + MDF 12 98.1 95.4 98.7 95.9 82.1 75.6 95.0 47.6 RoBERTa + EDF 12 99.5 95.8 99.5 99.4 56.9 56.9 86.3 19.6 RoBERTa + MDF 12 99.8 97.7 99.8 99.8 78.6 71.9 93.8 42.6 RoBERTa + IMLM + MDF 12 99.9 97.8 99.8 99.8 80.1 73.1 94.5 44.9 RoBERTa + BCAD + MDF 12 99.2 96.6 99.4 98.7 80.5 72.9 94.3 49.4 RoBERTa + IMLM + BCAD + MDF 12 99.9 98.6 99.9 99.9 84.4 76.7 95.4 59.9 TF-IDF + SVD 100 78.0 72.0 78.2 73.2 58.5 56.5 86.2 21.8 BERT + BCAD + MSP -68.5 69.0 61.5 65.4 68.3 63.5 89.7 34.1 RoBERTa + BCAD + MSP -73.7 69.3 69.0 75.3 62.1 59.6 85.9 27.8 Table 3: OOD detection performance on SST and CLINIC 150 for all models. than RoBERTa, especially with [CLS]. We guess next sentence prediction may cause this, which pretrains on [CLS] and is exclusive for BERT. In later sections, (Ro)BERT(a)-Single layer will refer to the best one in Table 2. We report the empirical results of OOD detection in Table 3 and the following observations. Pre-trained transformers produce good feature representations Methods using single-layer feature f (cid:96) outperforms frequency-based features (TF-IDF) and zero-shot classification (MSP), which validates the strong representation capability granted by self-supervised pre-training. Simple aggregations of all layers are not so effective The results of max-pooling and mean-polling are not very promising. Even though we observe an absolute 0.5% boost in SST using max-pooling, using the best single layer actually outperforms those simple aggregations in CLINIC150. MDF is more effective MDF consistently outperforms methods that directly use features f (cid:96) ( xxx ) , simple aggregations of f (cid:96) ( xxx ) , or TF-IDF features on all four metrics. In terms of AUROC, MDF outperforms the best single-layer of (Ro)BERT(a) by absolute 7.1% on SST and 14.0% on CLINIC150. MDF also performs better than EDF. Note that Euclidean distance is a special case of Mahalanobis distance when the covariance is an identity matrix. Empirically, the features generated by neural models are not invariant across all dimensions; and the comparison between MDF and EDF validates SVDD with a hyper-ellipsoid is better than a hypersphere. MDF is more efficient in training OC-SVM Notice that our approach is also more computationally efficient when obtaining optimal www and RRR since the optimization is performed on a new transformed low dimensional data space ( d = 12 is number of layers in f ). See column # feats in Table 3 for detailed comparisons. Fine-tuning techniques improve performance From Table 3, we can see both MILM and BCAD improve OOD detection performance when incorporated with MDF separately. The overall best detecting performance is achieved by MILM + BCAD + MDF, combining both proposed fine-tuning methods with MDF. We also find that RoBERTa outperforms BERT when using MDF, even though features from a single layer prefers BERT in Table 2. We plot the ROC curves of four different anomaly scores on SST in Figure 2 (a) and on CLINIC150 in Figure 2 (c), confirming that our proposed MDF and two fine-tuning techniques improve the ability in detecting OOD samples. We also present the distributions of anomaly scores I ( x ) generated by our best method in Figure 2 (b) for SST and in Figure 2 (d) for CLINIC150. For SST, the OOD detector can clearly separate I ( x ) of in-domain and out-domain samples, and the in-domain scores are densely concentrated on the low-score region. Although for CLINIC150, we do observe some OOD samples mixing with in-domain ones, accounting for the gap of metric scores between two datasets. We present some examples from CLINIC150 together with their corresponding predictions by TF-IDF, BERT-single layer and MDF methods in Table 4. TF-IDF predicts false positives for examples (b) and (d) because most of the words in the example test query are seen in the training set, like i would like you to buy me some paper plates (in-tent: order), i need to know how long to cook chicken for (intent: cooking time) and etc. BERT-single layer learns the syntax of can you tell me how to ..., which is frequently seen in the training data, but it fails to discern that the semantic meaning is out-of-domain. For example (d), all models make the mistake, potentially associating it with the intent: recipe (i need to find a good way to make chicken soup or what's the best way to make chicken stir fry). Out-of-domain detection is essentially an important component for trustworthy machine learning applications. There are two lines of work proposed to perform out-of-domain detection. One is to tackle the problem in specific multi-class classification tasks, where well-trained classifiers are utilized to design anomaly scores (e.g., Hendrycks and Gimpel, 2017; Liang et al., 2018; Lee et al., 2018; Card et al., 2019; Hendrycks et al., 2020; Xu et al., 2020), Those methods can only be useful when multi-class labels are available, which limits their application in more general domains. Our proposed work goes beyond this limitation and can utilize large amounts of unsupervised data. Another line of work is based on support estimation or density estimation, which assumes that the in-domain data is in specific support or from the high density region (Schlkopf et al., 2001; Tax and Duin, 2004). In principle, our work is closely related to this line of work. Besides, Zhai et al. (2016); Ruff et al. (2018); Zong et al. (2018) also leverage the features of neural networks, though these methods require designing specific network structures for different data. Our work circumvents the issues of prior work by designing a computationally efficient method that leverages the powerful representations of pre-trained transformers. Finally, the fine-tuning techniques we use to improve the representation of data are closely related to unsupervised pre-training for transformers (De-vlin et al., 2019; Yang et al., 2019), and recently proposed contrastive learning (e.g., He et al., 2020; Chen et al., 2020). Lately, Gururangan et al. (2020) discover that performing pre-training (MLM) on the target domain with unlabeled data can also help to improve downstream classification performance. To the best of our knowledge, our method is the first to incorporate transformers and pre-training techniques to improve out-of-domain detection. We study the problem of detecting out-of-domain samples with unsupervised in-domain data, which is a more general setting for out-of-domain detection. We propose a simple yet effective method using Mahalanobis distance as features, which significantly improves the detection ability and reduces computational cost in learning the detector. Two domain-adaptive fine-tuning techniques are further explored to boost the detection performance. In the future, we are interested in deploying our OOD method to real-world applications, such as detecting unseen new classes for incremental few-shot learning (Zhang et al., 2020; Xia et al., 2021) or filtering OOD samples in data augmentations. We would like to thank the anonymous reviewers for their valuable feedback and comments.
abstain abstain abstain method objective abstain result abstain abstain abstain other abstain abstain method method abstain abstain other abstain method abstain abstain abstain abstain abstain objective method objective objective method method abstain other abstain method method method method abstain method method method abstain abstain method abstain other abstain abstain abstain method abstain abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain method other abstain abstain abstain abstain abstain method abstain other abstain method abstain method abstain method other other abstain method abstain method abstain abstain abstain abstain abstain method abstain abstain abstain abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain result abstain abstain abstain abstain abstain abstain result abstain abstain result abstain abstain abstain abstain result abstain abstain abstain abstain result abstain abstain result abstain abstain abstain abstain abstain abstain abstain abstain result abstain result abstain abstain abstain abstain abstain abstain abstain result abstain abstain abstain abstain abstain abstain abstain other other other objective other abstain other method abstain other objective method objective abstain abstain other