Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

~6 min readMikolov et al. · 2013

Word2Vec,
explained.

To a computer, a word is just an arbitrary symbol — "cat" and "dog" are no more related than "cat" and "thermodynamics." This 2013 paper taught machines to place every word on a map of meaning, where nearness means relatedness and, astonishingly, you can do arithmetic on ideas: king − man + woman lands right next to queen.

The delta

What changes here: a word stops being an index

Rung 1, so the comparison is against how text was represented before. A word was a slot — one dimension in a vocabulary-sized vector, on or off. Under that representation every pair of distinct words is equally dissimilar: the distance from "cat" to "dog" is the distance from "cat" to "bureaucracy". Any notion of similarity had to come from a hand-built resource like a thesaurus.

The change is to learn a dense vector per word from context alone, on the hypothesis that words appearing in similar company mean similar things. Train a tiny model to predict a word's neighbours from the word, or the word from its neighbours, and throw the model away — the weights are the product. Similarity becomes geometry, and it is computed rather than curated.

Two things to carry up the ladder. First the scale trick that made it practical: predicting over a full vocabulary with a softmax is prohibitive, so the objective is reframed as telling real context words apart from a handful of randomly-drawn fake ones — negative sampling, which turns one expensive normalisation into a few cheap binary decisions and reappears at rungs 3 and 6. Second the result that made it famous: the space has linear structure, so vector arithmetic on king − man + woman lands near queen. Nothing in the objective asked for that, and it is the first evidence on this ladder that a learned representation can carry relations it was never taught.

01

Words as arbitrary symbols

Early NLP treated each word as a one-hot vector — a giant list of zeros with a single 1 marking that word's slot. In that scheme every word is exactly as far from every other; "Paris" and "France" are as unrelated as "Paris" and "banana." All the meaning is thrown away.

What you really want is a representation where similar words sit near each other — where the geometry itself carries meaning. That's a dense embedding: a few hundred real numbers per word instead of a lonely 1 in a sea of zeros.

02

A word is known by the company it keeps

Word2Vec's foundation is an old linguistic idea, the distributional hypothesis: words that show up in similar contexts tend to mean similar things. "Coffee" and "tea" appear around "drink," "cup," "hot," "morning" — so if you learn to predict context, words with similar contexts are pushed toward similar vectors.

The genius was making this cheap. Word2Vec is a tiny, shallow network — no deep stacks — trained on billions of words fast enough to actually run at scale.

03

Skip-gram and CBOW

The paper offers two mirror-image training games. CBOW (Continuous Bag of Words) hides the middle word and predicts it from the words around it. Skip-gram flips it: given the middle word, predict the words likely to surround it.

Nobody actually cares about these predictions. They're a pretext. The real prize is a by-product: to predict context well, the network is forced to arrange word vectors so that similar words cluster — and those vectors are what you keep. CBOW is faster; skip-gram shines on rare words and huge corpora.

The first paper got to billions of words with a hierarchical softmax; the follow-up's negative sampling is the cheaper trick most people remember. Rather than a full softmax over the whole vocabulary every step, it turns training into a cheap yes/no game: push a word's vector toward its real neighbors and away from a handful of k random impostors:

Real (word, context) pairs get their dot product raised; k sampled non-neighbors get theirs lowered. That's the whole training signal.

Do that across a corpus and a geometry emerges where direction encodes meaning — which is what lets you do arithmetic on words. The runnable version below shows that arithmetic resolving analogies by cosine nearest-neighbor.

04

Arithmetic on meaning

The result that made the field gasp: the vectors captured relationships as consistent directions. The step from "man" to "woman" is roughly the same vector as "king" to "queen" — a gender direction. So you can literally compute:

Analogy as vector arithmetic
king−man+woman≈queen

"Paris − France + Italy" lands near "Rome." These analogies weren't programmed in — they emerged from nothing but learning to predict nearby words. Meaning had genuine mathematical structure.

Worth knowing

This is why "similarity = distance" became the mental model for all of modern retrieval. Cosine similarity between embeddings — the beating heart of semantic search and RAG — is this idea, grown up.

