Natural Language Processing
Follow text from Unicode and token counts to document retrieval and grounded answers.
Follow text from Unicode and token counts to document retrieval and grounded answers.
Before you start. Machine Learning and the attention lesson in Deep Learning.
Use Python 3.12 for the examples. Install the dependencies with python -m pip install numpy scikit-learn torch. Run each week as a separate script. Try the worked example on paper first, then change one assumption in the lab and explain the result. All datasets are small or generated locally.
The 12 weeks
| Week | Lesson | What you’ll do |
|---|---|---|
| 1 | Text, Unicode, and tokenization | Compare code points, bytes, and tokens before counting text. |
| 2 | Bag of words and TF–IDF | Build a baseline and inspect the terms that drive its predictions. |
| 3 | N-grams, smoothing, and perplexity | Assign probability to unseen transitions and measure the cost on held-out text. |
| 4 | Word embeddings and similarity | Compare a sparse context vector with a compressed representation. |
| 5 | Sequence labelling and entity-level F1 | Measure entity boundaries separately from token accuracy. |
| 6 | Sequence-to-sequence models | Compare a decoder trained on the right prefix with its own generated prefix. |
| 7 | Transformers for language | Compare causal and bidirectional masks on the same sequence. |
| 8 | Pretraining objectives | Compare predicting the next token with reconstructing a token from both sides. |
| 9 | Decoding and adaptation | Compare greedy and sampled outputs, then measure a small adaptation. |
| 10 | Search and retrieval | Compare keyword and dense retrieval with a known relevance label. |
| 11 | Grounded answering and abstention | Separate retrieval misses from unsupported answers. |
| 12 | Capstone · Answer questions from documents with citations | Compare a naive answerer with a retriever that can abstain. |
Further reading · Jurafsky and Martin · Speech and Language Processing