Foundations to Frontier
Tests

One-Hot vs Dense Representations

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.

Level 0: Foundation

★☆☆☆☆ 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. 1. Two different one-hot vectors are…
  2. 2. Dense embeddings place similar words…
  3. 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. 1. Embedding parameters for 50,000 words × 300 dims:
  2. 2. PMI of words co-occurring twice as often as chance:
  3. 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.
import torch.nn as nn
emb = nn.Embedding(num_embeddings=50000, embedding_dim=300)   # trainable lookup table
vecs = emb(token_ids)                                          # (batch, seq, 300)
emb.weight.data.copy_(torch.tensor(pretrained_vectors))        # optional: start from pretrained vectors
🐍 Exercise 1: one-hot, lookups and cosine
Build one-hot vectors, compute cosine similarity and implement an embedding lookup.
📝 Quick check (Level 2)
0/3 answered
  1. 1. PyTorch's embedding layer:
  2. 2. Count-based embeddings come from…
  3. 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. 1. A one-hot linear model has ___ weight(s) per word:
  2. 2. Dense vectors transfer knowledge to…
  3. 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

  1. Z. Harris (1954). “Distributional structure.” Word 10(2–3).
  2. S. Deerwester, S. Dumais, G. Furnas, T. Landauer and R. Harshman (1990). “Indexing by latent semantic analysis.” JASIS 41(6).
  3. O. Levy and Y. Goldberg (2014). “Neural word embedding as implicit matrix factorization.” NeurIPS.