Before transformers there was word2vec. How do CBOW and skip-gram learn embeddings from raw text, and why does negative sampling make the training feasible?

The Problem: A search team at an app store needs 300-dimensional word vectors from 1 billion review tokens over a 200,000-word vocabulary, retrained every week on CPUs. A full softmax would score all 200,000 words for each of about 6 billion training pairs per epoch, rare terms such as “haptics” occur only 40 times, and the team must choose CBOW or skip-gram plus an output layer.
Answer
Word2vec slides a window over raw text and trains a shallow two-matrix model to predict words from their neighbors, so words that share contexts end up with similar vectors. CBOW averages the context vectors and predicts the center word, which is fast but blurs rare words into the average. Skip-gram predicts each context word from the center word, which yields more training pairs per token and better vectors for rare words. The bottleneck is the output layer, because a full softmax must score all vocabulary words for every pair. Negative sampling replaces that normalization with
binary logistic tasks that separate the true pair from
noise words, cutting the per-pair cost from
to
. Together with subsampling of frequent words, this makes training on billions of tokens feasible on ordinary CPUs.
(1) Self-Supervised Windows: Every corpus position with window radius yields (center, context) examples for free, so no labels are needed and the distributional hypothesis does the teaching.
(2) CBOW Averages, Skip-Gram Fans Out: CBOW makes one prediction per window from the mean of context vectors. Skip-gram makes up to
separate predictions, several times slower but giving every rare occurrence its own gradient.
(3) The Softmax Bottleneck: Normalizing over the vocabulary costs per pair and the gradient touches all
output vectors, about
multiply-adds at
and
.
(4) Hierarchical Softmax: A binary Huffman tree over the vocabulary turns one prediction into about sigmoid decisions, roughly 18 for 200,000 words and fewer for frequent words with short codes.
(5) Negative Sampling: Push the true pair’s score up and noise pairs’ scores down with independent sigmoids, drawing noise from
. Values of
from 5 to 20 suit small corpora and 2 to 5 suit large ones.
(6) Subsampling Frequent Words: Discarding tokens of “the” or “a” with probability speeds training 2 to 10 times and sharpens rare-word vectors by widening the effective window.
(7) Linear Geometry: The input vectors become the embeddings, and because the skip-gram negative-sampling optimum is a shifted PMI factorization, consistent relations appear as directions, as in .
The two architectures read the same window in opposite directions. In “a hot foamy latte with cinnamon” with , CBOW forms one example: average the input vectors of hot, foamy, with, and cinnamon, then predict latte. Skip-gram forms four examples, (latte, hot), (latte, foamy), (latte, with), and (latte, cinnamon), and
receives a separate gradient from each. CBOW’s averaging is a smoothing step, so frequent words get stable vectors quickly and an epoch is several times cheaper. A rare word, though, only ever appears as one of
averaged inputs or as a target predicted from common context. Skip-gram spends more compute per token, yet every occurrence of a rare word is a full training signal for its own vector, which is why the original word2vec tool from Google recommends it for infrequent words and windows around 10.

Figure 1: The same window with read two ways. CBOW averages four context vectors into
and makes one prediction of the center word, while skip-gram turns the window into four (center, context) pairs, each trained as its own negative-sampling task.
| Property | CBOW | Skip-gram |
|---|---|---|
| Prediction direction | Averaged context predicts the center word | Center word predicts each context word separately |
| Examples per token | 1 | Up to 2c, about c + 1 on average with dynamic windows |
| Training speed | Several times faster per epoch | Slower, roughly proportional to the window size |
| Rare words | Diluted in the context average and rarely a target | Every occurrence updates its own vector directly |
| Analogy accuracy (Mikolov et al., 640-d) | About 64% syntactic, 24% semantic | About 59% syntactic, 55% semantic |
| Recommended window | Around 5 | Around 10 |
| Good default for | Very large corpora, frequent-word quality, fast iteration | Smaller corpora, rare words, semantic similarity |
The output layer decides feasibility. The model keeps two matrices, and
, each of shape
, and the input side is a single row lookup. A full softmax needs
dot products for its normalizer and produces a dense gradient over all of
, which at the sizes above is
multiply-adds for one pair. Hierarchical softmax keeps a properly normalized distribution by factorizing it along a Huffman tree at about
cost. Negative sampling drops normalization altogether. It never estimates
; instead it trains a classifier that separates observed pairs from noise pairs, which is all an embedding needs. Each update touches one input row and
output rows, so lock-free parallel SGD across CPU threads rarely collides.
Mathematical Formulation:
Where:
is the skip-gram objective, an average over
corpus positions
;
is the maximum window radius (resampled uniformly from 1 to
at each position in the reference implementation) and
is the offset to a context word
.
is the full softmax probability of context word
given center word
, and its denominator sums over all
vocabulary words, which is the
bottleneck.
is the input vector of the center word (a row of
) and
the output vector of a context word (a row of
), with
typically 100 to 300.
is CBOW’s averaged context vector, which takes the place of
in both the softmax and the negative-sampling loss, with the center word
as the target.
is the per-pair negative-sampling loss,
is the logistic function, and
is the number of noise words
drawn for each true pair.
is the noise distribution, the unigram frequency
raised to the power 3/4 and renormalized, which flattens the head of the distribution toward rarer words.
is the probability of discarding a token before windowing, with threshold
; words with
are never dropped.
Negative sampling is easiest to understand through its gradient. Writing for the true pair and
for each noise pair, differentiating
gives three sparse updates.
Negative-Sampling Gradients:
Take and one skip-gram pair (latte, espresso) with a single noise word, tractor, so
. Let
for latte,
for espresso,
for tractor, and use a learning rate of 0.2.
Worked Example:
One step moved latte toward espresso and away from tractor, and the pair loss fell from 1.45 to about 1.34. The two scalars are the error signals. measures how far the true pair is from being classified as real, and
measures how strongly the noise pair is mistaken for real. Both shrink as training succeeds, so easy pairs stop producing updates. The output vectors move in the same step, computed from the old
: espresso gains
and tractor loses
.

Figure 2: (a) Output-layer work for one pair at and
on a log scale: the full softmax needs 60 million multiply-adds, hierarchical softmax about 5,400, and negative sampling 1,800 at
. (b) The worked example: one step moves
toward espresso and away from tractor, raising the true score from 0.50 to 0.58 and lowering the noise score to 0.38.
The remaining knobs have predictable effects. A small window of 2 favors syntactic and functional similarity, so the neighbors of “walking” are other gerunds, while a window of 10 favors topical relatedness, so “espresso” lands near “barista”. Dimensions beyond about 300 rarely improve analogy accuracy without more data, and a min_count of 5 discards words too rare to estimate at all. The famous analogy is a cosine nearest-neighbor search for with the three query words excluded. Without that exclusion the answer to the king, man, woman query is usually king itself, a detail that makes the linear structure look cleaner than it is.
Word2vec also opens a lineage: word2vec → GloVe → fastText → ELMo → BERT. GloVe fits the same global co-occurrence statistics directly with weighted least squares, and fastText builds each vector from character n-grams so misspelled or unseen words still get one. The decisive limit is that word2vec stores one static vector per word type, so “bank” gets the same vector beside “river” and beside “loan”. ELMo and BERT fixed that with contextual embeddings computed per occurrence, and BERT’s masked-language-model objective is essentially CBOW made deep, predicting a hidden token from its full bidirectional context. Modern LLMs such as Llama 3 still start from a word2vec-style lookup table but pay the full softmax over a 128,256-token vocabulary, because GPU matrix multiplies make affordable and generation needs a normalized distribution.
Leave a Reply