Paper commentary
Bag of Tricks for Efficient Text Classification
Simple linear models with fastText match deep classifiers while training far faster
- Problem Neural text classifiers are accurate but slow to train and run on very large datasets. Linear classifiers are cheap, but each class is learned separately, so classes with few examples gain little.
- Method fastText averages embeddings of word and N-gram features into one hidden vector, then scores it with a softmax. Instead of scoring every class, it uses a hierarchical softmax over a Huffman tree to cut the cost of many classes.
- Results On the YFCC100M tag task, fastText with 200 hidden units and bigrams reaches 46.1 prec@1. Tagspace with 200 hidden units reaches 35.6.
Why linear baselines fall short at scale
Neural text classifiers have become popular, but they are relatively slow at both training and test time, which restricts their use on very large datasets.
A plain linear classifier over bag of words estimates its weights for each feature and each class separately, so classes with few training examples get little benefit from what the model learns about other classes. Two standard remedies are to factorize the linear classifier into low-rank matrices or to replace it with a multilayer neural network, and both bring back the cost that linear models were meant to avoid.
The study asks how far a linear model can be pushed in training speed and output-space size by adding a rank constraint and a fast approximation of the loss, while keeping accuracy close to the state of the art.
How fastText turns a document into class probabilities
A document enters as its word features and N-gram features, which are normalized into a bag of features. The first weight matrix A is an embedding table indexed by these features, and the embeddings are averaged into a single hidden vector. That hidden vector is passed through a second weight matrix B, and a softmax f turns the resulting scores into a probability distribution over the predefined classes.
Training minimizes the average negative log-likelihood over the N documents, with x_n the normalized bag of features of document n, y_n its label, and A and B the two weight matrices. \[ -\frac{1}{N}\sum_{n=1}^{N} y_n \log\big(f(BAx_n)\big) \] Because the hidden vector is an average of embeddings and is then mapped linearly, the model has the form of a low-rank linear classifier, where the rank is set by the dimension of the hidden layer.
Training runs asynchronously on several CPUs with stochastic gradient descent and a linearly decaying learning rate.
Cheaper output layer with hierarchical softmax
The hierarchical softmax arranges the classes in a Huffman coding tree and lowers the training cost to O(h log2(k)).
\[ P(n_{l+1}) = \prod_{i=1}^{l} P(n_i) \] Here the node at depth l+1 has parents n_1 to n_l, so its probability is the product of the probabilities of its ancestors.
At test time, a depth-first search over the tree keeps track of the maximum probability among the leaves and discards any branch whose probability is small, which the authors observe reduces the test cost to O(h log2(k)) in practice. The same search extends to finding the top T targets at a cost of O(log(T)) by using a binary heap.
Recovering word order with N-gram features
A bag of words ignores word order, and modeling order explicitly is often computationally expensive, so fastText adds a bag of N-grams as extra features. These N-gram features capture some of the local order and are cheap to use, with results the authors describe as comparable to methods that model order explicitly.
The N-grams are mapped into a fixed index space with the hashing trick, using the same hashing function as in an earlier Mikolov et al. (2011) work, so that memory stays bounded. The number of hashing bins is 10M when only bigrams are used and 100M when longer N-grams are included.
Sentiment classification results
The sentiment experiments use the eight datasets and the evaluation protocol of Zhang et al. (2015), with fastText run with 10 hidden units. The learning rate is chosen on a validation set from {0.05, 0.1, 0.25, 0.5}, and fastText is trained for 5 epochs.
Adding bigrams improves accuracy by 1-4% on these datasets. For example, accuracy on Sogou rose from 93.9 to 96.8, and on Amazon Full from 55.8 to 60.2, when bigrams were added to the 10-hidden-unit model.
Using trigrams raises Sogou accuracy to 97.1%, and the validation search found that N-grams up to 5 gave the best performance.
The baseline rows in the sentiment table are taken from the cited prior works, while the fastText rows come from the authors' runs with shared parameters across datasets.
Comparison with Tang et al. on Yelp and IMDB
On Yelp'13, Yelp'14, Yelp'15 and IMDB, fastText reaches 64.2, 66.2, 66.6 and 45.2 test accuracy, while the LSTM-GRNN of Tang et al. reaches 65.1, 67.1, 67.6 and 45.3. Hyperparameters were chosen on the validation set, and the table reports test accuracy.
fastText is below the LSTM-GRNN on these four datasets, and above the SVM with TF features and the CNN in the same table. The authors attribute the roughly 1% gap to the fact that fastText does not use pre-trained word embeddings, while Tang et al. do.
Training and test speed on sentiment data
In Table 2, one epoch of fastText with 10 hidden units and bigrams takes 1s on AG, against 1h for the small char-CNN, 3h for the big char-CNN and 51m for the deepest VDCNN setting shown. On Amazon Full, one fastText epoch takes 9s, against 2d for the small char-CNN and 7h for the depth-29 VDCNN.
The char-CNN and VDCNN models were trained on an NVIDIA Tesla K40 GPU, while fastText was trained on a CPU with 20 threads. The GRNN method of Tang et al. takes around 12 hours per epoch on a CPU with a single thread.
The authors note that char-CNN could become about 10 times faster with more recent CUDA implementations of convolutions, yet fastText still trains in less than a minute on these datasets. The speed-up over neural network based methods grows with the size of the dataset, reaching at least 15,000× on the largest sentiment dataset.
The YFCC100M tag prediction dataset
YFCC100M contains almost 100M images with captions, titles and tags, and the tag task predicts tags from the title and caption only, without using the images. Words and tags occurring fewer than 100 times were removed before the data was split into train, validation and test sets.
The training set has 91,188,648 examples and 1.5B tokens, the validation set has 930,497 examples and the test set has 543,424 examples. The vocabulary has 297,141 words and the data contains 312,116 unique tags, so the output space is very large.
Accuracy is reported as precision at 1. The baselines are a frequency-based predictor that always outputs the most frequent tag and the linear version of Tagspace, which is built on the Wsabie model.
Tag prediction accuracy and speed against Tagspace
The frequency baseline reaches a prec@1 of 2.2, while Tagspace with 200 hidden units reaches 35.6 and fastText with 200 hidden units reaches 41.1. Adding bigrams raises fastText to 46.1 with 200 hidden units and to 36.7 with 50 hidden units, where it is 31.2 without bigrams.
Training fastText with 200 hidden units and bigrams takes 13m38, while Tagspace with 200 hidden units takes 5h32 to train. At test time, Tagspace computes a score for every class, which makes it slow when there are more than 300K classes, whereas fastText's fast inference keeps test time at 1m37 for the bigram model with 200 hidden units.
The authors report a 600× speedup in the test phase, and state that they are more than an order of magnitude faster to obtain a model of better quality. Training times are measured with 20 threads, while test times are measured on a single thread. Table 4 gives qualitative examples on the validation set, with some correct and some incorrect tag predictions from the 200-hidden-unit model with bigrams.
Limits of the evidence
The training-time comparison pairs fastText on a CPU with 20 threads against char-CNN and VDCNN on an NVIDIA Tesla K40 GPU, so the hardware differs across the rows. The speed figures therefore show the order of magnitude of the gap rather than a controlled cost comparison.
The tag task compares only with the linear version of Tagspace, and the convolutional version is not compared.
The authors question whether simple classification problems such as sentiment analysis are the right tests for the greater representational power of deep networks. Whether the approach keeps its accuracy on tasks beyond sentiment and tag prediction is not tested in these experiments.
Paper info
- Authors
- Armand Joulin, Edouard Grave, Piotr Bojanowski, Tomas Mikolov
- Venue
- arXiv · Preprint
- First published
- 2016-7-6
- Added
- 2026-11-11
- Updated
- 2016-8-9
- Version
- v3
- arXiv
- 1607.01759
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Armand Joulin, Edouard Grave, Piotr Bojanowski, Tomas Mikolov (2016). Bag of Tricks for Efficient Text Classification. arXiv:1607.01759v3. https://arxiv.org/abs/1607.01759v3
BibTeX
@misc{joulin2016tricks,
title = {{Bag of Tricks for Efficient Text Classification}},
author = {Armand Joulin and Edouard Grave and Piotr Bojanowski and Tomas Mikolov},
year = {2016},
eprint = {1607.01759},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1607.01759v3}
}