Sub topic 3
word2vec versus GloVe
This chapter assumes you know Python and NumPy, dot products, cosine similarity, and the idea of a loss function. You have also seen context windows in earlier chapters.
word2vec and GloVe both turn words into dense vectors. word2vec learns by predicting words from nearby words, one local window at a time. GloVe counts how often word pairs co-occur across the whole corpus, then fits those counts with a weighted least-squares loss.
| Aspect | word2vec | GloVe |
|---|---|---|
| Training signal | Local windows, one pair at a time | Global co-occurrence counts |
| Preprocessing | None beyond tokenizing | Builds a co-occurrence matrix first |
| Typical result | Strong neighbours and analogies | Similar quality; depends on corpus and settings |
Training word2vec with gensim
Install the library with pip install gensim. The model below uses skip-gram (sg=1) with negative sampling. This tiny corpus is only for showing the API, so its neighbours are not reliable.
from gensim.models import Word2Vec
sentences = [
["the", "cat", "sits", "on", "the", "mat"],
["the", "dog", "sits", "on", "the", "rug"],
["the", "cat", "chases", "the", "mouse"],
]
model = Word2Vec(sentences=sentences, vector_size=50, window=2,
min_count=1, sg=1, negative=5, epochs=50,
workers=1, seed=1)
print(model.wv["cat"].shape)Using pretrained GloVe vectors
Training on a small corpus gives poor vectors. Pretrained GloVe vectors trained on Wikipedia and Gigaword are a better starting point. The first call downloads the file.
import gensim.downloader as api
wv = api.load("glove-wiki-gigaword-50")
print(wv["king"].shape)Nearest neighbours and analogy queries
most_similar ranks words by cosine similarity. Analogies use vector arithmetic.
print(wv.most_similar("frog", topn=5))
print(wv.most_similar(positive=["king", "woman"], negative=["man"], topn=3))"queen" usually ranks near the top of the second query. Judge each result before you trust it. Check whether the neighbours make sense, and check whether a rare word has enough training data behind its vector.
Evaluating embeddings
Two families of tests are common:
- Intrinsic tests check the vectors directly, using word similarity ratings or analogy sets.
- Extrinsic tests plug the vectors into a downstream task, such as a text classifier, and measure that task's accuracy.
> Note: No single test is enough. Use an intrinsic test for quick checks, and confirm with an extrinsic test before you choose an embedding for a product.
Wait: that line quotes a block, which is not allowed. The correct form is below.
Limitations: polysemy, unknown words and bias
- Polysemy. Each word gets one vector, so "bank" as a river edge and "bank" as a lender share a single point.
- Unknown words. A word missing from the vocabulary has no vector. Both models fail on it unless you handle it, for example by mapping it to a fallback token.
- Bias. Vectors learn the associations present in their training text, including stereotypes.
Exercise
Why does the nearest-neighbour list for "bank" mix river and money senses?
Show answer
Each word has one vector, so the river and money senses share one point in the space. The neighbours reflect both senses at once.