What word2vec is
A neural network reads numbers, not words. word2vec gives every word of a vocabulary its own vector, one row of a lookup table, learned so that words used in similar contexts end up with similar vectors. The table on this page is the one Google released with the word2vec code in 2013: 3,000,000 words and phrases, each a vector of 300 numbers, trained on about 100 billion words of Google News.
The Word → vector panel shows one row beside a neighbour's. Its 300 numbers mean nothing one by one; training fixes only how the vectors sit relative to each other, so what carries meaning is direction.
Cosine similarity
The similarity of two words is the cosine of the angle between their vectors,
1 for the same direction, 0 for perpendicular ones and −1 for opposite ones. The Nearest words panel compares the chosen word with all 3,000,000 rows. For king the nearest are kings (0.714), queen (0.651) and monarch (0.641): words that can stand in the same sentences, which is all the model ever sees. Rare strings are the weak point. Aricent_combines, a phrase ranked beyond the first million by frequency, lands near frog with cosine 0.670: a phrase seen in few contexts gets a vector shaped by few updates, and it can end up anywhere.
Analogies by vector arithmetic
Mikolov and colleagues found that some relations are close to fixed offsets: king − man + woman lands near queen. The Analogy arithmetic panel normalises the three vectors to length 1, forms and lists the nearest words, leaving out the three inputs as gensim and the paper do. queen comes first (cosine 0.712); so do Berlin for France : Paris :: Germany and worse for good : better :: bad. For France : Paris :: Italy, though, the expected Rome is only second, behind Milan.
Leaving out the inputs matters. With them left in, the nearest word to king − man + woman is king itself (0.799): the arithmetic moves the vector only part of the way. Nissim, van Noord and van der Goot showed that this rule is behind some famous examples of bias. For man : doctor :: woman the page's answer with the inputs left out is gynecologist, with nurse second; with them left in, the nearest word is doctor. The vectors do carry the associations of their news corpus, stereotypes included, as Bolukbasi et al. measured, but an analogy that is forbidden to return its own input overstates them.
The Word groups panel projects a group such as countries and their capitals onto the two directions along which those vectors vary most (principal component analysis) and joins each pair with an arrow. Where the arrows run parallel, one offset takes every first word to its partner, which is why can work. Two dimensions keep only part of the variance of 300, so the picture is a shadow of the real geometry.
The skip-gram objective
Skip-gram turns raw text into a prediction task. Each word in turn is the centre , and each word within a window around it is a context word to predict. Every word has two vectors, when it is the centre and when it is the context. A softmax over the whole vocabulary,
would cost one dot product per vocabulary word for every pair. Negative sampling replaces it with a few yes-or-no questions: is a real context word of , and are words drawn at random not? With the loss of one pair is
with the noise words drawn in proportion to , which gives rare words a little more than their share. Two more details are on the page. Each centre draws its window size from 1 to the maximum, so near words count more often (the first paper's trick). And very frequent words such as the are dropped from the text before windowing (the second paper's subsampling): the page uses the released code's rule, which keeps a word of frequency with probability for a threshold . Goldberg and Levy's note derives the objective step by step.
Training a tiny model
To show training itself, the page trains a small skip-gram model on the CPU: Tiny Shakespeare (194,015 words, a 3,251-word vocabulary), 50 dimensions, windows of up to 5 words, 5 noise words per pair, , 10 epochs at a learning rate falling linearly from 0.025, seed 0. The How such vectors are trained panel takes one speech, strikes out the words subsampling dropped and lists the pairs one centre makes.
The One SGD step table follows one pair: and for the real context word (label 1) and five noise words (label 0), before and after one step, and the loss before and after. The step raises the real word's and lowers the noise words'. Over the run the mean loss per pair falls from 2.854 in the first epoch to 2.531 in the last. The Nearest words, tiny model panel shows a word's neighbours before training, after it, and in Google's table.
Where it came from
Bengio et al.'s neural language model (2003) already learned a vector per word, as the first layer of a network that predicted the next word. Mikolov et al. (2013) showed that much simpler log-linear models with no hidden layer learn good vectors from very large text cheaply: their first paper reports less than a day for 1.6 billion words. CBOW predicts the centre from its context, skip-gram the context from the centre. GloVe (2014) reached similar vectors by fitting global word co-occurrence counts.
A word2vec vector is the same in every sentence, so bank has one vector for rivers and money. Contextual models fixed that: a transformer's first layer is still a lookup table of this kind, and the layers after it let each vector take in its context. The RNN and LSTM tabs show the step in between.
Papers
- Efficient Estimation of Word Representations in Vector Space word2vec: the CBOW and skip-gram models and the analogy test.
- Distributed Representations of Words and Phrases and their Compositionality Negative sampling, subsampling of frequent words and phrase vectors, the training behind the Google News table.
- word2vec Explained: deriving Mikolov et al.'s negative-sampling word-embedding method A short derivation of the negative-sampling objective and its gradients.
- Fair is Better than Sensational: Man is to Doctor as Woman is to Doctor Shows how excluding the inputs from analogy answers shapes the biased examples, as in the page's man : doctor :: woman.
- Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddings Measures gender stereotypes in these Google News vectors and proposes a way to remove a gender direction.
- Glove: Global Vectors for Word Representation Word vectors fitted to global co-occurrence counts, the count-based counterpart of word2vec.
- A Neural Probabilistic Language Model Learned word vectors as the first layer of a neural network that predicts the next word.