4.2 Embeddings and language modeling
1. Learn
Section titled “1. Learn”- Stanford CS224n: NLP with Deep Learning (Winter 2025)lecture 1 "Word Vectors" and lecture 2 "Word Vectors and Language Models", with their slides and notes
- Jurafsky & Martin, Speech and Language Processing (3rd ed. draft), ch. 3 "N-gram Language Models"§3.1–3.3 (n-grams, evaluation, perplexity) and §3.6 (smoothing and interpolation)
2. Bridge
Section titled “2. Bridge”n-gram models. Module 3.8 predicted characters from the previous few characters. The same idea over words, with counts instead of a network, is the n-gram model: assume the next word depends only on the previous (the Markov assumption) and estimate as a ratio of counts. Longer contexts predict better but are seen less often, and most trigrams in new text never occurred in training (Jurafsky & Martin, SLP3 ch. 3).
Interpolation mixes the orders: with weights that sum to 1, where is an add-one unigram so that no word ever gets probability zero. Because each term is a distribution, so is the mixture. The weights are tuned on held-out data, and the model is compared by perplexity, the exponential of the mean negative log probability, exactly as in 3.8 (SLP3 §3.2, §3.6).
Meaning from company. An n-gram model treats “king” and “queen” as unrelated symbols. Distributional semantics represents each word by the words around it: count how often each pair occurs within a few positions, and words used alike get similar rows (CS224n lecture 1 notes). Raw counts are dominated by “the” and “and”, so reweight them by positive pointwise mutual information, , which keeps only pairs that co-occur more than chance. A truncated SVD then compresses the rows to dense vectors, as PCA did in 1.2. Word2vec’s skip-gram objective implicitly factorizes a shifted version of the same PMI matrix (Levy & Goldberg 2014).
Using the vectors. Similarity is the cosine of the angle between vectors, so frequent words with long vectors do not win by length. Analogies take and find the nearest other word. They work when a relation, such as gender, shows up as a consistent offset, (Mikolov et al. 2013), and they also expose biases in the training text (Bolukbasi et al. 2016).
3. Explore
Section titled “3. Explore”stubs/m4_2_ngrams_and_vectors.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m4_2_ngrams_and_vectors.py and run mlc check 4.2. The tests are identical; import the results on the progress page.
Then run both parts on Tiny Shakespeare (locally; the text is cached if you did 4.1):
cd exercises && python projects/shakespeare_ngrams_vectors.pyWith the reference solution, held-out perplexity falls from 589 (unigram) to 345 (bigram) and 337 (trigram): most trigrams in new text were never seen, so the third order adds little. The vectors find richard, edward, henry next to king and solve he : she :: him : her.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.