LS·208
Phase 12 — AI Foundation
The twelfth phase: Python for AI, the mathematics, classical machine learning, deep learning, PyTorch, Transformers and large language models, with three projects (a classifier, a neural network and a tiny Transformer); the order and why, an entry check, step-by-step exit checks mapped to Volume 14, the projects with milestones and definitions of done, traps, a time budget, and a lab in which the simplest model wins until the data grows.
Start here: Phase 12 in one page
The last three phases turn you into an engineer who can build with AI, not just call an API. Phase 12 lays the foundation: enough mathematics and machine learning to know what a model is doing, why it fails, and how to measure it, ending with a Transformer you built yourself. You won't become a researcher, and you don't need to; you need to read a model card, a training curve and an evaluation table without being fooled.
The sequence
Python → math → classical ML → deep learning → PyTorch → Transformers → LLMs
Python's numerical tools first, because every later step uses them. Then the mathematics (linear algebra, calculus, probability) in the amounts ML actually needs. Classical ML before deep learning, because it teaches the core ideas (training vs test data, overfitting, baselines, metrics) on models small enough to understand. Then neural networks and PyTorch, then the Transformer architecture, then the large language models built from it.
The three projects, in one lab
The lab trained the three project models on the same task (recognising handwritten digits, 8 × 8 pixels) with 100, 300 and all 1,257 training images, tested on 540 held-out images:
| Model | Parameters | 100 images | 300 images | 1,257 images |
|---|---|---|---|---|
| logistic regression (the classifier) | 650 | 0.917 | 0.956 | 0.970 |
| MLP, one hidden layer (the neural network) | 4,810 | 0.922 | 0.961 | 0.976 |
| one-layer Transformer | 9,418 | 0.719 | 0.922 | 0.978 |
With little data, the simplest models won by a wide margin; the Transformer only caught up when given everything. Bigger architectures need more data (or pre-training) to pay off, which is why the first thing to build on any ML problem is a simple baseline.
How deep to go in Phase 12
Volume 17 defines six mastery levels; Phase 12's exit targets are (higher if you're on the AI track):
| Topic | Exit level | What that means here |
|---|---|---|
| Python for AI (NumPy, pandas) | L3 | vectorised code, no loops over rows |
| mathematics | L2–L3 | vectors, matrices, gradients, probability, used confidently |
| classical ML and evaluation | L3 | train/test splits, cross-validation, metrics, baselines |
| deep learning and PyTorch | L3 | write and debug a training loop |
| Transformers and tokenisation | L3 | implement attention; explain tokens and context |
| LLMs | L2–L3 | pre-training, fine-tuning, decoding, limits |
The sequence, step by step
| Step | Learn | Read | Exit check |
|---|---|---|---|
| 1 | the AI landscape and Python for AI | Vol. 14, Start Here, ch. 143–144 | load, clean and plot a dataset with pandas and NumPy |
| 2 | linear algebra, calculus, probability | Vol. 14, ch. 145–147 | compute a gradient by hand and check it numerically |
| 3 | classical ML and evaluation | Vol. 14, ch. 148–149 | project 1: the classifier with a proper evaluation |
| 4 | data engineering for AI | Vol. 14, ch. 150 | a reproducible dataset split with no leakage |
| 5 | deep learning and PyTorch | Vol. 14, ch. 151–152 | project 2: an MLP trained with your own loop |
| 6 | architectures and Transformers | Vol. 14, ch. 153–154 | project 3: a tiny Transformer |
| 7 | tokenisation, LLMs, decoding | Vol. 14, ch. 155–157 | explain temperature and top-p with a demo |
| 8 | the foundation-model lifecycle | Vol. 14, ch. 158 | describe how a chat model is trained, stage by stage |
Entry check
Comfortable Python (Phase 1), and school algebra. The mathematics chapters start from there.
The phase projects: a classifier, a neural network, a tiny Transformer
Goal. Build all three on the same task, so the comparison teaches you as much as the models do.
Milestones. Choose a dataset (digits, a small text classification set, or your own) → a fixed split and a baseline → classifier (logistic regression or gradient boosting) with cross-validation, a confusion matrix and the right metric → neural network in PyTorch with your own training loop, learning curves and early stopping → tiny Transformer (attention written by you, then compared with nn.TransformerEncoderLayer) → a learning-curve comparison like the lab's → a short report.
Done when. All three models are evaluated on the same untouched test set; you can explain each model's errors; the training loop shows train and validation loss; the report says which model you'd ship and why (cost, accuracy, explainability).
Stretch. Train a character-level Transformer on a small text and sample from it.
Common traps
- Skipping the baseline.
- Tuning on the test set.
- Reaching for deep learning first.
- Math avoidance: you don't need proofs, but you need the shapes and the gradients.
- Treating LLMs as magic instead of next-token predictors with known failure modes.
Exit criteria
- [ ] The three projects meet their "done when" list.
- [ ] I can write and debug a PyTorch training loop.
- [ ] I can explain attention and implement it.
- [ ] I can choose metrics and spot leakage.
- [ ] I scored at least 80% on 40 questions from Volume 14's quizzes, twice.
Time budget
Volume 14 (about 95,000 words) is about 7 hours of reading; with practice, about 28 hours, plus 3–4 weeks for the projects: about 7 weeks at 8 hours a week (longer if the mathematics is new to you).
Lab: the three projects on one task
Setup. scikit-learn's handwritten digits (1,797 images of 8 × 8 pixels, scaled to 0–1), split 70/30 with stratification: 1,257 training images and 540 test images, the same for every model. Training subsets of 100, 300 and 1,257 images, drawn with a fixed seed. Two CPU threads, PyTorch on CPU.
- Classifier: scikit-learn
LogisticRegression(650 parameters). - Neural network:
MLPClassifier, one hidden layer of 64 units (4,810 parameters). - Tiny Transformer: each image is a sequence of 8 rows ("tokens" of 8 pixels), projected to 32 dimensions plus a learned position embedding, one
TransformerEncoderLayer(4 heads, feed-forward 64, dropout 0.1), mean-pooled, a linear output; Adam, 60 epochs, batch 64 (9,418 parameters).
Results
| Training images | Model | Test accuracy | Training time |
|---|---|---|---|
| 100 | logistic regression | 0.917 | < 0.1 s |
| 100 | MLP | 0.922 | 0.1 s |
| 100 | tiny Transformer | 0.719 | 1.3 s |
| 300 | logistic regression | 0.956 | < 0.1 s |
| 300 | MLP | 0.961 | 0.4 s |
| 300 | tiny Transformer | 0.922 | 1.0 s |
| 1,257 | logistic regression | 0.970 | < 0.1 s |
| 1,257 | MLP | 0.976 | 1.0 s |
| 1,257 | tiny Transformer | 0.978 | 4.3 s |
(Times are from a second run; the first run's Transformer times were longer because of PyTorch warm-up.)
What it shows
- Simple models are strong baselines. Logistic regression was within a point of the best model at every size, with 1/14 of the Transformer's parameters and no training time to speak of.
- Capacity needs data. The Transformer was 20 points behind at 100 images and narrowly ahead at 1,257. Large language models resolve this by pre-training on vast data first, so a small labelled set only has to adapt them.
- The differences at full size are tiny. 0.970 vs 0.978 on 540 images is 4 images; to claim one model is better you'd need repeated splits and confidence intervals, which is part of project 1.
Try it yourself
- Repeat each configuration with 5 different seeds and report means and standard deviations.
- Write attention yourself (
softmax(QKᵀ/√d)V) and check it againstnn.MultiheadAttention. - Plot train and validation loss per epoch for the Transformer at 100 images: where does it overfit?
- Add gradient boosting (
HistGradientBoostingClassifier) to the table.
Revision sheet: Phase 12, AI foundation
Key ideas
- Sequence: Python → math → classical ML → deep learning → PyTorch → Transformers → LLMs.
- Projects: a classifier, a neural network, a tiny Transformer, compared on one task.
- Baselines first; evaluate on untouched test data; capacity needs data or pre-training.
- LLMs are pre-trained next-token predictors, then instruction- and preference-tuned.
Numbers to remember
- Lab (540 test digits): 100 images → LR 0.917, MLP 0.922, Transformer 0.719; 1,257 images → 0.970, 0.976, 0.978.
- Parameters: 650 / 4,810 / 9,418.
- Budget: ~28 study hours + 3–4 project weeks ≈ 7 weeks at 8 h/week.
Pitfalls
- No baseline; tuning on test data; leakage.
- Deep learning first; skipping the maths.
- Over-reading tiny differences.
Glossary
- Baseline: a simple model the real one must beat.
- Overfitting: learning the training data rather than the pattern.
- Cross-validation: repeated train/validation splits for more reliable estimates.
- Attention: mixing positions with weights from query–key similarity.
- Token: the unit a language model reads and predicts.