5.2 — Embeddings: word2vec, geometry, and the modern picture
Goal: Understand the embedding matrix as a learned lookup table, derive the skip-gram-with-negative-sampling objective that put 'embedding' into the deep-learning vocabulary, and place word2vec inside the modern picture where embeddings are trained end-to-end with the language model.
1. Why this session exists
Section 5.1 ended with the input to the model being a sequence of integer token IDs. Integers are not a useful representation: arithmetic on them () is meaningless, and a neural network cannot extract structure from arbitrary categorical labels. We need to map each token ID to a dense vector — its embedding — and we want those vectors to capture semantic structure: tokens that mean similar things should map to nearby points; analogies should be linear; co-occurring tokens should align.
This session does three things:
- Define the embedding matrix and the lookup operation.
- Derive Mikolov's skip-gram-with-negative-sampling (SGNS) objective, the algorithm behind word2vec, and see why it produces the linear-analogy geometry that made embeddings famous.
- Place word2vec inside the modern picture: in LLMs, embeddings are not trained with a separate skip-gram step. They are part of the language model and are updated jointly with everything else. Word2vec is the conceptual ancestor and the geometric intuition, not the implementation.[1]
2. The embedding matrix
Fix a vocabulary of size from §5.1 and an embedding dimension . The embedding matrix is a learnable parameter
For token ID , the embedding of token is the -th row :
Equivalently: if is the one-hot vector with a 1 at coordinate , then . The embedding layer is therefore a linear map from one-hot vectors to dense vectors — the simplest possible learnable function from categorical inputs.
For a sequence of token IDs , the embedded input is a matrix with rows . This is the tensor that every subsequent layer of the model consumes.
Parameter count. . For (LLaMA-scale), the embedding matrix alone is parameters — comparable to a few Transformer blocks.
2.1 What we want from embeddings
We want when tokens have similar meaning, and more ambitiously we want linear semantic structure: relations between concepts should correspond to consistent vector offsets. The famous empirical observation:
That such a relation could be learned from raw co-occurrence statistics, with no human supervision, was a small revolution in 2013 and is still the cleanest demonstration of why distributed representations are powerful.
3. The distributional hypothesis
The conceptual backbone of every embedding method since Bengio et al. (2003)[4] is the distributional hypothesis, due to Harris (1954)[3] and Firth (1957)[2]:
"You shall know a word by the company it keeps." — Firth
Concretely: two words that appear in the same kinds of contexts (the words around them) tend to have similar meanings. If "dog" and "cat" are surrounded by similar neighbours ("pet", "walk", "vet"), they should be close in embedding space — even if the algorithm never sees a dictionary or an ontology.
Every embedding method since then can be read as: "design an objective whose optimum forces vectors of tokens with similar contexts to be similar." Word2vec is one such design; GloVe (Pennington et al., 2014)[7] is another; the LLM's own forward pass is a third. The conceptual unity is the distributional hypothesis.
4. Skip-gram with negative sampling (SGNS)
Mikolov et al. (2013)[6] introduced two related architectures (CBOW and skip-gram); skip-gram with negative sampling, the more commonly used variant, is what we derive here.
4.1 The skip-gram setup
Two embedding matrices: a target embedding and a context embedding . Each token has two vectors — one for when it appears as a "centre word" () and one for when it appears as a "neighbour" ().
Given a corpus tokenised as , define positive (centre, context) pairs by sliding a window of half-width :
A typical window is ; each occurrence of a word in the corpus generates up to positive pairs.
4.2 The objective
For each positive pair and negative samples drawn i.i.d. from a noise distribution over the vocabulary, SGNS minimises:
where is the logistic sigmoid. Summing over all positive pairs in the corpus:
Read the two terms:
- First term — positive pair. Push up (so approaches 1) — make the centre and a real context look "the same direction."
- Second term — negative samples. Push down (so approaches 1) — make the centre look unlike random tokens.
That is exactly a binary classifier with logistic loss (1.2 §3): "is a real corpus pair or a noise pair?" The clever idea was framing the multi-class "predict the context" problem as a series of binary classifications, sidestepping the prohibitive -way softmax that the original "hierarchical softmax" version needed.
4.3 The noise distribution
Mikolov found empirically that , where is the unigram frequency of token in the corpus, works far better than the unigram distribution or the uniform distribution. The exponent downweights very frequent words (function words like "the") and upweights rare content words; the resulting negative-sample distribution is more informative.
4.4 The hyperparameters
- — embedding dimension. 100–300 for word2vec; 4096+ for modern LLMs.
- — window half-width. Larger windows pick up topical similarity; smaller windows pick up syntactic similarity.
- — number of negative samples per positive. Typically 5–20.
4.5 What you get out
After training, the target embedding is the artefact people use downstream. The context matrix is discarded. The result: a matrix where similar-meaning tokens are nearby and (empirically) where linear analogies hold.
5. Why linear analogies emerge
The famous king − man + woman ≈ queen relation is empirical, not a theorem. But there is a clean heuristic argument.
Consider the analogy "man is to woman as king is to queen." Both pairs differ by the same semantic feature — gender. If embeddings encode each independent semantic dimension as an approximately linear direction in the embedding space (a feature axis), then the gender axis should satisfy
for similar . Adding the first equation to :
The success of this prediction at scale empirically validates the claim that SGNS (and successor methods) produces a representation whose principal directions roughly correspond to interpretable semantic axes. The structure is approximate — analogy accuracy on benchmark sets like Google Analogy is 60–70%, not 100% — but it is real, and it is the cleanest experimental evidence that distributed representations encode meaning geometrically.
[10]6. The modern picture: embeddings are part of the model
Every word2vec-style method has one structural feature that LLMs do not share: it trains embeddings as a standalone artefact, using an objective (SGNS) that is not the language-modelling objective we will define in 5.3.
In an LLM, the embedding matrix is the first layer of the network. It is initialised randomly (typically or similar) and updated by backprop from the next-token cross-entropy loss, jointly with every other parameter. There is no separate "pre-train embeddings, then train the model" step.
This matters for two reasons:
(a) The embeddings are task-specific. An LLM's embedding for "bank" is shaped by the contexts in which the language model must distinguish meanings — they are tuned to whatever the rest of the model needs them to be. Word2vec embeddings are tuned to one specific co-occurrence objective. Modern LLM embeddings do still exhibit the linear-analogy structure (you can probe a frozen LLM and find it), but they are richer.
(b) The dimension is much larger. Word2vec used . LLaMA-3-70B uses . The embedding matrix grows linearly with this hidden size and is a non-trivial fraction of the parameter budget.
6.1 Weight tying
A trick used by almost every LLM (introduced by Press & Wolf, 2017)[9]: the output projection that maps the final hidden state to logits over the vocabulary uses the same matrix as the embedding, transposed:
This halves the parameter count of the vocabulary-facing matrices (you keep one instead of two), and empirically improves perplexity slightly: the input and output representations of a token are forced to live in the same space, which makes pretraining sample-efficient.
LLaMA-1/2/3 tie embeddings. GPT-2 and GPT-3 tied them. Some larger models untie them again for capacity reasons; this is an architectural choice rather than a settled matter.
6.2 What the embedding contains
After pretraining, is a -vector that the rest of the model knows how to interpret. Empirically, even from a fully trained LLM:
- Similar tokens are nearby in similarity. "Cat" and "feline" cluster.
- Subword pieces of related words cluster (e.g. the
ertoken from §5.1 sits near other suffix-like fragments). - Syntactic categories cluster (verbs near verbs, prepositions near prepositions).
- The first few principal directions encode coarse semantic axes: gender, sentiment, topic.
These properties are emergent — no part of the loss function explicitly asks for them. They arise because the rest of the network is doing useful prediction work, and a useful embedding is one whose geometry makes that work easier.
7. What we keep going forward
Three durable lessons:
- The first layer of every LLM is a learnable lookup table .
- The geometry of that lookup table — distances, directions, linear analogies — emerges from training and encodes semantic structure without being explicitly programmed.
- Word2vec is the historical artefact that established the picture; the modern implementation is end-to-end training of jointly with the rest of the model.
5.3 will give the loss function that pulls all these knobs at once: next-token cross-entropy.
(a) Write the embedding-layer forward and backward passes explicitly. Given a one-hot input and an upstream gradient , what is for ? What does this imply for the sparsity of embedding-matrix updates?
(b) Suppose the SGNS objective uses one positive pair and one negative sample . Derive explicitly using . Interpret the sign of each term.
(c) Show that under weight tying , the gradient of the cross-entropy loss with respect to has two contributions: one from 's role as input embedding and one from its role in the output projection. Sketch this without full derivation.
(d) SGNS factorises the SPPMI matrix (the Aside in §4.5). Compute PMI for the pairs and in a hypothetical corpus where , , , , , . Which pair is more "associated"?
(e) Sketch why a model with benefits significantly more from weight tying than a model with .
References
- [1]
Prince, S. J. D. (2023). Understanding Deep Learning, §12 (Sequence-to-Sequence Models) and §15 (Recurrent Networks). MIT Press. — Embeddings (5.2), RNN treatment (5.5).
↩ - [2]
Firth, J. R. (1957). "A synopsis of linguistic theory, 1930–1955". Studies in Linguistic Analysis, 1–32. — Distributional hypothesis (5.2 §3).
↩ - [3]
Harris, Z. S. (1954). "Distributional structure". Word, 10(2–3), 146–162. — Distributional hypothesis (5.2 §3).
↩ - [4]
Bengio, Y., Ducharme, R., Vincent, P., & Jauvin, C. (2003). "A neural probabilistic language model". JMLR, 3, 1137–1155. — First neural LM with learned word embeddings; the conceptual ancestor of every modern LLM (5.2, 5.4).
↩ - [5]
Mikolov, T., Chen, K., Corrado, G., & Dean, J. (2013). "Efficient estimation of word representations in vector space". arXiv:1301.3781 — Word2Vec architectures (CBOW, skip-gram) (5.2).
↩ - [6]
Mikolov, T., Sutskever, I., Chen, K., Corrado, G., & Dean, J. (2013). "Distributed representations of words and phrases and their compositionality". NeurIPS. arXiv:1310.4546 — Skip-gram with negative sampling; the linear-analogy result (5.2 §4–§5).
↩ - [7]
Pennington, J., Socher, R., & Manning, C. D. (2014). "GloVe: Global vectors for word representation". EMNLP. — Co-occurrence-matrix factorisation as an alternative to word2vec (5.2 §3).
↩ - [8]
Levy, O., & Goldberg, Y. (2014). "Neural word embedding as implicit matrix factorization". NeurIPS. — Proves that SGNS factorises the shifted PMI matrix (5.2 Aside).
↩ - [9]
Press, O., & Wolf, L. (2017). "Using the output embedding to improve language models". EACL. arXiv:1608.05859 — Weight tying between input embedding and output projection (5.2 §6.1).
↩ - [10]
DeepSeek-AI (2024). "DeepSeek-V2: A strong, economical, and efficient mixture-of-experts language model" (MLA). arXiv:2405.04434 — Multi-head latent attention; low-rank K, V compression (6.14 §5.2; full treatment Cap 8.5).
↩