SlowGuess/NAACL_2021_OCR huggingface dataset:
Zihan Wang  $^{1}$  Dheeraj Mekala  $^{1}$  Jingbo Shang  $^{1,2}$
$^{2}$  Haliccioglu Data Science Institute, University of California San Diego, CA, USA
{ziw224, dmekala, jshang}@ucsd.edu
In this paper, we explore text classification with extremely weak supervision, i.e., only relying on the surface text of class names. This is a more challenging setting than the seed-driven weak supervision, which allows a few seed words per class. We opt to attack this problem from a representation learning perspective—ideal document representations should lead to nearly the same results between clustering and the desired classification. In particular, one can classify the same corpus differently (e.g., based on topics and locations), so document representations should be adaptive to the given class names. We propose a novel framework X-Class to realize the adaptive representations. Specifically, we first estimate class representations by incrementally adding the most similar word to each class until inconsistency arises. Following a tailored mixture of class attention mechanisms, we obtain the document representation via a weighted average of contextualized word representations. With the prior of each document assigned to its nearest class, we then cluster and align the documents to classes. Finally, we pick the most confident documents from each cluster to train a text classifier. Extensive experiments demonstrate that X-Class can rival and even outperform seed-driven weakly supervised methods on 7 benchmark datasets.
Weak supervision has been recently explored in text classification to save human effort. Typical forms of weak supervision include a few labeled documents per class (Meng et al., 2018; Jo and Cinarel, 2019), a few seed words per class (Meng et al., 2018, 2020a; Mekala and Shang, 2020; Mekala et al., 2020), and other similar open-data (Yin et al., 2019). Though much weaker than a fully annotated corpus, these forms still require non-trivial, corpus-specific knowledge from experts. For example, nominating seed words requires experts to consider
(a) NYT-Topics
(b) NYT-Locations
Figure 1: Visualizations of the same news corpus using Average BERT Representations on two criteria. Colors denote different classes.
their relevance to not only the desired classes but also the input corpus; To acquire a few labeled documents per class, unless the classes are balanced, one needs to sample and annotate a much larger number of documents to cover the minority class.
In this paper, we focus on extremely weak supervision, i.e., only relying on the surface text of class names. This setting is much more challenging than the ones above, and can be considered as almost-unsupervised text classification.
We opt to attack this problem from a representation learning perspective—ideal document representations should lead to nearly the same result between clustering and the desired classification. Recent advances in contextualized representation learning using neural language models have demonstrated the capability of clustering text to domains with high accuracy (Aharoni and Goldberg, 2020). Specifically, a simple average of word representations is sufficient to group documents on the same topic together. However, the same corpus could be classified using various criteria other than topics, such as locations and sentiments. As visualized in Figure 1, such class-invariant representations separate topics well but mix up locations. Therefore, it is a necessity to make document representations adaptive to the user-specified class names.
We propose a novel framework X-Class to conduct text classification with extremely weak supervision, as illustrated in Figure 2. Firstly, we esti
Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 3043-3053 June 6-11, 2021. ©2021 Association for Computational Linguistics
Figure 2: An overview of our X-Class. Given a raw input corpus and user-specified class names, we first estimate a class-oriented representation for each document. And then, we align documents to classes with confidence scores by clustering. Finally, we train a supervised model (e.g., BERT) on the confident document-class pairs.
mate class representations by incrementally adding the most similar word to each class and recalculating its representation. Following a tailored mixture of class attention mechanisms, we obtain the document representation via a weighted average of contextualized word representations. These representations are based on pre-trained neural language models, and they are supposed to be in the same latent space. We then adopt clustering methods (e.g., Gaussian Mixture Models) to group the documents into  $K$  clusters, where  $K$  is the number of desired classes. The clustering method is initialized with the prior knowledge of each document assigned to its nearest class. We preserve this assignment so we can easily align the final clusters to the classes. In the end, we pick confident documents from each cluster to form a pseudo training set, based on which, we can train any document classifier. In our implementation, we use BERT as both the pre-trained language model and the text classifier. Compared with existing weakly supervised methods, X-Class has a stronger and more consistent performance on 7 benchmark datasets, despite some of them using at least 3 seed words per class. It is also worth mentioning that X-Class has a much more mild requirement on the existence of class names in the corpus, whereas existing methods rely on the variety of contexts of the class names.
- We advocate an important but not-well-studied problem of text classification with extremely weak supervision.
- We develop a novel framework X-Class to attack this problem from a representation learning perspective. It estimates high-quality, class-oriented document representations based on pre-trained
neural language models so that the confident clustering examples could form pseudo training set for any document classifiers to train on.
- We show that on 7 benchmark datasets, X-Class achieves comparable and even better performance than existing weakly supervised methods that require more human effort.
Reproducibility. We will release both datasets and codes on  $\mathrm{Gibthub}^{1}$
In this section, we formally define the problem of text classification with extremely weak supervision. And then, we brief on some preliminaries about BERT (Devlin et al., 2019), Attention (Luong et al., 2015) and Gaussian Mixture Models.
Problem Formulation. The extremely weak supervision setting confines our input to only a set of documents  $D_{i}, i \in \{1, \dots, n\}$  and a list of class names  $c_{j}, j \in \{1, \dots, k\}$ . The class names here are expected to provide hints about the desired classification objective, considering that different criteria (e.g., topics, sentiments, and locations) could classify the same set of documents. Our goal is to build a classifier to categorize a (new) document into one of the classes based on the class names.
Seed-driven weak supervision requires carefully designed label-indicative keywords that concisely define what a class represents. This requires human experts to understand the corpus extensively. One of our motivations is to relax this burdensome requirement. Interestingly, in experiments, our proposed X-Class using extremely weak supervision can offer comparable and even better performance than the seed-driven methods.
https://github.com/ZihanWangKi/XClass
BERT. BERT is a pre-trained masked language model with a transformer structure (Devlin et al., 2019). It takes one or more sentences as input, breaks them up into word-pieces, and generates a contextualized representation for each word-piece. To handle long documents in BERT, we apply a sliding window technique. To retrieve representations for words, we average the representations of the word's word-pieces. BERT has been widely adopted in a large variety of NLP tasks as a backbone. In our work, we will utilize BERT for two purposes: (1) representations for words in the documents and (2) the supervised text classifier.
Attention. Attention mechanisms assign weights to a sequence of vectors, given a context vector (Luong et al., 2015). It first estimates a hidden state  $\tilde{h}_j = K(h_j, c)$  for each vector  $h_j$ , where  $K$  is a similarity measure and  $c$  is the context vector. Then, the hidden states are transformed into a distribution via a softmax function. In our work, we use attention to assign weights to representations, which we then average them accordingly.
Gaussian Mixture Model. Gaussian Mixture Model (GMM) is a traditional clustering algorithm (Duda and Hart, 1973). It assumes that each cluster is generated through a Gaussian process. Given an initialization of the cluster centers and the co-variance matrix, it iteratively optimizes the point-cluster memberships and the cluster parameters following an Expectation-Maximization framework. Unlike K-Means, it does not restrict clusters to have a perfect ball-like shape. Therefore, we apply GMM to cluster our document representations.
3 Our X-Class Framework
As shown in Figure 2, our X-Class framework contains three modules: (1) class-oriented document representation estimation, (2) document-class alignment through clustering, and (3) text classifier training based on confident labels.
3.1 Class-oriented Document Representation
Ideally, we wish to have some document representations such that clustering algorithms can find  $k$  clusters very similar to the  $k$  desired classes.
We propose to estimate the document representations and class representations based on pretrained neural language models. Algorithm 1 is an overview. In our implementation, we use BERT as an example. For each document, we want its document representation to be similar to the class
representation of its desired class.
Aharoni and Goldberg (2020) demonstrated that contextualized word representations generated by BERT can preserve the domain (i.e., topic) information of documents. Specifically, they generated document representations by averaging contextualized representations of its constituent words, and they observed these document representations to be very similar among documents belonging to the same topic. This observation motivates us to "classify" documents by topics in an unsupervised way. However, this unsupervised method may not work well on criteria other than topics. For example, as shown in Figure 1, such document representations work well for topics but poorly for locations.
We therefore incorporate information from the given class names and obtain class-oriented document representations. We break down this module into two parts, (1) class representation estimation and (2) document representation estimation.
Class Representation Estimation. Inspired by seed-driven weakly supervised methods, we argue that a few keywords per class would be enough
Figure 3: Overview of Our Class Rep. Estimation.
to understand the semantics of the user-specified classes. Intuitively, the class name could be the first keyword we can start with. We propose to incrementally add new keywords to each class to enrich our understanding.
Figure 3 shows an overview of our class representation estimation. First, for each word, we obtain its static representation via averaging the contextualized representations of all its occurrences in the input corpus. For words that are broken into word-piece tokens, we average all the token representations as the word's representation. Then, we define the static representation  $\mathbf{s}_w$  of a word  $w$  as
$$
\mathbf {s} _ {w} = \frac {\sum_ {D _ {i , j} = w} \mathbf {t} _ {i , j}}{\sum_ {D _ {i , j} = w} 1} \tag {1}
$$
where  $D_{i,j}$  is the  $j$ -th word in the document  $D_{i}$  and  $\mathbf{t}_{i,j}$  is its contextualized word representation. Ethayarajh (2019) adopted a similar strategy of estimating a static representation using BERT. Such static representations are used as anchors to initialize our understanding of the classes.
A straightforward way to enrich the class representation is to take a fixed number of words similar to the class name and average them to get a class representation. However, it suffers from two issues: (1) setting the same number of keywords for all classes may hurt the minority classes, and (2) a simple average may shift the semantics away from the class name itself. As an extreme example, when the  $99\%$  of documents are talking about sports and the rest  $1\%$  are about politics, it is not reasonable to add as many keywords as sports to politics—it will diverge the politics representation.
To address these two issues, we iteratively find the next keyword for each class and recalculate the class representation by a weighted average on all the keywords found. We stop this iterative process
Figure 4: Overview of Our Document Rep. Estimation.
when the new representation is not consistent with the previous one. In this way, different classes will have a different number of keywords adaptively. Specifically, we define a comprehensive representation  $\mathbf{x}_l$  for a class  $l$  as a weighted average representation based on a ranked list of keywords  $\kappa_l$ . The top-ranked keywords are expected to have more similar static representations to the class representation. Assuming that the similarities follow Zipf's laws distribution (Powers, 1998), we define the weight of the  $i$ -th keyword as  $1 / i$ . That is,
$$
\mathbf {x} _ {l} = \frac {\sum_ {i = 1} ^ {| \mathcal {K} _ {l} |} 1 / i \cdot \mathbf {s} _ {\mathcal {K} _ {l , i}}}{\sum_ {i = 1} ^ {| \mathcal {K} _ {l} |} 1 / i} \tag {2}
$$
For a given class, the first keyword in this list is always the class name. In the  $i$ -th iteration, we retrieve the out-of-list word with the most similar static representation to the current class representation. We then calculate a new class representation based on all the  $i + 1$  words. We stop this expansion if we already have enough (e.g.,  $T = 100$ ) keywords, or the new class representation cannot yield the same set of top- $i$  keywords in our list. In our experiments, some classes indeed stop before reaching 100 keywords.
Document Representation Estimation. Intuitively, the content of each document should stick to its underlying class. For example, in the sentence "I cheered for Lakers winning NBA", its content covers sports and happy classes, but not arts, politics, or sad. Therefore, we assume that each word in a document is either similar to its desired class's representation or unrelated to all classes. Based on this assumption, we upgrade the simple average of contextualized word representations (Aharoni and Goldberg, 2020) to a weighted average. Specifically, we follow the popular attention mechanisms to assign weights to the words based on their similarities to the class representations.
Figure 4 shows an overview of our document representation estimation. We propose to employ a mixture of attention mechanisms to make it more robust. For the  $j$ -th word in the  $i$ -th document
$D_{i,j} = w$ , there are two possible representations: (1) the contextualized word representation  $\mathbf{t}_{i,j}$  and (2) the static representation of this word  $\mathbf{s}_w$ . The contextualized representations disambiguate words with multiple senses by considering the context, while the static version accounts for outliers that may exist in documents. Therefore, it is reasonable to use either of them as the word representation e for attention mechanisms. Given the class representations  $\mathbf{x}_c$ , we define two attention mechanisms:
- one-to-one:  $h_{i,j} = \max_c\{\cos (\mathbf{e},\mathbf{x_c})\}$ . It captures the maximum similarity to one class. This is useful for detecting words that are specifically similar to one class, such as NBA to sports.
- one-to-all:  $h_{i,j} = \cos(\mathbf{e}, \mathrm{avg}_c\{\mathbf{x}_c\})$  which is the similarity to the average of all classes. This ranks words by how related it is to the general set of classes in focus.
Combining 2 choices of e and 2 choices of attention mechanisms totals 4 ways to compute each word's attention weight. We further fuse these attention weights in an unsupervised way. Instead of using the similarity values directly, we rely on the rankings. Specifically, we sort the words decreasingly based on attention weights to obtain 4 ranked lists. Following previous work (Mekala and Shang, 2020; Tao et al., 2018), we utilize the geometric mean of these ranks for each word and then form a unified ranked list. Like class representation estimation, we follow Zipf's law and assign a weight of  $1 / r$  to a word ranked at the  $r$ -th position in the end. Finally, we obtain the document representation  $\mathbf{E}_i$  from  $\mathbf{t}_{i,j}$  with these weights.
$$
\mathbf {E} _ {i} = \frac {\sum_ {j} \frac {1}{r _ {j}} \cdot \mathbf {t} _ {i , j}}{\sum_ {j} \frac {1}{j}} \tag {3}
$$
3.2 Document-Class Alignment
One straightforward idea to align the documents to classes is simply finding the most similar class based on their representations. However, document representations not necessarily distribute ball-shape around the class representation—the dimensions in the representation can be correlated freely.
To address this challenge, we leverage the Gaussian Mixture Model (GMM) to capture the covariances for the clusters. Specifically, we set the number of clusters the same as the number of classes  $k$  and initialize the cluster parameters based on the prior knowledge that each document
$D_{i}$  is assigned to its nearest class  $L_{i}$ , as follows.
$$
L _ {i} = \arg \max  _ {c} \cos \left(\mathbf {E} _ {i}, \mathbf {x} _ {c}\right) \tag {4}
$$
We use a tied co-variance matrix across all clusters since we believe classes are similar in granularity. We cluster the documents while remembering the class each cluster is initialized to. In this way, we can align the final clusters to the classes.
Considering the potential redundant noise in these representations, we also apply principal component analysis (PCA) for dimension reduction following the experience in topic clustering (Aharoni and Goldberg, 2020). By default, we fix the PCA dimension  $P = 64$ .
3.3 Text Classifier Training
The alignment between documents and classes produce high-quality pseudo labels for documents in the training set. To generalize such knowledge to unseen text documents, we train a text classifier using these pseudo labels as ground truth. This is a classical noisy training scenario (Angluin and Laird, 1987; Goldberger and Ben-Reuven, 2017). Since we know how confident we are on each instance (i.e., the posterior probability on its assigned cluster in GMM), we select the most confident ones to train a text classifier (e.g., BERT). By default, we set a confidence threshold  $\delta = 50\%$ , i.e., the top  $50\%$  instances are selected for classifier training.
We conduct extensive experiments to show and ablate the performance of X-Class.
We compare with two seed-driven weakly supervised methods. WeSTClass (Meng et al., 2018) generates pseudo-labeled documents via word embeddings of keywords and employs a self-training module to get the final classifier. We use the CNN version of WeSTClass as it is reported to have better performance compared to the HAN version. ConWea (Mekala and Shang, 2020) utilizes pre-trained neural language models to make the weak supervision contextualized. In our experiments, we feed at least 3 seed words per class to these two.
We also compare with LOTClass (Meng et al., 2020b), which works under the extremely weak supervision setting. In their experiments, it mostly relies on class names but has used a few keywords
Table 1: An overview of our 7 benchmark datasets. They cover various domains and classification criteria. The imbalance factor of a dataset refers to the ratio of its largest class's size to the smallest class's size.
AGNews	20News	NYT-Small	NYT-Topic	NYT-Location	Yelp	DBpedia
Corpus Domain	News	News	News	News	News	Reviews	Wikipedia
Class Criterion	Topics	Topics	Topics	Topics	Locations	Sentiment	Ontology
# of Classes	4	5	5	9	10	2	14
# of Documents	120,000	17,871	13,081	31,997	31,997	38,000	560,000
Imbalance	1.0	2.02	16.65	27.09	15.84	1.0	1.0
Table 2: Evaluations of Compared Methods and X-Class. Both micro-/macro- $F_1$  scores are reported. Supervised provides an upper bound.  $\dagger$  indicates the use of at least 3 seed words per class.  $\ddagger$  indicates the use of only class names.  $\S$  refers to number coming from other papers. ConWea is too slow on DBpedia, therefore not reported.
Model	AGNews	20News	NYT-Small	NYT-Topic	NYT-Location	Yelp	DBpedia
Supervised	93.99/93.99	96.45/96.42	97.95/95.46	94.29/89.90	95.99/94.99	95.7/95.7	98.96/98.96
WeSTClass†	82.3/82.1§	71.28/69.90	91.2/83.7§	68.26/57.02	63.15/53.22	81.6/81.6§	81.42/81.19
ConWea†	74.6/74.2	75.73/73.26	95.23/90.79	81.67/71.54	85.31/83.81	71.4/71.2	N/A
LOTClass‡	86.89/86.82	73.78/72.53	78.12/56.05	67.11/43.58	58.49/58.96	87.75/87.68	86.66/85.98
X-Class‡	85.74/85.66	78.62/77.76	97.18/94.02	79.02/68.55	91.8/91.98	90.0/90.0	91.32/91.17
Ablations
X-Class-Rep‡	77.86/76.84	75.37/73.7	92.13/83.69	77.06/65.05	86.36/88.1	78.0/77.19	74.05/71.74
X-Class-Align‡	83.32/83.28	79.19/78.46	96.42/92.32	79.12/67.76	90.09/90.63	87.19/87.13	87.36/87.27
X-Class-ExactT‡	84.85/84.76	73.95/74.13	97.18/94.02	79.18/68.96	88.94/88.02	90.0/90.0	88.48/88.37
X-Class-KMeans‡	81.29/81.08	70.79/71.18	94.96/89.66	72.83/64.79	93.88/92.94	80.6/80.56	65.76/66.94
to elaborate on some difficult classes. In our experiments, we only feed the class names to it.
We denote our method as X-Class. To further understand the effects of different modules, we have four ablation versions. X-Class-Rep refers to the prior labels  $L_{i}$  derived based on class-oriented document representation. X-Class-Align refers to the labels obtained after document-class alignment. X-Class-ExactT refers to not doing consistency check when estimating class representations, and having exactly T class words. X-Class-KMeans refers to using K-Means (Lloyd, 1982) of GMM during document class alignment.
We present the performance of supervised models, serving as an upper-bound for X-Class. Specifically, Supervised refers to a BERT model cross-validated on the training set with 2 folds (matching our confidence selection threshold).
Many different datasets have been adopted to evaluate weakly supervised methods in different works. This makes it hard for systematic comparison.
In this paper, we pool the most popular datasets to establish a benchmark on weakly supervised text classification. Table 1 provides an overview of our carefully selected 7 datasets, covering different text sources (e.g., news, reviews, and Wikipedia articles) and different criteria of classes (e.g., topics, locations, and sentiment).
- AGNews from (Zhang et al., 2015) (used in WeSTClass and LOTClass) is for topic categorization in news from AG's corpus.
- 20News from (Lang, 1995) $^2$  (used in WeSTClass and ConWea) is for topic categorization in news.
- NYT-Small (used in WeSTClass and ConWea) is for classifying topic in New York Times news.
- NYT-Topic (used in (Meng et al., 2020a)) is another larger dataset collected from New York Times for topic categorization.
- NYT-Location (used in (Meng et al., 2020a)) is the same corpus as NYT-Topic but for locations. It is noteworthy to point out that many documents from this dataset talk about several countries simultaneously, so simply checking the location names will not lead to satisfactory results.
- Yelp from (Zhang et al., 2015) (used in WeST-Class) is for sentiment analysis in reviews.
- DBpedia from (Zhang et al., 2015) (used in LOT-Class) is for topic classification based on titles and descriptions in DBpedia.
For all X-Class experiments, we report the performance under one fixed random seed. By default, we set  $T = 100$ ,  $P = 64$ ,  $\delta = 50\%$ . For contextualized token representations  $\mathbf{t}_{i,j}$ , we use the BERT-base-uncased to group more occur
(a) Our Class-Oriented Document Representations
Figure 5: t-SNE Visualizations of Representations. From left to right: NYT-Topics, NYT-Locations, Yelp.
(b) Simple Average of BERT Representations