Explaining Word2Vec Skip-gram and Negative Sampling
The Word2Vec skip-gram model learns word embeddings by predicting context words given a center word. It models the probability of a context word given the center as a softmax over the entire vocabulary, which requires computing exponentials and sums for all words—this becomes prohibitively expensive with large vocabularies.
Negative sampling simplifies training by reframing the prediction as a binary classification task that distinguishes real context pairs from randomly sampled “negative” word pairs. Instead of updating weights for the whole vocabulary, it updates only for the positive pair and a small number of negative samples, reducing the computational complexity from O(V) to O(k), where k is the number of negatives per example.
Formally, skip-gram with softmax models
[
p(c \mid w) = \frac{\exp(\mathbf{u}_c^\top \mathbf{v}w)}{\sum{v=1}^V \exp(\mathbf{u}_v^\top \mathbf{v}_w)}
]
where (\mathbf{v}_w) and (\mathbf{u}_c) are the input and output embeddings, and (V) is vocabulary size.
Negative sampling instead maximizes the objective
[
\log \sigma(\mathbf{u}c^\top \mathbf{v}w) + \sum{i=1}^k \mathbb{E}{n_i \sim P_n} \left[\log \sigma(-\mathbf{u}_{n_i}^\top \mathbf{v}_w)\right]
]
where (\sigma) is the sigmoid function, and the (n_i) are negative samples drawn from a noise distribution (P_n).
This approach avoids the costly softmax denominator and corresponding gradient updates for the entire vocabulary, making Word2Vec scalable to very large vocabularies.
In Mikolov et al.'s Word2Vec there are two closely related training objectives:
- In the continuous bag–of–words (CBOW) model you look at the words around a position and try to predict the missing centre word.
- In the skip–gram model you do the opposite: for each position (t) in a corpus you take the centre word (w_t) and try to make the model assign high probability to the words in a fixed window around it.
If you write that out probabilistically for skip–gram with a symmetric window of size (c), the training criterion is
[
\max_\theta \sum_{t=1}^T \sum_{-c\leq j\leq c,, j\neq 0} \log p_\theta(w_{t+j}\mid w_t),
]
where (v_w) and (u_w) are “input” and “output” vectors for each vocabulary item and a natural choice for (p) is a softmax over the output vectors:
[
p(w_o \mid w_i) = \frac{\exp(u_{w_o}^\top v_{w_i})}{\sum_{w' \in V} \exp(u_{w'}^\top v_{w_i})}.
]
The problem with this formulation on a realistic vocabulary (V) is the normalizing denominator: for every training pair ((w_i,w_o)) you have to compute a dot product and an exponential for every word in the vocabulary to get the probability and its gradient. With a million–word vocabulary and billions of training pairs, that (O(\lvert V\rvert)) cost per update is prohibitive.
Negative sampling replaces the multiclass softmax with a set of binary classification problems. Instead of normalising over all possible (w_o), you treat the observed pair ((w_i,w_o)) as a positive example and draw (k) “noise” words (n_1,\dots,n_k) from some simple distribution as negatives. The model is trained to assign a high sigmoid score (\sigma(u_{w_o}^\top v_{w_i})) to the positive pair and low scores (\sigma(u_{n_j}^\top v_{w_i})) to the negative pairs. The corresponding loss for one centre–context pair is
[
\log \sigma(u_{w_o}^\top v_{w_i}) + \sum_{j=1}^k \log \sigma(-u_{n_j}^\top v_{w_i}).
]
Because you only ever touch the parameters for the centre word, the true context word, and a handful of sampled negatives, each update costs (O(k)) instead of (O(\lvert V\rvert)). Empirically, choosing (k) on the order of 5–20 is enough to learn useful embeddings, which makes the training dramatically faster while still pushing vectors of words that co‑occur together and apart those that do not.