Automatically Identifying Words That Can Serve as Labels for Few-Shot Text Classification
Timo Schick, Helmut Schmid, Hinrich Schütze
Introduction
Pretraining language models on large corpora has led to improvements on a wide range of NLP tasks [Radford et al. (2018, Devlin et al. (2019, Liu et al. (2019, inter alia], but learning to solve tasks from only a few examples remains a challenging problem. As small datasets are common for many real-world applications of NLP, solving this challenge is crucial to enable broad applicability. A promising direction for many tasks is to reformulate them (e.g., by appending an instruction such as “translate into French”) so that they can directly be solved by a pretrained language model [Radford et al. (2019, Schick and Schütze (2020a, Brown et al. (2020]. The key idea of Pet [Schick and Schütze (2020a], one such approach aimed at text classification, is to rephrase each input as a cloze question for which the language model’s prediction can somehow be mapped to a label; an example is illustrated in Figure 1. While Pet achieves remarkable results with little or no labeled training data, manually defining the required mapping between a language model’s predictions and labels is difficult as it requires both task-specific knowledge and an understanding of the language model’s inner workings to identify words that it understands sufficiently well.
In this work, we show how this mapping can be obtained automatically, removing the need for expert knowledge: We introduce Pet with Automatic Labels (Petal), a simple approach for identifying words that can serve as proxies for labels given small amounts of training data. At its core, our approach breaks the intractable problem of finding the mapping that maximizes the likelihood of the training data into several manageable subproblems. Integrating our approach into Pet significantly outperforms regular supervised training and almost matches the performance of Pet with a manually defined mapping.
Related Work
Reformulating problems as language modeling tasks has been explored in fully unsupervised settings [Radford et al. (2019, Puri and Catanzaro (2019, Davison et al. (2019], in few-shot scenarios with limited amounts of training data [Opitz (2019, Shwartz et al. (2020, Brown et al. (2020], and even in high-resource settings [Raffel et al. (2019]. The same idea is also commonly used for probing the knowledge contained within pretrained language models [Petroni et al. (2019, Talmor et al. (2019, Schick and Schütze (2020b, Ettinger (2020, inter alia].
Our method is a direct extension of Pet [Schick and Schütze (2020a] and is similar in spirit to automatic verbalizer search (AVS) introduced therein. AVS is another method for automatically finding a mapping from labels to words that works as follows: First, the mapping is initialized by assigning a random word to each label and then, the mapping is improved over multiple iterations by successively replacing words with better alternatives given the current mapping in a greedy fashion. In contrast, our approach offers a closed-form solution that is conceptually simpler and faster, requires fewer hyperparameters – which can be crucial in a data-scarce scenario – and performs much better, especially for difficult tasks.
For Pet, expert knowledge is mostly encoded in the mapping from a language model’s prediction to labels, which is why we focus on automating this part. The complementary problem of automatically transforming inputs before processing them with a language model has been studied by ?). This is also closely related to approaches for extracting patterns in relation extraction [Brin (1999, Agichtein and Gravano (2000, Batista et al. (2015, Bouraoui et al. (2020].
Pattern-Exploiting Training
We review Pattern-Exploiting Training (Pet) as proposed by ?). Let be a pretrained masked language model (MLM), its vocabulary and the mask token. We consider the task of mapping textual inputs to some label where we assume w.l.o.g. that for some . In addition to training data , Pet requires a set of pattern-verbalizer pairs (PVPs). As exemplified in Figure 1, each PVP consists of
a pattern that is used to convert inputs to cloze questions. Formally, is defined as a function that maps each input to a sequence of tokens containing exactly one [MASK] token;
a verbalizer that maps each label to a single token representing its meaning. For Pet to work, the verbalizer must be chosen so that for each input , is a suitable replacement for the mask token in if and only if is the correct label for . We call the verbalization of and abbreviate it as .
Based on this intuition, ?) define the conditional probability distribution of given as
where denotes the raw score that assigns to at the masked position in ; that is, the probability of being the correct label for is derived from the probability of its verbalization being the “correct” token at the masked position in .
American Duo Wins Opening Beach Volleyball MatchNews:[MASK]213WorldBusinessSports Figure 1: Exemplary application of a pattern-verbalizer pair : An input is converted into a cloze question by applying . The probability of each label is derived from the probability of its verbalization being a plausible choice for the masked position. Pet basically works in three steps:
For each PVP , a separate MLM is finetuned on , using the cross entropy between the true labels and as loss function.
The resulting ensemble of finetuned MLMs is used to annotate a large set of unlabeled examples with soft labels.
Another pretrained language model with a sequence classification head is finetuned on the resulting soft-labeled dataset; this model serves as the final classifier for the task considered.
There are several additional details to Pet (e.g., an additional language modeling objective to prevent catastrophic forgetting); we skip these details as they are not relevant to our approach. For a more thorough explanation, we refer to ?).
Likelihood Ratio Verbalizer Search
Manually defining the verbalizer required for Pet can be challenging: It requires knowledge not only of a task’s labels and how they can best be expressed in natural language using a single word, but also of the used MLM’s capabilities as it is crucial to choose only such words as verbalizations that are understood sufficiently well by the language model and correspond to a single token in its vocabulary. We thus aim to automatically find a good verbalizer for some pattern without requiring task- or model-specific knowledge.
Our method requires sets of verbalization candidates for each label ; for now, we simply assume for all . Let be the set of all verbalizers consistent with these candidate sets, i.e., if and only if for all . A natural criterion for measuring the suitability of a verbalizer is to compute the likelihood of the training data given , leading to the maximum likelihood estimate
Unfortunately, iterating over to find the best verbalizer is intractable: the number of possible verbalizers grows exponentially in the number of labels and for a typical MLM, contains tens of thousands of tokens.
To circumvent this problem, we reframe the -class classification task as one-vs-rest classifications: For each , we search for a verbalization that enables to distinguish examples with label from examples with any other label. To this end, we introduce binarized training sets where if and otherwise. For , we define
analogous to Eq. 1 except that we consider all tokens for normalization, and . This enables us to formulate (and compute) the maximum likelihood estimate for each verbalization independently as
However, this reframing creates a label imbalance: If is balanced, each contains times as many negative examples as positive ones. To compensate for this, we raise each to the power of
where is the number of examples in with label . A similar fix for this imbalance problem was suggested by ?) for multi-class classification with support vector machines.
We next reformulate maximizing the likelihood as minimizing the cross entropy between and , that is, where
This can easily be derived from Eq. 4 after compensating for the label imbalance as described above. Unfortunately, there is the following problem with Eq. 6: As the vocabulary is quite large for most pretrained MLMs, will almost always be close to and thus, . This means that negative examples contribute almost nothing to this cross entropy loss, so optimizing for results in verbalizations that are overall highly likely, but do not necessarily reflect the meaning of . We fix this problem by considering not the absolute values of , but the likelihood ratio (LR):
Independently, this LR criterion was recently shown to compare favorably to cross entropy in gradient-based neural network training for image classification [Yao et al. (2020].
To arrive at , we have made quite a number of modifications to our starting point, the intractable maximum likelihood estimate. However, the two objectives are in fact quite similar. The key difference is that Eq. 2 enforces a large distance between and the maximum score assigned to the verbalizations of other labels, whereas Eq. 7 enforces a large distance between and the average score assigned to the verbalizations of other labels; this is shown in Appendix A.
Our above formulation requires sets of verbalization candidates for each . These candidate sets can trivially be obtained by setting , but to facilitate verbalizer search, we create candidate sets containing only a small subset of the vocabulary. First, we follow ?) and reduce by removing all tokens that do not correspond to real words or do not contain at least 2 alphabetic characters. From the remaining list, we collect the 10,000 tokens that occur most frequently in the task’s unlabeled data and denote this filtered vocabulary by .
As our loss formulation in Eq. 7 considers the likelihood ratio, it is indifferent to the overall likelihood of a token. To make sure that candidates are both syntactically and semantically plausible for a given pattern, we further restrict the set of candidates by keeping only tokens that maximize the likelihood of all positive examples: For each label , we define a candidate set that contains the 1000 tokens that maximize where . Naturally, this induces a bias towards frequent words. As recently shown by ?), pretrained language models tend to understand frequent words much better than rare words, so all other things being equal, a frequent word should be preferred over a rare word as verbalization; that is, this bias towards frequent words is indeed desirable.
2 Multi-Verbalizers
For some tasks, it makes sense to assign multiple verbalizations to some label.222For example, one of the categories in the AG’s News classification dataset [Zhang et al. (2015] is “Science/Tech” which can best be modeled by using two verbalizations “Science” and “Tech”. This applies all the more if the verbalizations are found automatically, as it may easily occur that the most likely verbalizations for a given label cover different aspects thereof. We thus introduce the concept of multi-verbalizers, a generalization of verbalizers to functions where denotes the power set of . To integrate multi-verbalizers into Pet, we replace the conditional probability distribution in Eq. 1 with
That is, we substitute the raw score that assigns to a label’s verbalization in standard PET with the average score across all its verbalizations.
Experiments
For our experiments with Petal, we use the Pet implementation of ?) and follow their experimental setup. In particular, we use RoBERTa-large [Liu et al. (2019] as underlying MLM, we use the same set of hyperparameters for Pet, the same evaluation tasks with the same patterns, and the same strategy for downsampling training sets. We deviate from ?) in that we convert all inputs to single sequences (i.e., we remove all [SEP] tokens) as we found this to slightly improve the verbalizers found by our approach in preliminary experiments. To ensure that our results are comparable with previous work and improvements in Pet’s performance are not simply due to this modification of patterns, we do so only for finding verbalizers and not for actual Pet training and inference.
We first analyze the verbalizers found by our method qualitatively. To this end, we consider Yahoo Questions [Zhang et al. (2015], a dataset consisting of questions and answers that have to be categorized into one of ten possible categories such as “Health”, “Sports” and “Politics”. We use the simple pattern
and 50 training examples, meaning that we provide just five examples per label. Table 1 shows the most likely verbalizations obtained for all labels using and ; for the latter, we consider both an unrestricted set of verbalization candidates and the candidate sets defined in Section 4. As can be seen, does not lead to useful verbalizers for the reason outlined in Section 4: it only identifies words that are overall highly likely substitutes for the [MASK] in . While with finds reasonable verbalizers, some verbalizations are rather uncommon tokens (“PLoS”, “phylogen”, “gcc”); using more restrained candidate sets () mitigates this issue and finds words that, in most instances, correspond well to the task’s actual labels. The shown verbalizations also illustrate the benefit of using multi-verbalizers. For example, the verbalizations for “Computer” include “hardware” and “software”; in isolation, none of these terms fully covers this category, but their combination does cover most of its aspects.
Next, we consider the more challenging MNLI dataset [Williams et al. (2018], a natural language inference dataset where given two sentences and , the task is to decide whether both sentences contradict each other, one sentence entails the other, or neither. On this dataset, Table 2 compares Petal to AVS, the approach of ?) for automatically finding verbalizers, using the pattern
and 50 labeled training examples. While both approaches clearly fail to find good verbalizations for the label “Neutral”, using Petal results in much better verbalizations for the other two labels, with most of the words identified by AVS being entirely unrelated to the considered labels.
To evaluate our approach quantitatively, we use the Yelp Review Full Star (Yelp) and AG’s News (AG’s) datasets [Zhang et al. (2015] in addition to Yahoo Questions and MNLI. The task for Yelp is to guess the number of stars (ranging from 1 to 5) that a customer gave to a restaurant based on their textual review; for AG’s, one of the four categories “World”, “Business”, “Sports” and “Science/Tech” has to be assigned to a news article.
Following ?), we again consider a scenario where we have labeled training examples and a set of unlabeled examples for each task; the unlabeled examples are only required for Pet and not used for finding a verbalizer. For our approach, we consider both a variant where verbalizers are computed for each pattern separately (sep), and a variant were a single verbalizer is computed for all patterns as in AVS (joint); for the latter, the likelihood ratio losses for all patterns are simply added up and minimized jointly. We use a multi-verbalizer where are the most likely verbalizations per label and compare Petal to the following baselines:
supervised: Regular supervised learning without Pet, i.e., we add a regular sequence classification head on top of the pretrained language model and perform finetuning as in ?).
Pet + random: We generate a multi-verbalizer by randomly choosing 10 words per label uniformly from . We include this baseline to verify that any improvements over supervised learning are not simply due to Pet using additional unlabeled examples and auxiliary objectives, but that the actual source of improvement is the improved verbalizer.
Pet + AVS: We generate a multi-verbalizer with 10 labels per word using automatic verbalizer search with its default parameters.
Pet + manual: We consider the manually defined verbalizers of ?). This serves as an upper bound of what is achievable by incorporating task- and model-specific knowledge.
Results can be seen in Table 3. On average, Pet with random verbalizers performs slightly better than regular supervised learning; we surmise that this is due to Pet leveraging additional unlabeled data. Random verbalizers perform much worse than AVS which, in turn, is cleary outperformed by our method for 3 out of 4 tasks, with an especially large margin on MNLI. This holds true for both the joint and sep variant of Petal, with the latter performing slightly better on average. Furthermore, especially for MNLI, our approach almost matches the performance of Pet with manually defined mappings while requiring no task-specific knowledge for finding verbalizers. The large gap between supervised learning and Petal is especially surprising given that the patterns – the only other source of task-specific knowledge in Pet – are very generic in nature.
We finally note that our method adds a single hyperparameter to Pet: the number of verbalizations per label , which may be difficult to optimize for small training sets. However, as shown in Figure 2, results on all tasks are relatively stable for a wide range of values ranging from to ; the best result across all tasks is obtained for .
Conclusion
We have devised Petal, a simple approach that enriches Pet with the ability to automatically map labels to words. Qualitative and quantitative analysis shows that our approach is able to identify words that are suitable to represent labels with as little as 50 examples and almost matches the performance of hand-crafted mappings for some tasks. For future work, it would be interesting to see whether the patterns required by Pet can similarly be obtained in an automated fashion.
Acknowledgements
This work was supported by the European Research Council (grant #740516).
References
Appendix A Relation of Maximum Likelihood Estimate and One-Vs-Rest Likelihood Ratio
We analyze the impact of all modifications introduced in Section 4: reframing -class classification as one-vs-rest classifications, downsampling negative examples and replacing with . For the sake of conciseness, we drop the condition on and in and , respectively. We start by reformulating the maximum likelihood estimate in Eq. 2 as
through logarithmization and multiplication by . By applying the definition of , we obtain
Finally, we can derive from the tangent line approximation that the left part of each addend is a soft approximation of (also commonly referred to as LogSumExp), so we can approximate as
We now consider the verbalizer obtained using as in Eq. 7, for which we assume that is a balanced dataset. That is, for each label , there are examples with label in . We abbreviate the set of all labels except as .
As for each verbalization is independent of all verbalizations for other labels, we can simply write the optimization criterion for as the sum of likelihood ratio losses for all verbalizations:
As can be seen in the definition of , each contributes to the above sum times: times as negative example for each , and once as a positive example . We can thus rewrite the above as
⋅𝑠1subscript𝑞𝑃subscript𝑣𝑦1subscript𝑞𝑃subscript𝑣𝑦0subscriptsuperscript𝑦′subscript𝑌𝑦⋅𝑠0subscript𝑞𝑃subscript𝑣superscript𝑦′0subscript𝑞𝑃subscript𝑣superscript𝑦′1\displaystyle=\operatorname*{arg\,min}_{v\in\mathcal{V}}-\smashoperator[]{\sum_{(\mathbf{x},y)\in\mathcal{T}}^{}}\ \ \left(s(1)\cdot\log\frac{q_{(P,v_{y})}(1)}{q_{(P,v_{y})}(0)}+\sum_{y^{\prime}\in Y_{\setminus y}}s(0)\cdot\log\frac{q_{(P,v_{y^{\prime}})}(0)}{q_{(P,v_{y^{\prime}})}(1)}\right) (15) and again use the fact that for all as well as the definition of and to obtain: (16) (17) Using and the fact that is independent of , we can further simplify:
This concludes our verification of the statement made in Section 4: Eq. 2 enforces a large distance between and the maximum score of other verbalizations, whereas Eq. 7 penalizes their average score.