GloVe Embeddings

How can we learn word embeddings that effectively combine local context with global co-occurrence statistics? GloVe embeddings address this by integrating both local window-based information and overall corpus statistics. Traditional methods like Word2Vec excel at capturing local context through skip-gram or CBOW models, while techniques such as Latent Semantic Analysis (LSA) focus on global patterns. GloVe synthesizes these two perspectives, aiming for richer semantic representations that leverage the strengths of both.

The Power of Co-occurrence Counts

Co-occurrence Probability
The co-occurrence probability P(ji)P(j|i) is the likelihood that word jj appears in the context of word ii within a specified window size across a corpus. These probabilities are derived from a co-occurrence matrix, which tabulates how often words appear together.
Example: In the sentence "The quick brown fox jumps over the lazy dog," with a context window of 2, the word "quick" co-occurs with "The" and "brown." If "quick" appears 100 times in the corpus and 80 of those times "brown" is within its 2-word window, then P(brownquick)P(\text{brown}|\text{quick}) would be 0.80.8 (simplified).

The core intuition behind GloVe is that ratios of co-occurrence probabilities carry more semantic meaning than the probabilities themselves. Consider two words, 'ice' and 'steam,' and a set of probe words like 'solid,' 'gas,' and 'water.' The raw probability P(solidice)P(\text{solid}|\text{ice}) might be high, and P(gassteam)P(\text{gas}|\text{steam}) also high. However, comparing P(solidice)P(\text{solid}|\text{ice}) to P(solidsteam)P(\text{solid}|\text{steam}) reveals a stark difference.

This ratio, P(solidice)/P(solidsteam)P(\text{solid}|\text{ice}) / P(\text{solid}|\text{steam}), would be very large, indicating 'solid' is strongly associated with 'ice' but not 'steam.' Conversely, P(gasice)/P(gassteam)P(\text{gas}|\text{ice}) / P(\text{gas}|\text{steam}) would be very small. For a word like 'water,' which relates to both, the ratio P(waterice)/P(watersteam)P(\text{water}|\text{ice}) / P(\text{water}|\text{steam}) would be close to 1. These ratios effectively encode the semantic relationships between words.

Co-occurrence Probability Ratios for 'Ice' vs. 'Steam'
Probe Word (k)P(kice)P(k|\text{ice})P(ksteam)P(k|\text{steam})Ratio P(kice)/P(ksteam)P(k|\text{ice}) / P(k|\text{steam})
solid0.850.0328.33
gas0.020.780.03
water0.600.551.09
fashion0.010.011.00
This table illustrates how co-occurrence probability ratios reveal semantic distinctions. A high ratio (e.g., for 'solid') indicates a strong association with the numerator word ('ice'). A low ratio (e.g., for 'gas') indicates a strong association with the denominator word ('steam'). A ratio near 1 (e.g., for 'water' or 'fashion') suggests a balanced or unrelated association.
Check Your Understanding
If the ratio P(kWord X)/P(kWord Y)P(\text{k}|\text{Word X}) / P(\text{k}|\text{Word Y}) is very close to zero, what does it imply about Word X and Word Y relative to probe word k?

The GloVe Objective Function

📐 GloVe Objective Function

The GloVe model minimizes the following cost function:

i,j=1Vf(Xij)(wiTw~j+bi+b~jlogXij)2\sum_{i,j=1}^{V} f(X_{ij}) (w_i^T \tilde{w}_j + b_i + \tilde{b}_j - \log X_{ij})^2

Where:
- VV is the size of the vocabulary.
- XijX_{ij} is the number of times word ii and word jj co-occur.
- wiw_i and w~j\tilde{w}_j are the word vectors for word ii and context word jj, respectively.
- bib_i and b~j\tilde{b}_j are the bias terms for word ii and context word jj.
- f(Xij)f(X_{ij}) is a weighting function that gives less weight to very rare or very frequent co-occurrences.

The GloVe objective function is designed to learn word vectors such that their dot product effectively predicts the logarithm of their co-occurrence probability. The term wiTw~jw_i^T \tilde{w}_j represents the dot product between the word vector wiw_i and the context word vector w~j\tilde{w}_j. This dot product is intended to approximate logP(ji)\log P(j|i), which is related to logXij\log X_{ij}.

Bias terms, bib_i and b~j\tilde{b}_j, are included to account for inherent biases of words, independent of their specific context. For instance, some words might be generally more frequent or appear in more diverse contexts. The weighting function f(Xij)f(X_{ij}) is crucial; it assigns higher weights to meaningful co-occurrences while down-weighting very rare pairs (which might be noise) and very frequent pairs (which might not carry much specific information, like stop words). This function typically increases monotonically but saturates for large XijX_{ij} values.

GloVe vs. Word2Vec: A Head-to-Head

Comparing GloVe and Word2Vec
FeatureWord2VecGloVe
Input/Data SourceLocal context windows (sentences)Global co-occurrence matrix
Model TypePredictive (neural network based)Count-based (matrix factorization)
Information CapturedLocal syntactic and semantic relationshipsGlobal semantic relationships via co-occurrence ratios
Training MechanismTrains on individual word pairs/windows using SGDTrains on non-zero entries of co-occurrence matrix using AdaGrad
StrengthsCaptures nuanced local context, computationally efficient for large corporaLeverages global statistics, often better for analogy tasks, faster training on smaller corpora
WeaknessesDoes not explicitly use global statistics, can be slower for very large vocabulariesRequires building a co-occurrence matrix first, which can be memory-intensive for huge vocabularies
GloVe and Word2Vec represent different philosophies in learning word embeddings, each with distinct advantages depending on the corpus size and specific task.
Check Your Understanding
Which embedding method would likely be more efficient for a very large corpus where global co-occurrence statistics are paramount for capturing nuanced semantic relationships?

Putting GloVe to Work

pythonLoading and Using Pre-trained GloVe Embeddings
Try It Yourself
Using the loaded GloVe model, find the top 3 most similar words to 'computer' and then perform the analogy 'Paris - France + Italy = ?' (expecting a city).
python
This code uses a deep-learning or other compiled library (e.g. PyTorch, TensorFlow, Keras, spaCy, Transformers) that has no in-browser build, so it can't run here. Google Colab has these preinstalled — with a free GPU — so you can run and experiment there for free.
Expected
You should see the top 3 similar words to 'computer' (e.g., 'software', 'technology', 'internet') and the analogy result 'Rome' (or a similar city).
Key Takeaways
  • GloVe learns word embeddings by leveraging global word-word co-occurrence statistics.

  • The core idea is that ratios of co-occurrence probabilities encode semantic relationships more effectively than raw probabilities.

  • The GloVe objective function minimizes the squared difference between the dot product of word vectors and the logarithm of their co-occurrence count, incorporating bias terms and a weighting function.

  • GloVe combines the benefits of local context (like Word2Vec) with global statistical information (like LSA) into a single model.

  • Compared to Word2Vec, GloVe is a count-based model that can be more efficient for certain tasks and often performs well on analogy tasks.

  • Pre-trained GloVe embeddings are readily available and can be used to find word similarities and perform semantic analogies, directly addressing how to combine local context with global co-occurrence statistics for rich word representations.

← All lessons in Word Embeddings

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free