Word Embeddings

GATE Relevance: Medium

Prerequisites

🎯 Learning Objectives

  • Understand why dense embeddings are superior to sparse one-hot vectors
  • Explain the Word2Vec CBOW and Skip-gram architectures
  • Describe GloVe’s co-occurrence matrix approach
  • Perform vector arithmetic (king − man + woman ≈ queen)
  • Choose appropriate embedding dimensionality for a task

1. From Sparse to Dense Representations

In Bag of Words and TF-IDF, each word is represented as a sparse vector of dimension |V| (vocabulary size, often 50,000–100,000+). This has major drawbacks:

  • High dimensionality: Vectors are huge and mostly zeros.
  • No semantic similarity: The cosine similarity between any two one-hot vectors is 0. “king” and “queen” are as different as “king” and “pizza”.
  • No generalization: The model cannot leverage similarity between words to generalize.
Definition — Word Embedding: A mapping W: V → ℝd that assigns each word in vocabulary V a dense, low-dimensional vector (typically d = 50–300). Semantically similar words have similar embedding vectors.

The distributional hypothesis (Firth, 1957) underpins all embedding methods: “You shall know a word by the company it keeps.” Words appearing in similar contexts have similar meanings and should have similar vectors.

2. Word2Vec

Word2Vec (Mikolov et al., 2013) learns embeddings by training a shallow neural network on a word prediction task. There are two architectures:

2.1 Continuous Bag of Words (CBOW)

CBOW: Predicts the center word given its surrounding context words within a window of size c.

Architecture:

  1. Input: One-hot vectors of context words wt−c, …, wt−1, wt+1, …, wt+c
  2. Embedding layer: Look up each context word’s embedding and compute their average: h = (1/2c) ∑i vwi
  3. Output layer: Linear projection + softmax over vocabulary to predict center word wt
  4. Loss: Cross-entropy between predicted distribution and true one-hot target

2.2 Skip-gram

Skip-gram: Predicts the context words given the center word. The inverse of CBOW.

Architecture:

  1. Input: One-hot vector of center word wt
  2. Embedding layer: Look up the center word’s embedding vwt
  3. Output layer: For each context position, produce a probability distribution over V
  4. Training objective: Maximize ∑j log P(wt+j | wt) for j in window

Example: Skip-gram Training

Sentence: “the quick brown fox jumps” with window size c = 2

Center word: “brown” (position 3)

Training pairs: (brown, the), (brown, quick), (brown, fox), (brown, jumps)

The model learns to maximize P(the|brown), P(quick|brown), P(fox|brown), P(jumps|brown).

2.3 Training Efficiency: Negative Sampling

Computing the full softmax over a vocabulary of |V| = 100,000 words is expensive. Negative Sampling approximates the objective:

Instead of computing the full softmax, for each positive pair (wt, wc), sample k negative words wneg that do not appear in the context. The objective becomes:

L = log σ(vwc · vwt) + ∑i=1k log σ(−vwneg,i · vwt)

where σ is the sigmoid function. Typically k = 5–20 for small datasets, k = 2–5 for large ones.

3. GloVe (Global Vectors)

GloVe: Learns embeddings from the global word-word co-occurrence matrix X, where Xij counts how often word i appears in the context of word j across the entire corpus.

Objective function:

J = ∑i,j=1|V| f(Xij) (wiTw̃j + bi + b̃j − log Xij)²

where f(Xij) is a weighting function that caps at a maximum value (typically xmax = 100) to prevent extremely frequent pairs from dominating:

f(x) = (x / xmax)α if x < xmax, else 1   (α = 0.75 typically)

Key insight: GloVe combines the benefits of count-based methods (like LSA) with prediction-based methods (like Word2Vec). It leverages global statistics while learning meaningful embeddings.

4. Embedding Properties & Vector Arithmetic

Well-trained embeddings capture semantic and syntactic regularities as linear relationships in the vector space:

Famous Example: King − Man + Woman ≈ Queen

v(king) − v(man) + v(woman) ≈ v(queen)

The vector difference v(king) − v(man) captures the concept of “royalty” independent of gender. Adding v(woman) to this “royalty” vector points near “queen”.

Other learned relationships:

RelationshipExampleVector Operation
Genderman:woman :: king:queenking − man + woman ≈ queen
Country → CapitalFrance:Paris :: Japan:TokyoJapan − France + Paris ≈ Tokyo
Tensewalk:walked :: swim:swamswim − walk + walked ≈ swam
Comparativebig:bigger :: small:smallersmall − big + bigger ≈ smaller

Similarity measurement: Cosine similarity is the standard metric:

cos(θ) = (a · b) / (‖a‖ · ‖b‖)

Values range from −1 (opposite) to +1 (identical direction). Words like “cat” and “dog” typically have high cosine similarity (~0.7–0.8).

5. Embedding Dimensionality

The embedding dimension d is a key hyperparameter:

DimensionTypical UseTrade-off
50Small datasets, quick experimentsLess capacity but fast training
100–200General NLP tasksGood balance
300Pre-trained Word2Vec/GloVe standardRich representations
768–1024Contextual embeddings (BERT, GPT)Highest capacity but expensive

Rule of thumb: Performance typically improves with dimension up to a point, then marginal gains diminish. For most tasks, d = 100–300 works well.

6. Word2Vec vs GloVe Comparison

FeatureWord2VecGloVe
MethodPredictive (neural network)Count-based + regression
TrainingSliding window over corpusGlobal co-occurrence matrix
ObjectiveMaximize context word probabilityMinimize weighted least squares
SpeedFaster for small corporaBetter for large corpora (one-time matrix)
PerformanceSimilar on analogy tasksSimilar on analogy tasks

7. Limitations of Static Embeddings

  • Polysemy: Each word gets one vector regardless of context. “bank” (financial) and “bank” (river) share the same embedding.
  • Out-of-vocabulary (OOV): Words not seen during training have no embedding.
  • Bias: Embeddings encode societal biases present in training data (e.g., gender stereotypes).

These limitations motivated contextual embeddings like ELMo, BERT, and GPT, where a word’s representation depends on its surrounding context.

8. Summary

  • Word embeddings map words to dense vectors in ℝd where similar words are nearby.
  • Word2Vec CBOW: context → center word; Skip-gram: center word → context.
  • GloVe: factorizes the global co-occurrence matrix.
  • Vector arithmetic captures semantic relationships (king − man + woman ≈ queen).
  • Cosine similarity measures word similarity: cos(θ) = (a·b)/(‖a‖·‖b‖).
  • Typical dimension: 100–300 for static embeddings.
  • Limitation: static (one vector per word) — solved by contextual embeddings (BERT, GPT).