06

Why it still matters

Every embedding model owes Word2Vec its core premise: meaning can be a vector, and similarity can be distance. Modern systems use contextual embeddings from Transformers — where a word's vector shifts with its sentence, so "bank" by a river differs from "bank" with money — rather than one fixed vector per word. But the leap Word2Vec made is the one everything stands on.

When you build semantic search, a recommender, or a RAG retriever, you're placing things on a map of meaning and measuring nearness. That map was first drawn here.

How it works

How word2vec trains, step by step

Word2vec is two papers. The first (Mikolov, Chen, Corrado and Dean, January 2013) proposed the CBOW and skip-gram architectures, made them cheap with a hierarchical softmax, and introduced a 19,544-question analogy test. The second (Mikolov, Sutskever, Chen, Corrado and Dean, NIPS 2013) replaced the hierarchical softmax with negative sampling, added subsampling of frequent words, and extended the model to phrases. The procedure below is the combined recipe that the released word2vec tool implements.

window: up to c = 2 words each side we drink hot tea each morning (tea, drink) (tea, hot) (tea, each) (tea, morning) positive pairs: push σ(v′·v) toward 1 noise ∝ count^0.75 k draws per positive pair (tea, piano) (tea, of) negative pairs: push σ(v′·v) toward 0
Skip-gram on one window: every context word inside the dashed window forms a positive pair with the centre word, and k words drawn from the smoothed unigram distribution form negative pairs.
  1. Build the vocabulary. Count every token and drop rare ones; the second paper discarded words seen fewer than five times, which left 692,000 words from a one-billion-word news corpus. Each surviving word gets two vectors: an input vector vw, which is the one you keep, and an output vector v′w, used only during training.
  2. Slide a window. At each position the centre word is paired with words up to c positions either side. Skip-gram does not use a fixed width: for every centre word it draws R uniformly from 1 to c and takes R words on each side, so near neighbours are sampled more often than distant ones. The first paper used c = 10 for skip-gram and four words either side for CBOW.
  3. Pick the prediction. CBOW averages the input vectors of the context words and predicts the centre word. Skip-gram uses the centre word's vector to predict each context word separately, so one position yields 2R training pairs. Both models are log-linear: no hidden non-linearity, just a dot product between an input vector and an output vector.
  4. Avoid the full softmax. Normalising over the whole vocabulary on every pair is the expensive part, and the two papers solve it differently. The first uses a hierarchical softmax: the vocabulary becomes a binary Huffman tree built from word counts, each inner node has an output vector, and a word's probability is the product of left-or-right sigmoid decisions along its path. A balanced tree over a million words needs about 20 decisions; the Huffman tree roughly halves that on average because frequent words sit near the root. The second paper uses negative sampling: score the true context word against k noise words drawn from the unigram distribution raised to the 3/4 power, with k from 5 to 20 on small corpora and 2 to 5 on large ones.
  5. Subsample frequent words. Each occurrence of word w is discarded with probability 1 − √(t / f(w)), where f(w) is its share of the corpus and t is around 10^−5. Words rarer than t are always kept, while a word as common as the loses almost every occurrence. In the released code the discarded words are removed before windowing, so the effective window widens as well.
  6. Train with stochastic gradient descent. The first paper's smaller comparisons used three epochs of stochastic gradient descent with a learning rate that starts at 0.025 and decays linearly to zero. Its largest runs, on a Google News corpus of about six billion tokens with the vocabulary capped at the million most frequent words, were spread across a cluster with the DistBelief framework, asynchronous mini-batch updates and Adagrad: the 1,000-dimensional skip-gram model took roughly two and a half days on 125 CPU cores.
  7. Keep the input vectors and query them. The output vectors are thrown away. Similarity is cosine between input vectors. An analogy a : b :: c : ? is answered by computing vb − va + vc and returning the nearest word by cosine, excluding the three question words, and it counts only if that word is exactly the expected one — synonyms are scored as errors.

Phrases

