3.8 Neural language models
1. Learn
Section titled “1. Learn”- Karpathy, The spelled-out intro to language modeling: building makemorewhole video (1:57:45): counting bigrams, sampling, the negative log likelihood and smoothing 00:03:03–01:02:57; the same model as a one-layer neural net 01:02:57–01:56:16
- Karpathy, Building makemore Part 2: MLP00:01:48–01:00:49 (the Bengio et al. 2003 walkthrough, embedding table, hidden layer, cross-entropy, training loop, learning-rate search, train/val/test splits) and 01:05:27 (visualizing the embeddings); video length 1:15:39
- Bengio et al., A Neural Probabilistic Language Model (2003)sections 1–2
2. Bridge
Section titled “2. Bridge”A language model assigns a probability to every sequence by the chain rule, , so all it has to do is predict the next symbol. Here the symbols are characters and the sequences are names. A boundary token ”.” marks both the start and the end, so the model also learns how names begin and when they stop. The score is the mean negative log likelihood per character, the cross-entropy from 2.4; its exponential, the perplexity, is the effective number of equally likely choices (Karpathy, makemore part 1).
The bigram model looks only at the previous character. Its maximum-likelihood estimate is a table of counts with each row normalised. A pair never seen in training gets probability zero and an infinite loss, so add a pseudo-count to every cell first. Adding one is Laplace smoothing; like the prior in module 2.4’s MAP estimate, it pulls rare estimates away from the extremes (Jurafsky & Martin, SLP3 §3.6).
The MLP model (Bengio et al. 2003) looks at the last few characters. Each one is looked up in an embedding table , the vectors are concatenated, and a tanh hidden layer and a softmax produce the next-character distribution. The lookup is a matrix product with a one-hot vector, so backprop sends each row’s gradient back to the row of it came from. A character that appears twice in a batch gets two contributions, and they must be added: np.add.at, not assignment (Karpathy, makemore part 2). Because similar characters end up with similar vectors, what the model learns about one transfers to the others. A table of counts cannot do that, and it grows exponentially with the context length, while the MLP’s parameters grow linearly.
Sampling draws a character from the predicted distribution, appends it, and repeats until the boundary comes out. Dividing the logits by a temperature below 1 sharpens the distribution; above 1 flattens it (Holtzman et al. 2020, §3).
3. Explore
Section titled “3. Explore”stubs/m3_8_char_lm.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m3_8_char_lm.py and run mlc check 3.8. The tests are identical; import the results on the progress page.
Then train on all 32,000 names (locally; downloads about 230 KB once):
cd exercises && python projects/makemore_names.pyIt fits the bigram model, then trains the MLP with 10-dimensional embeddings and 200 hidden units for 30,000 steps (under a minute). The reference solution reaches a validation NLL of about 2.13, against 2.45 for bigrams, and samples names like briannan, dalyn and ammari. Part 4 replaces the fixed context window, first with recurrence and then with attention.
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.