Introduction
In Natural Language Processing (NLP), document embeddings are a fundamental tool for representing variable-length text as fixed-length numerical vectors. Standard training paradigms, such as the Paragraph Vector (PV-DBOW) model, typically optimize the dot product between document and word vectors. However, the dot product is inherently sensitive to vector magnitude; models can maximize scores simply by increasing vector lengths, a phenomenon particularly prevalent with frequent words. This "magnitude problem" often leads to overfitting, where the model relies on vector size rather than semantic orientation.
Methodology
This paper proposes a modification to the training objective of document embedding models: replacing the standard dot product with cosine similarity. By normalizing vectors during training, the model is forced to focus entirely on the angular relationship—and thus the semantic "direction"—of the data.
As illustrated in Figure 1, the model calculates the cosine similarity between the document vector ($v_d$) and n-gram vectors ($v_w$). To counteract the bounded nature of cosine similarity (-1 to 1), which can lead to "flat" probability distributions in the softmax layer, the authors introduce a scaling hyperparameter, $\alpha$. By scaling the similarity by a factor of 6, the model produces more confident and decisive probability distributions.
Experimental Results
The approach was evaluated using the IMDB movie review dataset, a classic benchmark for binary sentiment classification. The dataset includes 25,000 labeled training reviews, 25,000 test reviews, and 50,000 unlabeled reviews used for unsupervised embedding learning.
The results show a consistent performance gain when using cosine similarity across different feature sets:
| Feature Set | Dot Product Accuracy (%) | Cosine Similarity Accuracy (%) |
|---|---|---|
| Unigrams | 89.60 | 90.75 |
| Unigrams + Bigrams | 91.27 | 92.56 |
| Unigrams + Bigrams + Trigrams | 92.14 | 93.13 |
When combined with a Naive Bayes weighted bag-of-words model, the ensemble achieved a competitive accuracy of 93.68%.
Discussion
The transition to cosine similarity provides several advantages beyond raw accuracy:
- Implicit Regularization: Normalizing vector magnitudes prevents the model from over-weighting frequent terms, acting as a natural regularizer that often outperforms explicit L2 regularization.
- Geometric Separability: PCA visualizations demonstrate that cosine similarity leads to better-defined clusters in the embedding space.
- Optimization Sensitivity: Interestingly, training with cosine similarity requires a significantly lower learning rate (0.001 vs. 0.25), likely due to a more complex error surface.
As seen in Figure 2, the embeddings trained with cosine similarity (c) exhibit clearer separation between sentiment classes compared to those trained with the standard dot product (a) or dot product with L2 regularization (b).
Conclusion
Replacing the dot product with cosine similarity during training offers a simple yet effective way to improve document embeddings. By providing implicit regularization and better geometric alignment with inference metrics, this method achieves high performance on sentiment classification tasks. Future research will investigate whether these benefits extend to other NLP domains such as topic modeling and semantic similarity.
Summary updated