Abstract
We consider the problem of producing compact architectures for text classification, such that the full model fits in a limited amount of memory. After considering different solutions inspired by the hashing literature, we propose a method built upon product quantization to store word embeddings. While the original technique leads to a loss in accuracy, we adapt this method to circumvent quantization artefacts. Our experiments carried out on several benchmarks show that our approach typically requires two orders of magnitude less memory than fastText while being only slightly inferior with respect to accuracy. As a result, it outperforms the state of the art by a good margin in terms of the compromise between memory usage and accuracy.
Introduction
While modern deep learning models have revolutionized Natural Language Processing (NLP), they often come with a massive memory footprint. The popular fastText library is prized for its speed, but its reliance on large embedding matrices for words and n-grams can result in models that take up gigabytes of RAM. This is a major hurdle for "on-device" AI. If a spam filter or a sentiment analyzer is to run locally on a smartphone or an embedded system, it must fit within a limited memory budget. The challenge is to find a "sweet spot" where the model is tiny enough to fit in limited memory but still accurate enough to be useful.
fastText.zip introduces a powerful compression pipeline that shrinks text classification models by up to two orders of magnitude (100x–1000x) with minimal loss in accuracy. By combining product quantization with a novel norm-based feature pruning strategy, high-performance NLP models can fit into less than 100KB.
Methodology
The proposed multi-stage compression pipeline is built on top of the existing fastText architecture and includes several key components:
- Product Quantization (PQ): Instead of storing full-precision floating-point vectors, PQ breaks vectors into smaller sub-vectors and approximates them using a "codebook" of centroids. The authors specifically use Optimized Product Quantization (OPQ) and a variant that separately encodes the vector's norm (magnitude) and direction to minimize quantization errors.
- Norm-based Pruning: The authors discovered that the L2 norm of an embedding vector is a great proxy for its importance. By keeping only the $K$ embeddings with the highest norms, they can drastically reduce the vocabulary size.
- Max-Cover Pruning: To prevent "empty" documents (where all words are pruned away), a greedy algorithm ensures that every document in the training set is covered by at least one retained feature.
- The Hashing Trick: Hash functions map words and n-grams to buckets, eliminating the need to store a massive string-to-index dictionary.
- Re-training: Quantization is followed by re-training the output classifier to adapt to the new, quantized inputs, which recovers significant accuracy.
As seen in the figure above, using Product Quantization with explicit norm encoding (PQ, norm) consistently provides the best accuracy for a given memory budget per vector across different datasets like Sogou, Yahoo, and Yelp.
Experimental Results
The study evaluates the approach on eight standard benchmarks (AG News, Amazon reviews, DBPedia, Sogou news, Yahoo Answers, and Yelp reviews) and a massive 12GB FlickrTag dataset.
Key Insights
- Magnitude as Importance: Pruning based on the vector norm is significantly more effective than traditional frequency-based or entropy-based pruning in text classification.
- Quantization Synergy: Quantization and pruning are highly complementary. Cumulative compression ratios of 1000x are achievable by pruning the vocabulary and quantizing the remaining vectors.
- Efficiency: Compressed linear models (
fastText.zip) can often outperform character-level Convolutional Neural Networks (CNNs) while being much smaller and orders of magnitude faster to train.
Performance Trade-offs
The results show a remarkable trade-off between size and performance. On most benchmarks, models could be reduced by a factor of 100 with less than a 1% drop in accuracy. Several models were shrunk to under 100KB while remaining competitive with state-of-the-art techniques.
The figure above illustrates the loss of accuracy as models are pruned to extreme levels. Even at 1MB or 100KB, the pruned models (blue line) remain remarkably close to the full fastText performance (blue circle), significantly outperforming other baselines.
Conclusion
fastText.zip demonstrates that aggressive compression is possible for text classification without sacrificing significant utility. While extremely aggressive pruning can eventually lead to coverage issues for short documents, the current pipeline offers a robust solution for deploying NLP models on memory-constrained devices. Future work may explore variable vector sizes for different word frequencies and joint optimization of quantization and classifier weights.
Summary updated