Abstract: We develop a streaming (one-pass, bounded-memory) word embedding algorithm based on the canonical skip-gram with negative sampling algorithm implemented in word2vec. We compare our streaming algorithm to word2vec empirically by measuring the cosine similarity between word pairs under each algorithm and by applying each algorithm in the downstream task of hashtag prediction on a two-month interval of the Twitter sample stream. We then discuss the results of these experiments, concluding they provide partial validation of our approach as a streaming replacement for word2vec. Finally, we discuss potential failure modes and suggest directions for future work.
Introduction
Standard word embedding algorithms, such as Skip-Gram with Negative Sampling (SGNS) in word2vec, are designed for batch processing. They typically require two passes over a static dataset: one to build a fixed vocabulary of the most frequent words and another to train the vector representations. This paradigm is ill-suited for massive datasets that exceed disk capacity or for infinite data streams, like Twitter feeds, where the vocabulary is non-stationary and constantly evolving.
This paper introduces spacesaving-word2vec, a one-pass, bounded-memory algorithm that approximates the performance of batch word2vec on streaming data without requiring multiple passes or full dataset storage.
The Spacesaving-word2vec Algorithm
The proposed algorithm modifies SGNS to operate within a fixed memory footprint using several key innovations:
- Space-Saving Vocabulary Management: Instead of a static vocabulary, the model uses the Space-Saving algorithm to maintain an approximate list of the $K$ most frequent words. When the vocabulary limit is reached, the least frequent word is replaced by new incoming terms.
- Reservoir Sampling for Negative Sampling: To approximate the word distribution for negative sampling—a core component of SGNS—the algorithm maintains a reservoir of words seen throughout the stream. This ensures a uniform sample of the stream is available for training at any point.
- Dynamic Learning Rates: Each word is assigned an individual learning rate that decays with updates but resets if a word is ejected and subsequently re-enters the vocabulary.
Experimental Results
Intrinsic Evaluation: Fidelity-Memory Trade-off
The authors evaluated the algorithm's accuracy as a function of vocabulary size ($K$) using the text8 dataset (100MB of Wikipedia text). A critical finding is the relationship between $K$ and the relative error in word counts. As $K$ increases, the streaming model's count accuracy for frequent words improves significantly, eventually reaching near-zero error for the top-ranked terms.
With a sufficiently large $K$ (e.g., 70,000), the word similarities generated by the streaming model correlate highly (~0.8) with those from the batch word2vec model.
Extrinsic Evaluation: Hashtag Prediction
To test real-world utility, the model was applied to a two-month Twitter sample stream for hashtag prediction. A CNN trained with streaming embeddings (stream-cnn) achieved an F1 score of 0.42, nearly matching the 0.43 F1 score of a static-cnn trained on batch embeddings. This suggests that the streaming approximation is robust enough for practical downstream NLP tasks.
Limitations and Future Work
While the algorithm performs well, it faces challenges with out-of-vocabulary (OOV) words and non-stationary streams. If the vocabulary size $K$ is too small relative to the stream's diversity, the model may suffer from high OOV rates and poor similarity correlations.
Future work could address these limitations by incorporating sub-word information (e.g., character-level embeddings) to handle OOV terms and developing automated methods for hyperparameter tuning, such as learning rate adaptation, as the stream evolves.
Summary updated