Sub topic 15
word2vec versus GloVe
This chapter assumes you know skip-gram with negative sampling, the windowed co-occurrence matrix, and the GloVe objective from the earlier chapters. It also assumes you are comfortable with dot products, logarithms, and gradient descent. Here we compare the two methods on their training signal, cost, and results, and then decide which to use.
Two routes to the same kind of vector
Both methods use the distributional hypothesis and produce dense vectors, usually 50 to 300 dimensions. They differ in how they get statistics from the corpus into those vectors.
- word2vec scans the corpus and turns each window into small prediction problems. A model predicts context words from a center word (skip-gram), or the center word from its context (CBOW). Parameters change after every pair or small batch.
- GloVe first counts how often each word pair co-occurs across the whole corpus and stores the counts in a matrix. It then fits vectors to that matrix by weighted least squares.
The objectives side by side
Skip-gram with negative sampling (SGNS) maximizes the following for each observed pair of center word w and context word c, with k sampled noise words:
log σ(uc · vw) + Σ over k noise words n of log σ(−un · vw)
The noise words come from the unigram distribution raised to the power 0.75. Each update touches only k + 1 output vectors plus one input vector.
GloVe minimizes a sum over only the nonzero entries of the co-occurrence matrix:
J = Σ f(Xij) · (wi · w̃j + bi + b̃j − log Xij)²
Here f is the weighting function. It is (x / xmax)^α when x < xmax and 1 otherwise, with the published defaults x_max = 100 and α = 0.75. It does two jobs. It sets the weight of zero counts to zero, so log 0 never appears. It also caps the influence of very frequent pairs such as ("the", "of").
import numpy as np
def glove_weight(x, x_max=100.0, alpha=0.75):
return np.minimum((x / x_max) ** alpha, 1.0)
def glove_pair_loss(w_i, w_j, b_i, b_j, x_ij):
error = np.dot(w_i, w_j) + b_i + b_j - np.log(x_ij)
return float(glove_weight(x_ij) * error ** 2)
for x in [1, 10, 100, 1000]:
print(x, round(float(glove_weight(x)), 4))
rng = np.random.default_rng(0)
w_i = rng.normal(scale=0.1, size=50)
w_j = rng.normal(scale=0.1, size=50)
print(glove_pair_loss(w_i, w_j, 0.0, 0.0, x_ij=20.0))The printed weights rise from 0.0316 at a count of 1 to 1.0 at a count of 100, and stay at 1.0 above that. Rare pairs are noisy, so they count for little. Very frequent pairs stop dominating once they pass the cap.
What the differences mean in practice
| Aspect | word2vec (SGNS) | GloVe |
|---|---|---|
| Training signal | Local windows, seen pair by pair | Global counts, aggregated first |
| Objective | Logistic loss on real versus noise pairs | Weighted squared error on log counts |
| Setup cost | None beyond a vocabulary | One pass to build the matrix, which can be large in memory |
| Cost per epoch | Grows with corpus size and window | Grows with the number of nonzero matrix entries |
| Frequency handling | Subsampling and the 0.75 noise power | Weighting function f and x_max |
| Vectors released | Input vectors, usually | Sum of word and context vectors |
| Incremental training | Easy to continue on new text | Needs a rebuilt or updated matrix |
Counting is the trade-off that matters most. Once X exists, a GloVe epoch does not depend on how many tokens the corpus holds. That helps with very large corpora when the vocabulary is fixed. Memory is the cost, because X must be stored and shuffled, and a cutoff on vocabulary size is usually needed. word2vec streams the text and needs almost no memory beyond the vectors. This also makes it easy to resume training or fine-tune on new text.
They are closer than they look
The two methods are less opposed than the "predictive versus count-based" label suggests. Levy and Goldberg (2014) showed that SGNS implicitly factorizes a shifted pointwise mutual information (PMI) matrix, where the dot product of the vectors approximates PMI(w, c) − log k. GloVe fits log counts, which is the same kind of quantity with the marginals absorbed into the bias terms. Both methods are therefore matrix factorization with different loss functions and weights.
Levy, Goldberg and Dagan (2015) went further. They found that hyperparameters often matter more than the algorithm. Window size, subsampling, the noise exponent, the number of negatives, and whether context vectors are added to word vectors can each change benchmark scores by more than the gap between methods. A fair comparison therefore needs matched settings and the same corpus.
Typical results
On word similarity and analogy benchmarks, the two methods usually land within a few points of each other when tuned. GloVe's paper reported an advantage on analogies, and later controlled studies largely narrowed it. Expect these patterns:
- Both place near-synonyms and related words as neighbours. The neighbours of "frog" will mix amphibians and species names in either model.
- Both show the king − man + woman ≈ queen pattern, but only approximately. The query word is excluded from the answers, which flatters the result.
- Both inherit the biases and polysemy problems of their corpus. A word with several senses gets one vector in either method.
- Rare words are weak in both. word2vec sees them in few windows, and GloVe gives them low weight in f.
Choosing between them
- You need good vectors quickly and a trusted release exists. Load pretrained GloVe or word2vec vectors. Match the vocabulary casing and tokenization to your pipeline.
- You have domain text, such as clinical notes or legal filings. Train word2vec with gensim. It runs from raw text, handles streaming, and supports further training.
- You have a huge corpus and a stable vocabulary. GloVe's count-once design can pay off, provided you can hold the matrix.
- You need subword information for rare or misspelled words. Neither method gives it. Consider fastText, which extends skip-gram with character n-grams.
Exercise
Your corpus is 200 GB of text, and your team needs to retrain embeddings every week as new documents arrive. Give one reason to prefer word2vec and one reason GloVe could still be attractive.
Show answer
word2vec streams the text, needs little memory, and can continue training on new documents, so weekly updates are cheap. GloVe could still be attractive because, after one counting pass, each epoch costs time proportional to the nonzero co-occurrence entries rather than the corpus size. That helps if the vocabulary is fixed and the matrix fits in memory, but a weekly update would need the counts rebuilt or updated.
Exercise
A GloVe pair has co-occurrence count 400 and another has count 4. Using x_max = 100 and α = 0.75, what are their weights, and why is the first one not larger than 1?
Show answer
The count 400 gives weight 1.0, because f is capped at 1 for x ≥ x_max. The count 4 gives (4/100)^0.75, which is about 0.0894. The cap stops very frequent pairs from dominating the loss.