The second paper also learns vectors for phrases such as "New York Times". A bigram is merged into one token when (count(wiwj) − δ) / (count(wi) × count(wj)) exceeds a threshold, where the discount δ stops pairs of rare words from qualifying. Two to four passes with a falling threshold build longer phrases, which are then trained exactly like words.

The math

The skip-gram objective and the negative-sampling shortcut

J = (1/T) Σt Σ−c ≤ j ≤ c, j ≠ 0 log p(wt+j | wt)
p(wO | wI) = exp(v′O · vI) / Σw=1…W exp(v′w · vI)
NEG: log σ(v′O · vI) + Σi=1…k Ewi ∼ Pn [ log σ(−v′i · vI) ], Pn(w) ∝ U(w)^3/4
  • vI, v′O — the input vector of the centre word and the output vector of one context word; only the input vectors survive training.
  • W — the vocabulary size. The softmax denominator touches every word, which is what makes the exact objective too slow to train on billions of pairs.
  • σ(x) = 1/(1 + e^−x) — the logistic function: the true pair is a yes decision, each noise word a no.
  • k — noise words per positive pair: 5 to 20 on small corpora, 2 to 5 on large ones.
  • U(w)^3/4 — unigram counts raised to the 3/4 power and renormalised, which the second paper found clearly better than either the raw unigram or a uniform distribution.

Negative sampling is noise-contrastive estimation with the noise-probability correction dropped, so it no longer approximates the softmax's probabilities; it only has to produce good vectors. At the extremes: with k = 0 there is nothing to push against, and the objective is maximised by making every dot product huge; with the exponent at 1 the negatives are mostly "the" and "of", and at 0 they are mostly rare words nothing is confused with. Hierarchical softmax instead writes p(w | wI) as a product of σ(±v′n · vI) over the inner nodes n on w's tree path — a properly normalised distribution at one sigmoid per level.

The evidence

What the two word2vec papers measured

The first paper's test was its own analogy set: 8,869 semantic questions (capitals, currencies, cities in states, male and female forms) and 10,675 syntactic ones (plurals, tenses, comparatives, nationality adjectives), scored by exact match. It compared its two architectures with earlier neural language models at 640 dimensions on the same training data, then scaled them to the six-billion-word Google News corpus on a cluster. The second paper trained 300-dimensional skip-gram models on a one-billion-word news corpus and compared ways of avoiding the full softmax.

SetupModelSemantic %Syntactic %Total %
Paper 1 · 640-d, same dataFeedforward NNLM2353—
Paper 1 · 640-d, same dataCBOW2464—
Paper 1 · 640-d, same dataSkip-gram5559—
Paper 1 · 1,000-d, 6B wordsCBOW57.368.963.7
Paper 1 · 1,000-d, 6B wordsSkip-gram66.165.165.6
Paper 2 · 300-d, 1B wordsHierarchical softmax405347
Paper 2 · 300-d, 1B wordsNCE, k = 5456053
Paper 2 · 300-d, 1B wordsNegative sampling, k = 15586361
Paper 2 · same, subsampledNegative sampling, k = 15616161

The architecture rows explain the usual advice: skip-gram was far stronger on semantic questions, CBOW slightly stronger on syntactic ones and cheaper to train. The second paper's ablations carry its argument. Subsampling frequent words at t = 10^−5 cut training time by roughly half or more — negative sampling with k = 5 went from 38 to 14 minutes — while holding or improving accuracy, and it lifted hierarchical softmax from 47% to 55%. The 3/4-power noise distribution beat both the raw unigram and the uniform distribution on every task.

The negative result is that no estimator won everywhere. Hierarchical softmax lost to negative sampling on single-word analogies, yet on the paper's new phrase analogies it became the best method once frequent words were subsampled. The best phrase model — hierarchical softmax, 1,000 dimensions, the whole sentence as context and about 33 billion training words — reached 72%, against 66% with six billion words.

Since then

What the authors admit, and what happened next

