A new approach for fine-tuning sentence transformers for intent classification and out-of-scope detection tasks (2024)

Abstract: In virtual assistant (VA) systems it is important to reject or redirect user queries that fall outside the scope of the system. One of the most accurate approaches for out-of-scope (OOS) rejection is to combine it with the task of intent classification on in-scope queries, and to use methods based on the similarity of embeddings produced by transformer-based sentence encoders. Typically, such encoders are fine-tuned for the intent-classification task, using cross-entropy loss. Recent work has shown that while this produces suitable embeddings for the intent-classification task, it also tends to disperse in-scope embeddings over the full sentence embedding space. This causes the in-scope embeddings to potentially overlap with OOS embeddings, thereby making OOS rejection difficult. This is compounded when OOS data is unknown. To mitigate this issue our work proposes to regularize the cross-entropy loss with an in-scope embedding reconstruction loss learned using an auto-encoder. Our method achieves a 1-4% improvement in the area under the precision-recall curve for rejecting out-of-sample (OOS) instances, without compromising intent classification performance.

## introduction virtual assistant (va) systems often can handle only a limited scope of intents. out-of-scope (oos) rejection refers to the ability of a va to identify and reject incoming queries that are outside its scope. this is a difficult @xcite and increasingly important task in many scenarios. our work is inspired by vas in cars, which nowadays often operate in a hybrid mode where processing of certain user requests is handled locally, while others are transmitted to the cloud for response retrieval. responding to users' requests using ondevice/embedded models is cost-effective, quick, * work as a part of an internship at cerence inc. and, importantly, can safeguard sensitive information. cloud models on the other hand are typically much bigger and can respond to a wider range of queries. in such a setting, it is important that the on-device natural language understanding (nlu) models not only identify user queries for intents that are in-scope but also accurately detect out-ofscope input so that they can be either routed to the cloud or ignored. another important use case for oos rejection is the combination of a light-weight, specialized va that works tandem with large language models (llms) for free conversation with the user. similar to the in-car use case, the specialized va can be run before the llm and capture a subset of the incoming queries. this increases cost-effectiveness and controllability of the full solution, provided that it has good oos rejection capabilities. the most common approach for intent classification while rejecting oos samples is based on first generating an encoding for the sentences @xcite and then performing classification on them. in both @xcite and @xcite it was shown that the most suitable sentence encoders for this purpose are transformer-based encoders. based on the task's domain, one could use one of the several sentence encoders available in the huggingface sentence transformer library foot_0 . fine-tuning sentence encoders on the domainspecific data leads to better intent classification accuracy. this fine-tuning typically is performed by applying a softmax to the sentence embeddings. at test time, the same softmax layer could be used to perform intent classification, however, the softmax tends to produce over-confident predictions even for oos samples @xcite . hence, after fine-tuning, the softmax layer is removed from the model and other classification approaches based on embedding similarities are used for intent classification and oos rejection @xcite . this fine-tuning approach is shown to be effective in teaching the model the class-discriminative features @xcite which in our task would result in a very good intent classification accuracy. however, fine-tuning without regularization could make the model forget some of the taskagnostic knowledge about general linguistic properties, which could help oos detection @xcite . this shortcoming was tackled in @xcite by adding a regularization term based on contrastive loss. in this paper, we propose a new regularization term based on the global dispersion of in-scope sentence embeddings foot_1 . this is similar to the idea of deep one-class classification @xcite , in which the model learns to project all in-scope samples into a relatively small neighborhood in the embedding space. in our approach, this is achieved by attaching an auxiliary autoencoder head to the fine-tuning architecture which reduces the global dispersion of the in-scpoe embeddings through minimization of reconstruction error. this approach is explained in detail in section 3. ## related work there are largely two categories of approaches for detecting oos samples when performing intentclassification. the first category is based on explicitly teaching the model to distinguish between in-scope and oos samples by introducing oos samples during training. this is done by adding an extra oos class to the classifier @xcite @xcite or by adding an auxiliary loss function to the cross entropy loss to enforce the model to output a uniform probability distribution over in-scope classes when dealing with oos samples @xcite . these approaches only work if the oos test samples are drawn from a distribution similar to that of the oos training samples. in @xcite the authors prove mathematically that it is not possible to detect samples outside of known distributions unless some conditions are met. this means for robust detection of oos samples, the training oos test samples have to represent a wide variety of possible distributions. while collecting such training samples is not feasi-ble, synthesizing oos samples using models like gans @xcite and manifold learning @xcite have shown promise to make the decision boundary around in-system training samples as tight as possible. the second category consists of approaches that rely only on in-scope training data without making any assumption about the oos class. these approaches are largely based on sentence embeddings. sentence embeddings generated by transformer encoders are shown to perform better than the ones generated using traditional nlu models @xcite . the classification of sentence embeddings into inscope intent classes and into in-scope versus oos could be done using non-parametric methods such as knn @xcite or density based methods @xcite @xcite . there is a trade-off between the model footprint and its accuracy when it comes to choosing between parametric and non-parametric approaches. due to constraints on the size of the model put in the car we chose the parametric approach based on the mahalanobis distance. the sentence embeddings could be generated using pretrained sentence transformers @xcite but fine-tuning the encoder for the task at hand provides more suitable embeddings @xcite @xcite . the work in @xcite highlights that while fine-tuning based on cross-entropy loss effectively separates sentence embeddings of different intent classes, it struggles to differentiate between in-scope samples and oos samples. in that paper, this issue is tackled by adding a secondary loss function to the fine-tuning based on contrastive loss. the contrastive loss increases the distance between intent classes in the embedding space while reducing the distance between embeddings of the same intent class. however, since this loss tries to push the in-scope intent classes as far as possible from each other, the intent classes could start overlapping with oos samples in the embedding space. our approach inspired by the one-class classification in @xcite tries to reduce the dispersion of the in-scope intent classes in the embedding space by replacing the contrastive loss with reconstruction loss obtained using an autoencoder. ## methodology this section discusses the details of our modelling formalism. sub-section 3.1 talks about our training cost-function(s), whereas sub-section 3.3 talks about our inference methodology. ## experimental setup this section talks about our experimental setup. the main objectives of our experimental setup is to evaluate the capability of our proposed fine-tuning approach to improve the model's ability to detect oos queries robustly while maintaining in-scope intents classification accuracy. ## results and discussion the oos detection performance and intent classification accuracy of both the baseline and the proposed fine-tuning approaches are presented in the results show that in 3 out of 4 datasets we tested, the proposed method improved oos detection, while maintaining the same in-scope intent classification accuracy. specifically, the relative improvement with regard to auproos is seen to be 3.22% on the stackoverflow dataset, 3.45% on the mtop dataset and 1.15% on the car assistant dataset. due to its larger test set, the improvement on our internal car assistant dataset is statistically more significant than the improvement on the other two test sets. this can be attributed to the presence of a larger training set, which enables the autoencoder head to exert greater influence over the more than 100 million parameters of the sentence encoder.