A one-hot vector represents a word as a vocabulary-sized vector with a single 1. Every pair of different words is equally dissimilar (orthogonal), and nothing learned about one word transfers to another. Dense embeddings represent each word by a short real vector (50 to 4096 numbers) placed so that words used in similar contexts are close, which is the distributional hypothesis. They can be built by factorising co-occurrence statistics (PPMI + SVD) or learned by prediction (word2vec, F3-2), and in neural networks an embedding layer is simply a lookup table equal to one-hot × E. This page covers one-hot encoding, the distributional hypothesis, co-occurrence matrices, PMI, SVD embeddings, cosine similarity and embedding layers. In the exercises you show one-hot vectors are orthogonal and an embedding lookup equals one-hot × E. You then classify words never seen by the classifier. One-hot vectors stayed at chance (0.23). 20-dimensional SVD embeddings reached 0.77 from a 300-sentence corpus (0.63 on rare words) and 1.00 from 3000 sentences.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Words as points on a map
One way to give each word a number pattern is a long list of zeros with a single 1 in the word’s own slot. But then “rocket” is as different from “satellite” as from “banana”. Dense embeddings instead place each word as a point in a space of, say, 100 dimensions, so that words used in similar sentences sit close together. A model that learns something about “rocket” then automatically knows a little about “satellite”. The demo shows tiny 2-D word vectors and their similarities.
Language lab
cos(king, queen) = 0.874. king − man + woman lands nearest to queen: directions in embedding space can encode relations.
Why engineers care
Embeddings power semantic search over technical documents, clustering of incident reports, and the first layer of every language model. They let systems match “engine flame-out” with “loss of combustion”.
📝 Quick check (Level 0)
0/3 answered
1. Two different one-hot vectors are…
2. Dense embeddings place similar words…
3. A dense embedding usually has…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
By hand
One-hot: e_i ∈ ℝ^V with a 1 in position i. Cosine similarity between different words is 0; dimension = vocabulary size (50,000+).
Embedding layer: a matrix E ∈ ℝ^(V×d); word i’s vector is row i, i.e. one-hot(i) × E, implemented as a lookup. 50,000 words × 300 dims = 15M parameters.
Distributional hypothesis (Harris, Firth): “you shall know a word by the company it keeps”.
Co-occurrence and PMI: count how often words appear near each other. PMI(x, y) = log P(x, y) / (P(x)P(y)); PPMI = max(PMI, 0) keeps positive associations.
SVD embeddings (LSA-style): factorise the PPMI matrix, M ≈ U S Vᵀ, and use U_d S_d^(1/2) as d-dimensional word vectors.
Similarity: cosine similarity; nearest neighbours reveal what the space has learned.
📝 Quick check (Level 1)
0/3 answered
1. Embedding parameters for 50,000 words × 300 dims:
2. PMI of words co-occurring twice as often as chance:
3. An embedding lookup equals…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.
Embeddings in code
Exercise 1.
One-hot vectors are mutually orthogonal.
An embedding lookup equals the one-hot matrix multiplied by E.
Cosine similarity measures closeness.
Practice.
In PyTorch, nn.Embedding(V, d) is the lookup table; it is trained with the rest of the model or initialised from pretrained vectors.
Classical count-based embeddings: build a PPMI matrix (window 2–5) and take a truncated SVD (sklearn.decomposition.TruncatedSVD).
Normalise vectors before cosine similarity, and inspect nearest neighbours to sanity-check an embedding space.
Never feed one-hot vectors of a large vocabulary densely; use sparse matrices or lookups.
Build one-hot vectors, compute cosine similarity and implement an embedding lookup.
📝 Quick check (Level 2)
0/3 answered
1. PyTorch's embedding layer:
2. Count-based embeddings come from…
3. Embedding spaces are sanity-checked by…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.
Theory
Why one-hot cannot generalise. A linear model on one-hot inputs has one independent weight per word. A word unseen during training has an all-zero contribution, so the model knows nothing about it, however similar it is to known words.
Why dense vectors can. If similar words have similar vectors, any smooth function of the vectors (a linear classifier, a neural network) gives similar outputs for them. Knowledge transfers from frequent to rare words in proportion to how well their contexts were estimated.
Factorisation view. Levy and Goldberg (2014) showed that skip-gram with negative sampling implicitly factorises a shifted PMI matrix. So count-based (PPMI + SVD) and prediction-based (word2vec) embeddings are two routes to the same object: a low-rank summary of co-occurrence statistics.
📝 Quick check (Level 3)
0/3 answered
1. A one-hot linear model has ___ weight(s) per word:
2. Dense vectors transfer knowledge to…
3. Skip-gram implicitly factorises…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
Can a classifier generalise to words it has never seen?
History.
Z. Harris, “Distributional structure” (Word, 1954); J. R. Firth (1957).
S. Deerwester et al., “Indexing by latent semantic analysis” (1990).
O. Levy and Y. Goldberg, “Neural word embedding as implicit matrix factorization” (NeurIPS 2014).
Exercise 2.
Corpus: sentences of one target word and 6 context words. 100 target words in 4 semantic classes (25 each, Zipfian frequencies). With probability 0.35 each context word comes from a pool typical of the target’s class, otherwise from 30 general words. Corpora of 300 or 3000 sentences.
Representations:
one-hot (210 dimensions);
20-dimensional SVD embeddings of the PPMI co-occurrence matrix (L2-normalised).
Task: a ridge classifier trained on 30% of the target words predicts the class of the other 70%, words it never saw.
corpus
one-hot
dense SVD
dense, rare words
dense, frequent words
dense nearest neighbour same class
300 sentences
0.229
0.771
0.628
1.000
0.780
3000 sentences
0.229
1.000
1.000
1.000
1.000
One-hot vectors cannot generalise: an unseen word’s vector has no overlap with any training word, so accuracy is at chance (0.25).
Dense vectors transfer class knowledge: 0.77 from only 300 sentences, perfect from 3000, with 20 dimensions instead of 210.
Rare words are the weak spot: with 300 sentences many rare words occurred only once or twice, so their vectors are noisy (0.63 against 1.00 for frequent words).
Seeds: seeds 1 and 2 gave dense accuracy 0.600/0.743 with 300 sentences and 0.986/1.000 with 3000; one-hot stayed at 0.257.
A design step: my first corpus (strong class context, uniform word frequencies) gave 1.00 even with 300 sentences. A weaker topic signal and Zipfian frequencies make it more realistic.
🐍 Exercise 2 (research): can a classifier generalise to words it has never seen?
Build PPMI + SVD word embeddings from corpora of two sizes and classify unseen words with one-hot vs dense representations.
Embeddings in AI, robotics and aerospace
Semantic search: matching queries to maintenance or incident reports that use different words for the same fault.
Clustering: grouping anomaly descriptions into failure modes.
Model inputs: the first layer of every language and code model.
Design lesson: represent items so that similar things are close; then what a model learns about one transfers to the others.
References
Z. Harris (1954). “Distributional structure.” Word 10(2–3).
S. Deerwester, S. Dumais, G. Furnas, T. Landauer and R. Harshman (1990). “Indexing by latent semantic analysis.” JASIS 41(6).
O. Levy and Y. Goldberg (2014). “Neural word embedding as implicit matrix factorization.” NeurIPS.
Bytes for 1000 dense one-hot vectors over 50,000 words at 4 bytes each?
200000000.
Problem 7Level 1
Number of non-zero entries in a one-hot vector?
1.
Level 2
Problem 8Level 2
Unseen-word accuracy with one-hot vectors?
0.229.
Problem 9Level 2
Unseen-word accuracy with dense vectors, 300 sentences?
0.771.
Problem 10Level 2
Unseen-word accuracy with dense vectors, 3000 sentences?
1.
Problem 11Level 2
Dense accuracy on rare words, 300 sentences?
0.628.
Problem 12Level 2
One-hot dimension in exercise 2?
210.
Problem 13Level 2
Fraction of target words used for training the classifier?
0.3.
Level 3
Problem 14Level 3
Prove that an embedding lookup equals one-hot × E.
Let e_i be the one-hot row vector with 1 in position i. Then (e_i E)_j = Σ_k (e_i)_k E_kj = E_ij, so e_i E is row i of E. Multiplying a matrix of one-hot rows by E therefore selects the corresponding rows, which a lookup does without the multiplication. ∎
Problem 15Level 3
Explain why one-hot accuracy was at chance for unseen words.
A ridge classifier on one-hot features learns one weight vector per training word. For a test word never seen in training, its one-hot feature has weight zero in every class, so all class scores are zero and the prediction is a fixed default class, right only for that class’s share of words (about 1/4). ∎
Problem 16Level 3
Compare count-based and prediction-based embeddings.
Count-based methods (PPMI + SVD, GloVe) compute global co-occurrence statistics and factorise them, which is transparent and fast for moderate vocabularies. Prediction-based methods (word2vec) learn by predicting context words with SGD, scale to huge corpora in streaming fashion, and implicitly factorise a similar matrix. With tuned hyperparameters they perform similarly. ∎
Problem 17Level 3
Advise on representing failure-mode terms for clustering incident reports.
Use pretrained embeddings (or a sentence-embedding model) rather than one-hot features, ideally adapted on in-domain text so that technical synonyms are close. Check nearest neighbours of key terms with engineers, and handle rare terms with subword-aware models such as fastText. ∎
Level 4
Problem 18Level 4
Dense nearest neighbour same-class rate with 300 sentences?
0.78.
Problem 19Level 4
Dense accuracy with 300 sentences, seed 1?
0.6.
Problem 20Level 4
Probability a context word comes from the class pool?
0.35.
Coding practice
🐍 Code 1 (Level 0): one-hot memory
V · n · bytes.
🐍 Code 2 (Level 0): embedding size
V · d.
🐍 Code 3 (Level 0): PMI
log P(x,y)/(P(x)P(y)).
🐍 Code 4 (Level 0): PPMI
max(PMI, 0).
🐍 Code 5 (Level 0): nearest neighbour
Most cosine-similar row.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
One-Hot vs Dense Representations: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.