The structural limitation is one vector per word, forever: a word has a single representation regardless of context, so every sense of "bank" is averaged into one point. The model has no mechanism to do otherwise, and the paper does not claim one.

The analogy result was also over-read, and the correction is instructive. Later analyses showed the arithmetic works much less cleanly than the famous examples suggest — performance depends heavily on the evaluation excluding the query words themselves, and many relation types barely work at all. The linear structure is real and it is weaker than the demonstration implied. Worth holding as a general caution: a striking example is not a measurement.

The other finding that aged badly in a more serious way: because the vectors encode the statistics of the corpus, they encode its biases as geometry, which made "similarity is just geometry" a claim with consequences nobody had priced in.

How it held up: the specific model is history and the idea is the foundation of everything above it. Every rung on this ladder, and every retrieval system on this site, rests on "meaning is a direction in a learned space". The one-vector-per-word limit was removed by contextual models — which is rung 1 of the scaling ladder, and why rung 3 here is about repairing it for similarity search.

SOLVE IT YOURSELF

Do arithmetic on words

The result that made the field gasp — king − man + woman ≈ queen — is just vector subtraction and a nearest-neighbor lookup. To run it without downloading gigabytes of trained vectors, this uses a small space with interpretable axes (royal, gender, place…); the vectors are illustrative, but the operations are exactly what you run on real Word2Vec embeddings. Watch the analogies resolve, then check that related words sit close and unrelated ones are orthogonal. Add a word and invent your own analogy.

YOUR TASK

Write the body of analogy. Everything else in the file is already written; this is the function the paper is about. Press ▶ Run to check it against the 5 assertions below — they start red.

WHAT IT HAS TO SATISFY — 5 ASSERTIONS
  1. King - man + woman resolves to queen
  2. Paris - france + italy resolves to rome
  3. A vector is identical to itself (cosine 1)
  4. Related words are closer than unrelated ones
  5. The analogy never returns one of its input words
CPython · WebAssembly
Questions

Questions people ask about Word2Vec

How is word2vec's negative sampling objective derived?

Start from skip-gram's softmax, which scores a context word against every word in the vocabulary and is too slow to train on billions of pairs. Noise-contrastive estimation turns that into a classification problem: tell real (centre, context) pairs apart from pairs built with noise words. Negative sampling, introduced in the second 2013 word2vec paper by Mikolov, Sutskever, Chen, Corrado and Dean, simplifies noise-contrastive estimation further by dropping its correction for the noise probabilities. Each training pair then contributes log σ(v′_O · v_I) for the real context word plus log σ(−v′_n · v_I) for each of k noise words drawn from the unigram distribution raised to the 3/4 power. The result no longer estimates the softmax's probabilities, but the authors only needed good vectors, and on their analogy test it beat both noise-contrastive estimation and the hierarchical softmax.

How does hierarchical softmax work in word2vec?

Hierarchical softmax replaces one normalisation over the whole vocabulary with a path through a binary tree. Every word is a leaf, every inner node has its own output vector, and the probability of a word is the product of the left-or-right sigmoid decisions along the path from the root to its leaf. Word2vec builds the tree as a Huffman code from word counts, so frequent words get short paths. A balanced tree over a million words needs about 20 decisions per prediction, and the first word2vec paper notes that the Huffman tree roughly halves that for a vocabulary of that size. Unlike negative sampling, the result is a properly normalised distribution. In the second paper it lost to negative sampling on single-word analogies but became the best method on phrase analogies once frequent words were subsampled.

Why does word2vec raise word frequencies to the 3/4 power?

The noise distribution decides which wrong words the model learns to push away. Sampling from raw unigram frequencies would make most negatives 'the', 'of' and 'and', which teaches little; sampling uniformly would make most negatives rare words that are trivially different. Raising each count to the 3/4 power and renormalising sits between the two: it flattens the distribution, so mid-frequency words are drawn more often than their raw share. A word ten times as frequent as another is drawn only about 5.6 times as often. The exponent is empirical. The second word2vec paper reports that the 3/4-power distribution clearly outperformed both the unigram and the uniform distribution for noise-contrastive estimation and negative sampling on every task it tried, and GloVe later chose the same 3/4 exponent for its weighting function, also on empirical grounds.

