Word2Vec
Word2Vec is a family of shallow neural network models introduced by Tomas Mikolov et al. at Google in 2013 that learn dense vector representations of words — called word embeddings — by predicting words from their context (or context from words) in a large text corpus. These embeddings capture rich semantic and syntactic relationships, enabling arithmetic on meaning: the now-famous result king − man + woman ≈ queen emerges naturally from the training objective. Remarkably, Word2Vec never explicitly encodes meaning — it learns geometry purely from co-occurrence statistics, yet this geometry generalizes to analogical reasoning, sentiment, and syntax with striking accuracy.
Words that appear in similar contexts tend to mean similar things — Word2Vec operationalizes this distributional hypothesis by training a neural network to predict context from a word (Skip-gram) or a word from its context (CBOW), then discarding the prediction head and keeping only the learned weight matrix as the embedding. The network is never told what words mean; it discovers meaning as a byproduct of solving a prediction task at massive scale. The "aha moment" is this: the embedding is not the goal — it is the side effect, and that side effect turns out to be one of the most useful representations in all of NLP.
The most common mistake is believing Word2Vec embeddings encode meaning in any absolute sense — practitioners say "the vector for 'bank' means financial institution," then are puzzled when it fails on financial texts because the corpus was general web data. The correct mental model is that Word2Vec embeddings encode relative co-occurrence geometry for a specific corpus: the vector for "bank" sits between financial and river contexts depending on what your training data emphasizes. A model trained on financial news will produce a fundamentally different geometry than one trained on Wikipedia. There is no universal "meaning" — only corpus-conditional distributional statistics frozen into vectors.
How It Works
Word2Vec offers two architectures that share the same core idea but invert the prediction direction.
Skip-Gram: Predicting Context from Word
Given a center word at position , Skip-gram tries to predict each context word within a window of radius .
Explanation: The probability of observing context word given center word is the softmax over dot products between the output embedding of every vocabulary word and the input embedding of the center word.
Symbols:
- — the input (center) embedding of word , drawn from the input weight matrix
- — the output (context) embedding of word , drawn from the output weight matrix
- — vocabulary size; — embedding dimensionality (typically 100–300)
The full training objective for a corpus of tokens is:
Explanation: Maximize the average log-probability of every context word within window of every center word across the entire corpus — equivalently, minimize negative log-likelihood.
The Softmax Bottleneck and Negative Sampling
Computing the denominator of Eq. [1] over a vocabulary of size is prohibitively expensive at every gradient step. Negative Sampling replaces the full softmax with a binary classification problem: distinguish true context pairs from randomly sampled "noise" pairs.
Explanation: The model is rewarded for scoring real word–context pairs highly (first term) and penalized for scoring noise samples highly (second term), where is the sigmoid function.
Symbols:
- — sigmoid function
- — number of negative samples (typically 5–20 for small corpora, 2–5 for large)
- — noise distribution, the unigram frequency raised to the 3/4 power to slightly up-sample rare words
The gradient update for the center embedding becomes:
Explanation: The center embedding is pushed toward the true context embedding and pushed away from each of the noise embeddings, with magnitude proportional to how wrong the current prediction is.
CBOW: Predicting Word from Context
The Continuous Bag-of-Words (CBOW) model inverts Skip-gram: it averages the input embeddings of all context words and predicts the center word.
Explanation: The center word probability is computed from the dot product between its output embedding and the mean of all context word input embeddings — CBOW smooths over context order, which makes it faster but slightly less precise on rare words than Skip-gram.
PyTorch Sketch: Negative Sampling Skip-Gram
```python import torch import torch.nn as nn import torch.nn.functional as F class SkipGramNS(nn.Module): def __init__(self, vocab_size: int, embed_dim: int): super().__init__()
# Two separate embedding tables: input (center) and output (context) self.center_emb = nn.Embedding(vocab_size, embed_dim) self.context_emb = nn.Embedding(vocab_size, embed_dim)
# Initialise small for stable early training nn.init.uniform_(self.center_emb.weight, -0.5 / embed_dim, 0.5 / embed_dim) nn.init.zeros_(self.context_emb.weight) def forward(self, center, pos_context, neg_context):
# center: (B,) — center word indices
# pos_context: (B,) — true context word indices
# neg_context: (B, k) — k negative sample indices per example v_c = self.center_emb(center) # (B, D) u_pos = self.context_emb(pos_context) # (B, D) u_neg = self.context_emb(neg_context) # (B, k, D)
# Positive score: log σ(u_pos · v_c) pos_score = F.logsigmoid((u_pos * v_c).sum(dim=1)) # (B,)
# Negative score: sum_k log σ(-u_neg · v_c) neg_score = F.logsigmoid( -(u_neg * v_c.unsqueeze(1)).sum(dim=2) # (B, k) ).sum(dim=1) # (B,) loss = -(pos_score + neg_score).mean() return loss ```
Mikolov completed his PhD at Brno University of Technology working on recurrent language models before joining Google Brain, where in 2013 he led the team that published the original Word2Vec papers — both the architecture and the NNLM-beating efficiency tricks like negative sampling and subsampling of frequent words. His specific insight was that a single hidden-layer network trained with the right objective on enough data could produce embeddings that outperformed far deeper networks on word analogy benchmarks. Surprisingly, Mikolov later joined Facebook AI Research and then founded his own Prague-based lab; he has remained skeptical of large language models, consistently arguing that symbolic grounding, not scale, is the path to general intelligence.
Intuition
Imagine you are learning a foreign language but the dictionary has been destroyed — all you have are millions of sentences. You notice that the words "doctor" and "physician" keep appearing near the same other words: "hospital," "patient," "treatment," "diagnosis." Meanwhile, "doctor" and "hammer" never appear in the same neighborhoods. A smart student would conclude that "doctor" and "physician" are related, and that both are far from "hammer." This is exactly what Word2Vec does, except the "notebook" in which the student records these relationships is a vector in 300-dimensional space, and the "proximity" is a dot product. Training is just the student getting quizzed over and over — "given 'hospital,' what word was nearby?" — and updating their notebook after every wrong answer. When training stops, the notebook entries are the embeddings. The analogy that breaks this intuition: the student never learns what a doctor is — only the company doctors keep.
Historical Development
Why It Matters
Prerequisites
What This Leads To
See Also
GloVe (Global Vectors) · Transformer Architecture · BERT · Recurrent Neural Networks · Attention Mechanism · Subword Tokenization and fastText · Cosine Similarity and Vector Spaces