Abstract
Vector representations of sentences, trained on massive text corpora, are widely used as generic sentence embeddings across a variety of NLP problems. The learned representations are generally assumed to be continuous and real-valued, giving rise to a large memory footprint and slow retrieval speed, which hinders their applicability to low-resource (memory and computation) platforms, such as mobile devices. In this paper, we propose four different strategies to transform continuous and generic sentence embeddings into a binarized form, while preserving their rich semantic information. The introduced methods are evaluated across a wide range of downstream tasks, where the binarized sentence embeddings are demonstrated to degrade performance by only about 2% relative to their continuous counterparts, while reducing the storage requirement by over 98%. Moreover, with the learned binary representations, the semantic relatedness of two sentences can be evaluated by simply calculating their Hamming distance, which is more computational efficient compared with the inner product operation between continuous embeddings. Detailed analysis and case study further validate the effectiveness of proposed methods.
Introduction
In modern Natural Language Processing (NLP), sentences are typically represented as high-dimensional vectors of real numbers. While these embeddings are powerful, they present significant challenges for deployment on "edge" devices like smartphones or IoT hardware. Specifically, storing thousands of 4096-dimensional float32 vectors consumes substantial RAM and disk space, and calculating similarity between these vectors is computationally expensive. This research explores "binarizing" these vectors into strings of 1s and 0s, allowing for massive storage savings and lightning-fast similarity calculations using the Hamming distance.
Binarization Strategies
The authors propose and evaluate four distinct methods for converting continuous embeddings ($h$) into compact binary codes ($b$):
- Hard Threshold: The most basic approach where values above a specific threshold become 1 and others become 0. While simple, it often results in significant information loss.
- Random Projection: This method multiplies the original vector by a random matrix to adjust dimensionality before binarization. It requires no training and serves as a surprisingly robust baseline.
- Principal Component Analysis (PCA): This technique projects embeddings into a lower-dimensional space that captures maximum variance before thresholding, utilizing the statistical distribution of the data.
- Autoencoder (AE) with Semantic-Preserving Loss: This sophisticated architecture uses an encoder to compress vectors into bits and a decoder to reconstruct the original continuous vector. The key innovation is a Semantic-preserving Regularizer ($L_{sp}$), which penalizes the model if the Hamming distance between binary codes does not reflect the cosine similarity of the original continuous vectors.
To overcome the non-differentiable nature of binary bits during training, the authors employ a Straight-Through (ST) Estimator to facilitate gradient flow through the threshold function.
Experimental Results
The proposed methods were evaluated using the SentEval toolkit across various classification and sentence-matching tasks. The models were trained on the SNLI and MultiNLI datasets.
Efficiency Gains
The transition to binary representations yielded a storage reduction of over 98%. Furthermore, calculating the Hamming distance was found to be over 12 times faster than calculating cosine similarity in standard Python implementations, owing to highly optimized bitwise operations in hardware.
Accuracy and Code Length
The best-performing model, AE-binary-SP (Autoencoder with Semantic-Preserving loss), showed only a marginal 2% average performance degradation compared to continuous embeddings. As shown in the figure below, the AE-SP method scales effectively with the number of bits, consistently outperforming standard autoencoders and baseline projection methods at higher bit counts.
Conclusion
This work demonstrates that high-quality sentence embeddings can be compressed into binary form with minimal loss in semantic utility. The success of the semantic-preserving loss highlights that reconstruction alone is insufficient for useful compression; maintaining the relative distances between vectors is crucial. While the current approach relies on binarizing existing pre-trained embeddings, future work may explore end-to-end binary training and deeper hardware-level optimizations to further enhance on-device performance.
Summary updated