Efficient Estimation of Word Representations in Vector Space
2013 · arXiv / ICLR workshop · ~11 min read
Read the original paperLearns a vector for every word from raw text by predicting nearby words with a deliberately tiny model. Read it for the cost argument behind that model, and to see what the famous analogies do and do not show.
Suppose it is 2012 and you are building a sentiment classifier for product reviews. Your training set contains the word “excellent” hundreds of times and “superb” twice. The model learns a strong positive weight for “excellent” and almost nothing for “superb”, because to the model they are unrelated symbols: entry 14,302 and entry 41,977 in a vocabulary. Any knowledge about one word has to be learned again, from scratch, for every other word.
Word2vec is the name for two papers Tomas Mikolov and colleagues at Google published in 2013 and the open-source tool that came with them. They trained a very cheap model on billions of words of ordinary text and got, for every word, a vector of a few hundred numbers in which “superb” sits next to “excellent”. Those vectors could be downloaded and dropped into any model as its first layer.
This explainer follows how the model learns from a five-word sentence, works one training update by hand with negative sampling, and then looks hard at the famous analogy results: what they measure, and how far to trust them. The first paper (Efficient Estimation of Word Representations in Vector Space) introduced the models; the second, a few months later, supplied the training tricks the tool shipped with. We treat them together, as most people do.
Why one-hot vectors teach a model nothing about neighbours
The default input representation was a one-hot vector: as long as the vocabulary, all zeros except a one at the word’s position. The dot product of any two different one-hot vectors is zero. Every word is exactly as far from every other word, so similarity has to come from somewhere else, and in a linear classifier it comes from nowhere.
The idea that would fix it was old. Linguists had argued since the 1950s that words that occur in similar contexts tend to have similar meanings. Bengio’s neural language model (2003) already learned a dense vector per word as a side effect of predicting the next word. Latent semantic analysis got vectors by factorising a word-document count matrix. The problem was cost. Neural language models took weeks to train on a few hundred million words, and the vectors improve with more text. Nobody could afford to train on the billions of words that were sitting on the web.
Mikolov’s cost argument: remove the hidden layer
The first paper is, at heart, an accounting exercise. It writes down the number of operations per training example for each model and asks which term dominates. For a feed-forward neural language model that reads N previous words, each a D-dimensional vector, through a hidden layer of H units, and predicts one of V vocabulary words, the cost is roughly N·D (look up the inputs) + N·D·H (the hidden layer) + H·V (score every word in the vocabulary).
Put in numbers of the size the paper discusses: N = 10, D = 500, H = 500 and a vocabulary of V = 1,000,000. The lookup is 5,000 operations, the hidden layer 2.5 million and the output layer 500 million. A known trick, the hierarchical softmax, replaces the output layer with a walk down a binary tree of the vocabulary, about log2(1,000,000) ≈ 20 steps, so H·V becomes 500 × 20 = 10,000. Now the hidden layer is 99 per cent of the work.
So remove it. In the paper’s two new models, a word’s vector is used directly to make the prediction, with no hidden layer and no non-linearity. Continuous bag-of-words (CBOW) averages the vectors of the surrounding words and predicts the middle one. Skip-gram goes the other way: it takes the middle word and predicts each surrounding word in turn. With a context of up to 10 words, 300-dimensional vectors and the hierarchical softmax, skip-gram costs about 10 × (300 + 300 × 20) ≈ 63,000 operations per example, around forty times less than the neural language model above. The paper reports learning good vectors from 1.6 billion words in under a day.
The authors were explicit about what they gave up. A model this simple is a worse language model; it cannot represent the order of words in its window, and it cannot compose meanings non-linearly. Their bet was that for learning word vectors, a simple model trained on far more text would beat a rich model trained on less. The paper’s tables bear this out.
Skip-gram with a window of 2 reads the sentence “cats chase small grey mice”. How many (centre, context) training pairs does it produce, and which pairs involve “small”?
Show the worked answer
Fourteen. Each word pairs with up to two words on each side, but the words near the ends have fewer neighbours: “cats” gets 2 pairs, “chase” 3, “small” 4, “grey” 3, “mice” 2.
With “small” as the centre, the pairs are (small, cats), (small, chase), (small, grey) and (small, mice). In the real tool, the window size for each centre word is drawn at random between 1 and the maximum, so close neighbours appear in more pairs than distant ones. That is a cheap way to weight nearby words more heavily.
One training update with negative sampling, by hand
Each word has two vectors: an input vector, used when it is the centre word, and an output vector, used when it is being predicted. The score for a pair is the dot product of the centre word’s input vector with the context word’s output vector. With a full softmax, the model turns the score into a probability by comparing it against the scores of every word in the vocabulary. For a million-word vocabulary and 300 dimensions, that is 300 million multiply-adds for a single pair.
The second paper replaced this with negative sampling. Instead of asking “which of a million words is the context?”, the model is asked a yes-or-no question for the real pair and for k fake pairs made by drawing random words: is this a real neighbour? Take the pair (small, grey) with toy three-dimensional vectors and k = 2 noise words, “piano” and “election”.
input v(small) = ( 0.2, -0.1, 0.4)
output u(grey) = ( 0.3, 0.2, 0.5) real context
output u(piano) = ( 0.1, 0.4, -0.2) noise
output u(election) = (-0.3, 0.1, 0.2) noise
score(grey) = 0.06 - 0.02 + 0.20 = 0.24 sigmoid = 0.560
score(piano) = 0.02 - 0.04 - 0.08 = -0.10 sigmoid = 0.475
score(election) =-0.06 - 0.01 + 0.08 = 0.01 sigmoid = 0.503The model wants the sigmoid of the real pair’s score near 1 and the sigmoids of the noise pairs near 0. Right now it is close to a coin toss on all three, and the loss is −log(0.560) − log(1 − 0.475) − log(1 − 0.503) ≈ 0.58 + 0.64 + 0.70 = 1.92.
The gradient for v(small) is a weighted sum of the three output vectors. The real pair contributes (0.560 − 1) × u(grey), pulling v(small) towards “grey”; each noise word contributes its sigmoid times its vector, pushing v(small) away from it. The total is about (−0.236, 0.152, −0.215). With a learning rate of 0.1 (larger than the tool’s default of 0.025, so the change is visible), the new v(small) is (0.224, −0.115, 0.422). Recompute the scores: “grey” rises from 0.240 to 0.255, “piano” falls from −0.100 to −0.108, and “election” falls from 0.010 to 0.006. The output vectors are updated the same way. Repeat this a few billion times and words that share neighbours are pulled towards the same regions.
The cost per pair is now k + 1 dot products. With k = 5 and 300 dimensions that is 1,800 multiply-adds, against 300 million for the full softmax and about 6,000 for the hierarchical softmax. The second paper recommends k between 5 and 20 for small corpora and 2 to 5 for large ones.
Two more details decide the quality of the result, and both were found by trying things. Noise words are not drawn by their raw frequency, which would make almost every negative “the” or “of”. They are drawn in proportion to frequency raised to the power 3/4. If “the” has frequency 0.05 and a rarer word 0.0001, the raw odds are 500 to 1; after the 3/4 power they are about 106 to 1, so rare words get chosen as negatives more often. And frequent words are thrown away at random before training, with probability 1 − √(t/f) for a word of frequency f and a threshold t around 10−5. For “the” at 0.05, that discards about 98.6 per cent of occurrences. A word with frequency 10−4 loses about 68 per cent; anything rarer than the threshold is kept. The paper reports that this both speeds up training and improves the vectors of rare words, because the window now reaches past the filler words to something informative.
Where the analogies come from, and how far to trust them
The result the papers are remembered for was a test, not a training objective. The first paper built a set of 8,869 semantic questions (Athens is to Greece as Oslo is to what?) and 10,675 syntactic ones (good is to better as rough is to what?). To answer “a is to b as c is to ?”, take the vector b − a + c and find the nearest word by cosine similarity. A question counts as correct only if the nearest word is exactly the expected one, so a synonym scores zero.
Why should subtraction mean anything? Consider what the training pushes towards. “King” and “queen” appear in many of the same contexts (crown, throne, reign), and differ mostly in the contexts where “man” and “woman” also differ (he, she, his, her). If the model is close to linear, those shared and differing contexts become shared and differing directions, and the difference king − queen ends up similar to man − woman. The simulation below places sixteen words in two dimensions so that these relations hold exactly. Real vectors have hundreds of dimensions and the relations hold only roughly.
Sixteen words placed in two dimensions so the relationships are visible. Pick a − b + c and see which word is nearest to the result. Real embeddings have hundreds of dimensions and the relationships are only approximate, but they are there.
Training is nothing more than: given the centre word, predict the words in its window, millions of times over. Words that keep the same company end up with similar vectors, and because the model is linear, consistent relationships (gender, tense, capital-of) become consistent directions. Nobody told it what a capital was.
Three cautions. First, in the standard evaluation the three input words are excluded from the answer. Later analyses found that without that exclusion, the nearest vector to king − man + woman is often “king” itself. The analogy usually works because “queen” is the best answer among the remaining words, which is a weaker claim than “the arithmetic lands on queen”. Second, analogy accuracy correlated imperfectly with how useful the vectors were in real tasks. Third, the same arithmetic reproduced stereotypes in the training text; a 2016 paper by Bolukbasi and colleagues showed analogies associating “man” with “computer programmer” and “woman” with “homemaker”. The vectors encode the corpus, including its biases.
One table in the first paper deserves more attention than the analogies get. It varies vector size and training data together and shows that increasing only one of them stops helping quickly; the gains come from growing both. That is a small, early version of the argument the scaling-laws paper made seven years later.
Train two skip-gram models on the same text, one with a window of 2 words and one with a window of 10. Which model will put “Hogwarts” near “Dumbledore”, and which will put it near other school names like “Sunnydale”?
Show the worked answer
The wide window puts it near “Dumbledore”. Ten words on either side capture what a passage is about, so words from the same topic share contexts and cluster together.
The narrow window puts it near other names that fill the same grammatical slot, including other fictional schools. Two words of context mostly capture syntax and type: what kind of word goes here. Later analyses, notably by Levy and Goldberg in 2014, made this contrast explicit. Window size is not a tuning detail; it decides which notion of similarity the vectors encode.
The choices, and what each one gave up
| choice | what it bought | what it gave up | what later work did |
|---|---|---|---|
| no hidden layer | about 40× cheaper per example; billions of words become feasible | word order within the window; non-linear composition | contextual models (ELMo, BERT) brought depth back once compute allowed |
| one vector per word | a lookup table anyone can download and reuse | “bank” has one vector for rivers and money | contextual embeddings give a different vector per sentence |
| whole words as units | simple vocabulary | no vector for unseen or misspelled words | fastText (2016) built vectors from character n-grams |
| negative sampling | k + 1 dot products per pair instead of V | no longer a proper probability model of context | shown to factorise a shifted PMI matrix (Levy and Goldberg, 2014) |
| subsampling frequent words | faster training, better rare-word vectors | a heuristic threshold to tune | kept as standard practice |
The negative-sampling row hides a nice result. Levy and Goldberg showed in 2014 that skip-gram with negative sampling is implicitly factorising a matrix of word–context co-occurrence statistics (pointwise mutual information, shifted by log k). So word2vec was not as far from the count-based methods it replaced as it looked. Stanford’s GloVe, published the same year, fit vectors to the co-occurrence counts directly and got comparable results. The practical difference was that word2vec streamed through text and never built the matrix.
When static word vectors are the wrong tool
If the meaning you need depends on the sentence, a single vector per word is the wrong representation. “The bank raised rates” and “we walked along the bank” get the same vector for “bank”, and no downstream model can fully untangle that. Since 2018, models such as BERT compute a vector per token in context, and for most language tasks they replaced word2vec. Sentence and document search today uses embeddings from Transformer encoders, not averaged word vectors.
Static vectors still earn their place where cost dominates and context matters less: as features in small classifiers, in recommendation, where “words” are product IDs and “sentences” are shopping sessions (item2vec and its relatives), and wherever a few hundred megabytes of lookup table beats a GPU in production. The skip-gram objective itself, predict what co-occurs, is still the right first idea for any domain where you have sequences and no labels.
How to read the original
Read both papers. In the first (Mikolov, Chen, Corrado and Dean), Section 2 sets out the cost formulas for older models; skim the details but keep the Q expressions, because Section 3’s new models are justified entirely by them. Figure 1 shows CBOW and skip-gram side by side. Section 4.1 and Table 1 describe the analogy test set; look at the examples to see what kind of relation is being tested. Table 2, accuracy against vector size and training words, is the most instructive table in the paper. The large-scale results on a distributed system are worth a skim for the training times.
In the second paper (Distributed Representations of Words and Phrases and their Compositionality), Section 2 holds negative sampling, the 3/4-power noise distribution and subsampling, which is what the released tool implements. Section 4 learns phrases such as “New York Times” as single tokens, a step most summaries leave out. Read the analogy numbers with the cautions above in mind: exact-match nearest neighbour, input words excluded.
Word2vec gives each word one vector, fixed before the sentence is read. The next paper, Attention Is All You Need, starts from those per-token vectors and lets each one be rewritten by looking at the other words in the sentence, which is how “bank” finally gets to mean two things.
The original. Read it after this page; the explainer above is a map, not a substitute.
The gentlest complete explanation of skip-gram and negative sampling, with pictures.
Fly through real word vectors in three dimensions and search for neighbours.