What does subsampling of frequent words do in word2vec?

Before training, each occurrence of a word w is thrown away with probability 1 − √(t / f(w)), where f(w) is the word's share of the corpus and t is a threshold around 10^−5. Any word rarer than t is always kept, while a word making up 1% of all tokens loses about 97% of its occurrences. The reasoning is that a pair like (France, the) carries almost no new information after it has been seen a million times. In the second word2vec paper this cut skip-gram training time by roughly half or more and improved the vectors, especially for rarer words; it lifted hierarchical softmax from 47% to 55% on the analogy test. In the released tool the dropped words are removed before windowing, so the effective context window widens too.

Which word2vec results come from the first paper and which from the second?

The January 2013 paper, Efficient Estimation of Word Representations in Vector Space, introduced CBOW and skip-gram, used the hierarchical softmax to make them fast, built the 19,544-question semantic and syntactic analogy test, and scaled training to the six-billion-word Google News corpus, where its best skip-gram model answered 65.6% of the analogy questions. The NIPS 2013 paper, Distributed Representations of Words and Phrases and their Compositionality, added negative sampling, the 3/4-power noise distribution, subsampling of frequent words, phrase detection with a phrase analogy test, and additive composition such as Germany + capital landing near Berlin. The released word2vec tool implements both, which is why they are usually cited as one method.

Check yourself

4 questions to test what you just read

Work each one out before you open it — a number, a prediction or a reason, not a definition. Producing the answer is what makes it stick. Your progress is kept on this device.

Skip-gram draws R uniformly from 1 to c for each centre word. With c = 5, how many (centre, context) pairs does one position produce on average, and why do near words end up weighted more?

Show answer

R averages (1+2+3+4+5)/5 = 3, so a position yields 2R = 6 pairs on average instead of 10. A word one position away is included for every value of R, but a word five away only when R = 5 — one draw in five — so nearer context words are sampled more often. It is a cheap distance weighting.

With t = 10^−5, what share of its occurrences does a word lose if it makes up 0.1% of the corpus, and what about a word at 0.001%?

Show answer

The discard probability is 1 − √(t/f). For f = 10^−3: 1 − √0.01 = 1 − 0.1 = 0.9, so 90% of its occurrences are dropped. For f = 10^−5, √1 = 1 and the probability is 0: words at or below the threshold are always kept.

Word A appears 10 times as often as word B. How much more often is A drawn as a negative under the 3/4-power noise distribution, and why is that better than raw frequency?

Show answer

10^0.75 ≈ 5.6 times, not 10. Flattening the distribution spends more negatives on mid-frequency words the model could plausibly confuse with the true context, rather than on "the" and "of". The second paper found the 3/4 power beat both the raw unigram and the uniform distribution.

For a one-million-word vocabulary, roughly how many output-vector dot products does one prediction cost with a full softmax, a balanced binary tree, and negative sampling with k = 5?

Show answer

Full softmax: about 1,000,000, one per word for the normaliser. Balanced tree: log2(10^6) ≈ 20 sigmoid decisions, and the Huffman tree of the first paper averages fewer because frequent words sit near the root. Negative sampling: 6 — the true context word plus five noise words.

Where this sits

From a word vector to RAG

The longest ladder here, and the most linear: nine papers that carry one idea — text as geometry — from a 2013 word embedding to a graph-structured retrieval system.

  1. 1Word2Vec2013
  2. 2GloVe2014
  3. 3Sentence-BERT2019
  4. 4Product Quantization2011
  5. 5HNSW2016
  6. 6Dense Passage Retrieval2020
  7. 7ColBERT2020
  8. 8Retrieval-Augmented Generation2020
  9. 9GraphRAG2024

All 110 papers, in 19 ladders →

Finished this one? 0 / 110 Paper Breakdowns done
NextGloVe Next on the “From a word vector to RAG” ladder · rung 2 of 9

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns