Bag of Tricks for Efficient Text Classification

Armand Joulin, Edouard Grave, Piotr Bojanowski, Tomas Mikolov

Introduction

Text classification is an important task in Natural Language Processing with many applications, such as web search, information retrieval, ranking and document classification [Deerwester et al. (1990, Pang and Lee (2008]. Recently, models based on neural networks have become increasingly popular [Kim (2014, Zhang and LeCun (2015, Conneau et al. (2016]. While these models achieve very good performance in practice, they tend to be relatively slow both at train and test time, limiting their use on very large datasets.

Meanwhile, linear classifiers are often considered as strong baselines for text classification problems [Joachims (1998, McCallum and Nigam (1998, Fan et al. (2008]. Despite their simplicity, they often obtain state-of-the-art performances if the right features are used [Wang and Manning (2012]. They also have the potential to scale to very large corpus [Agarwal et al. (2014].

In this work, we explore ways to scale these baselines to very large corpus with a large output space, in the context of text classification. Inspired by the recent work in efficient word representation learning [Mikolov et al. (2013, Levy et al. (2015], we show that linear models with a rank constraint and a fast loss approximation can train on a billion words within ten minutes, while achieving performance on par with the state-of-the-art. We evaluate the quality of our approach fastTexthttps://github.com/facebookresearch/fastText on two different tasks, namely tag prediction and sentiment analysis.

Model architecture

A simple and efficient baseline for sentence classification is to represent sentences as bag of words (BoW) and train a linear classifier, e.g., a logistic regression or an SVM [Joachims (1998, Fan et al. (2008]. However, linear classifiers do not share parameters among features and classes. This possibly limits their generalization in the context of large output space where some classes have very few examples. Common solutions to this problem are to factorize the linear classifier into low rank matrices [Schutze (1992, Mikolov et al. (2013] or to use multilayer neural networks [Collobert and Weston (2008, Zhang et al. (2015].

Figure 1 shows a simple linear model with rank constraint. The first weight matrix AA is a look-up table over the words. The word representations are then averaged into a text representation, which is in turn fed to a linear classifier. The text representation is an hidden variable which can be potentially be reused. This architecture is similar to the cbow model of ?), where the middle word is replaced by a label. We use the softmax function ff to compute the probability distribution over the predefined classes. For a set of NN documents, this leads to minimizing the negative log-likelihood over the classes:

where xnx_{n} is the normalized bag of features of the nn-th document, yny_{n} the label, AA and BB the weight matrices. This model is trained asynchronously on multiple CPUs using stochastic gradient descent and a linearly decaying learning rate.

When the number of classes is large, computing the linear classifier is computationally expensive. More precisely, the computational complexity is O(kh)O(kh) where kk is the number of classes and hh the dimension of the text representation. In order to improve our running time, we use a hierarchical softmax [Goodman (2001] based on the Huffman coding tree [Mikolov et al. (2013]. During training, the computational complexity drops to O(hlog2(k))O(h\log_{2}(k)).

The hierarchical softmax is also advantageous at test time when searching for the most likely class. Each node is associated with a probability that is the probability of the path from the root to that node. If the node is at depth l+1l+1 with parents n1,,nln_{1},\dots,n_{l}, its probability is

This means that the probability of a node is always lower than the one of its parent. Exploring the tree with a depth first search and tracking the maximum probability among the leaves allows us to discard any branch associated with a small probability. In practice, we observe a reduction of the complexity to O(hlog2(k))O(h\log_{2}(k)) at test time. This approach is further extended to compute the TT-top targets at the cost of O(log(T))O(\log(T)), using a binary heap.

2 N-gram features

Bag of words is invariant to word order but taking explicitly this order into account is often computationally very expensive. Instead, we use a bag of n-grams as additional features to capture some partial information about the local word order. This is very efficient in practice while achieving comparable results to methods that explicitly use the order [Wang and Manning (2012].

We maintain a fast and memory efficient mapping of the n-grams by using the hashing trick [Weinberger et al. (2009] with the same hashing function as in ?) and 10M bins if we only used bigrams, and 100M otherwise.

Experiments

We evaluate fastText on two different tasks. First, we compare it to existing text classifers on the problem of sentiment analysis. Then, we evaluate its capacity to scale to large output space on a tag prediction dataset. Note that our model could be implemented with the Vowpal Wabbit library,Using the options --nn, --ngrams and --log_multi but we observe in practice, that our tailored implementation is at least 2-5×\times faster.

We employ the same 8 datasets and evaluation protocol of ?). We report the n-grams and TFIDF baselines from ?), as well as the character level convolutional model (char-CNN) of ?), the character based convolution recurrent network (char-CRNN) of [Xiao and Cho (2016] and the very deep convolutional network (VDCNN) of ?). We also compare to ?) following their evaluation protocol. We report their main baselines as well as their two approaches based on recurrent networks (Conv-GRNN and LSTM-GRNN).

Results.

We present the results in Figure 1. We use 10 hidden units and run fastText for 5 epochs with a learning rate selected on a validation set from {\{0.05, 0.1, 0.25, 0.5}\}. On this task, adding bigram information improves the performance by 1-4%\%. Overall our accuracy is slightly better than char-CNN and char-CRNN and, a bit worse than VDCNN. Note that we can increase the accuracy slightly by using more n-grams, for example with trigrams, the performance on Sogou goes up to 97.1%\%. Finally, Figure 3 shows that our method is competitive with the methods presented in ?). We tune the hyper-parameters on the validation set and observe that using n-grams up to 5 leads to the best performance. Unlike ?), fastText does not use pre-trained word embeddings, which can be explained the 1%\% difference in accuracy.

Training time.

Both char-CNN and VDCNN are trained on a NVIDIA Tesla K40 GPU, while our models are trained on a CPU using 20 threads. Table 2 shows that methods using convolutions are several orders of magnitude slower than fastText. While it is possible to have a 10×\times speed up for char-CNN by using more recent CUDA implementations of convolutions, fastText takes less than a minute to train on these datasets. The GRNNs method of ?) takes around 12 hours per epoch on CPU with a single thread. Our speed-up compared to neural network based methods increases with the size of the dataset, going up to at least a 15,000×\times speed-up.

2 Tag prediction

To test scalability of our approach, further evaluation is carried on the YFCC100M dataset [Thomee et al. (2016] which consists of almost 100M images with captions, titles and tags. We focus on predicting the tags according to the title and caption (we do not use the images). We remove the words and tags occurring less than 100 times and split the data into a train, validation and test set. The train set contains 91,188,648 examples (1.5B tokens). The validation has 930,497 examples and the test set 543,424. The vocabulary size is 297,141 and there are 312,116 unique tags. We will release a script that recreates this dataset so that our numbers could be reproduced. We report precision at 1.

We consider a frequency-based baseline which predicts the most frequent tag. We also compare with Tagspace [Weston et al. (2014], which is a tag prediction model similar to ours, but based on the Wsabie model of ?). While the Tagspace model is described using convolutions, we consider the linear version, which achieves comparable performance but is much faster.

Results and training time.

Table 5 presents a comparison of fastText and the baselines. We run fastText for 5 epochs and compare it to Tagspace for two sizes of the hidden layer, i.e., 50 and 200. Both models achieve a similar performance with a small hidden layer, but adding bigrams gives us a significant boost in accuracy. At test time, Tagspace needs to compute the scores for all the classes which makes it relatively slow, while our fast inference gives a significant speed-up when the number of classes is large (more than 300K here). Overall, we are more than an order of magnitude faster to obtain model with a better quality. The speedup of the test phase is even more significant (a 600×\times speedup). Table 4 shows some qualitative examples.

Discussion and conclusion

In this work, we propose a simple baseline method for text classification. Unlike unsupervisedly trained word vectors from word2vec, our word features can be averaged together to form good sentence representations. In several tasks, fastText obtains performance on par with recently proposed methods inspired by deep learning, while being much faster. Although deep neural networks have in theory much higher representational power than shallow models, it is not clear if simple text classification problems such as sentiment analysis are the right ones to evaluate them. We will publish our code so that the research community can easily build on top of our work.

We thank Gabriel Synnaeve, Hervé Gégou, Jason Weston and Léon Bottou for their help and comments. We also thank Alexis Conneau, Duyu Tang and Zichao Zhang for providing us with information about their methods.

References