Word Embeddings
Prerequisites
- Text Preprocessing — tokenization, BoW, TF-IDF
- Neural Network Basics — forward pass, loss functions
🎯 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.
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)
Architecture:
- Input: One-hot vectors of context words wt−c, …, wt−1, wt+1, …, wt+c
- Embedding layer: Look up each context word’s embedding and compute their average: h = (1/2c) ∑i vwi
- Output layer: Linear projection + softmax over vocabulary to predict center word wt
- Loss: Cross-entropy between predicted distribution and true one-hot target
2.2 Skip-gram
Architecture:
- Input: One-hot vector of center word wt
- Embedding layer: Look up the center word’s embedding vwt
- Output layer: For each context position, produce a probability distribution over V
- 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)
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:
| Relationship | Example | Vector Operation |
|---|---|---|
| Gender | man:woman :: king:queen | king − man + woman ≈ queen |
| Country → Capital | France:Paris :: Japan:Tokyo | Japan − France + Paris ≈ Tokyo |
| Tense | walk:walked :: swim:swam | swim − walk + walked ≈ swam |
| Comparative | big:bigger :: small:smaller | small − 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:
| Dimension | Typical Use | Trade-off |
|---|---|---|
| 50 | Small datasets, quick experiments | Less capacity but fast training |
| 100–200 | General NLP tasks | Good balance |
| 300 | Pre-trained Word2Vec/GloVe standard | Rich representations |
| 768–1024 | Contextual 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
| Feature | Word2Vec | GloVe |
|---|---|---|
| Method | Predictive (neural network) | Count-based + regression |
| Training | Sliding window over corpus | Global co-occurrence matrix |
| Objective | Maximize context word probability | Minimize weighted least squares |
| Speed | Faster for small corpora | Better for large corpora (one-time matrix) |
| Performance | Similar on analogy tasks | Similar 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).