This is a great question about Word2Vec skip-gram with negative sampling. Let me break this down comprehensively.
Word2Vec Skip-Gram Overview
Word2Vec skip-gram is a method for learning word embeddings. The basic idea is:
Given a target word, predict the surrounding context words (within a window)
Train dense vector representations of words that are good at this prediction task
The Original Problem with Softmax
In the original skip-gram model, for each target word, you want to predict context words. The probability of a context word given the target word is modeled with softmax:
The first term pushes the positive pair's score higher
The second term pushes negative samples' scores lower
P_noise is typically the unigram distribution raised to the 3/4 power
Why This Works
Negative sampling reduces complexity from O(V) to O(k) where k is typically 5-20, making training feasible for large vocabularies. It approximates the original softmax objective while providing a stronger learning signal by forcing the model to distinguish real word pairs from noise. This approach has solid theoretical grounding in the work showing that negative sampling approximates the softmax objective.
Mathematical Details
For a word w_t at position t, the skip-gram objective with negative sampling is:
L = Σ_{-m ≤ j ≤ m, j ≠ 0} [log σ(v'{w{t+j}} · v_{w_t}) + Σ_{i=1}^k E_{w~P_n}[log σ(-v'w · v{w_t})]]
Where m is the window size.
Why the 0.75 power for negative sampling distribution?
The negative sampling distribution uses P_n(w) ∝ freq(w)^0.75 rather than proportional probability or uniform sampling. This exponent balances between oversampling rare words and undersampling frequent ones, ensuring rare words receive sufficient training signal while keeping common words from dominating the learning process.
Word2Vec Skip-Gram with Negative Sampling
The Core Idea
Skip-gram objective: Given a target word, predict its context words.
Computing this requires normalizing over all V words
With billions of training pairs, this becomes prohibitively expensive
Gradient computation: O(V) per example
Negative Sampling: The Solution
Instead of normalizing over all words, use binary classification on sampled pairs:
Replace softmax with binary problems
For each positive (target, context) pair:
Positive example: (w_target, w_context) → label 1
k negative examples: (w_target, random_word) → label 0
Each trained as a binary logistic regression:
code
L = log σ(v'_context · v_target) + Σ_{i=1}^k E_w~P_noise[log σ(-v'_w · v_target)]
└─ maximize score for real pair ┘ └─ minimize score for k random words ┘
Where:
σ(x) = 1/(1 + e^-x) is sigmoid
P_noise(w) ∝ freq(w)^0.75 (unigram distribution to 3/4 power)
Complexity Comparison
Approach
Cost per Example
Softmax
O(V) - compute scores for all words
Negative Sampling
O(k) - compute scores for ~1 positive + k negatives
Typical: k = 5-20, V = 100K+ 100x-1000x speedup!
Code Example
python
import numpy as np
def negative_sampling_loss(target_vec, context_vec, negative_vecs, k=5):
"""
Args:
target_vec: embedding of target word [d]
context_vec: embedding of context word [d]
negative_vecs: k sampled negative words [k, d]
"""
# Positive example: (target, actual context) → score should be high
pos_score = np.dot(target_vec, context_vec)
pos_loss = -np.log(sigmoid(pos_score))
# Negative examples: (target, random words) → scores should be low
neg_scores = np.dot(negative_vecs, target_vec) # [k]
neg_loss = -np.sum(np.log(sigmoid(-neg_scores))) # flip sign to minimize
return pos_loss + neg_loss
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
# During training:
# 1. Sample a batch of (target, context) pairs from corpus
# 2. For each pair:
# - Sample k negative words using P(w) ∝ freq(w)^0.75
# - Compute loss above
# - Update embeddings via SGD
Why This Works: Theoretical Justification
Negative sampling approximates the softmax through:
Probabilistic interpretation:
The k negative samples form a low-rank approximation of the full softmax partition function
As k increases, it converges toward full softmax while remaining tractable
Empirical finding by Mikolov et al.:
With noise distribution P_n(w) = freq(w)^0.75:
Not inverse frequency (0.5): rare words too common
Not uniform (1.0): common words get ignored
0.75 balances both: rare words get more signal
Key Advantages Summary
Advantage
Explanation
Speed
O(k) vs O(V)
Scalability
Works with million-word vocabularies
Learning signal
Forces model to distinguish target from noise
Simplicity
Just binary classification, not complex normalization
Trade-off: Small approximation error vs massive computational gain (almost always worth it!)