← Course Home Module 2 · Machine Learning Basics
Module 2 · Foundations

Machine Learning Basics

Both papers are, at their core, supervised machine learning systems. This module gives you the full vocabulary of that game: data, labels, loss, gradient descent, splits, overfitting, and the first evaluation metrics.

▶
Audio recap
A ~2-minute spoken summary of this module — great for revision on the go.

2.1 What "learning from data" means

Classical programming: a human writes explicit rules ("if the DNA letter at position 51 is T, then…"). Machine learning flips this: we show the program examples and let it find the rules itself. Instead of writing the recognizer, we write a procedure that produces a recognizer from data.

In supervised learning — the setting of both papers — the data is a set of pairs (x, y): an input x and its correct answer, the label y. The numbers describing each input are its features. Paper 1's inputs are 101-letter DNA windows labeled with a disease; Paper 2's inputs are tactile images labeled with a material.

The one-sentence definition Supervised learning: given many (input, label) pairs, find a function f such that f(x) ≈ y — and, crucially, such that this still holds on new inputs the model never saw during training.

2.2 Classification vs regression

Two flavors of supervised learning, split by the type of label:

For a K-class problem, labels are usually written as one-hot vectors: a vector of K entries with a single 1 at the true class and 0 everywhere else. Example: with 4 classes, "class 2" (counting from 0) is encoded as (0, 0, 1, 0). This turns a label into a probability distribution that puts all its mass on the truth — exactly the target that cross-entropy (Module 1.7) compares against.

In the papers Paper 1: 21 classes — 20 monogenic diseases plus a "Not a Disease" class, so a benign variant has somewhere to go. Its one-hot labels have 21 entries. Paper 2: 32 material classes (wood, steel, fabric, …). Both models end in a softmax over their class count.

2.3 The loss function

To improve a model you must first score it. A loss function compresses "how wrong is the model on this example?" into a single non-negative number — 0 would mean perfect, larger means worse. Training is then simply: adjust the weights to make the average loss over the training data smaller.

For multi-class classification the standard loss is cross-entropy, which you met in Module 1.7: with a one-hot target it equals −log ptrue, punishing the model heavily when it assigns the true class a tiny probability (assigning 0.9 costs 0.105; assigning 0.01 costs 4.6).

Why not just use accuracy as the training signal? Accuracy only changes when a prediction flips from wrong to right — it is a staircase, flat almost everywhere, so its gradient is zero and gradient descent has no slope to follow. Cross-entropy changes smoothly as probabilities shift, so every tiny weight nudge produces feedback. Train on cross-entropy; report accuracy.

2.4 Gradient descent and the learning rate

The loss is a function of all the model's weights. The gradient ∇L points uphill in weight-space (Module 1.4), so we repeatedly step the other way:

w ← w − η ∇L

Here η (eta) is the learning rate — the step size. Too large, and you overshoot the valley, bouncing around or diverging entirely. Too small, and training crawls, possibly never reaching a good solution in your compute budget. It is the single most important hyperparameter to get right.

Computing ∇L over the entire dataset per step is wasteful, so in practice we use stochastic (mini-batch) gradient descent: each step estimates the gradient from a small random batch of examples. Paper 1 uses a batch size of 32 — each update looks at 32 DNA windows. One full pass through the training set is called an epoch; training runs for many epochs.

Tiny example Suppose L(w) = w² (minimum at w = 0), starting at w = 3 with η = 0.1. The gradient is 2w = 6, so the update is w ← 3 − 0.1·6 = 2.4, then 1.92, 1.536… — a steady slide toward 0. With η = 1.1 instead: w ← 3 − 1.1·6 = −3.6, then +4.32, then −5.18… each step overshoots and the loss grows. That's divergence.

2.5 Train / validation / test splits

We never judge a model on the data it trained on — it may have simply memorized it. So the dataset is split into three disjoint parts:

SplitUsed forHow often it's touched
Training setComputing gradients and updating weightsEvery step
Validation setTuning hyperparameters (learning rate, architecture) and deciding when to stop (early stopping, Module 3.6)Every epoch, but never for gradients
Test setThe final, honest estimate of real-world performanceOnce, at the very end
In the papers Paper 1 splits its data 70% / 15% / 15% into train / validation / test, and additionally uses stratified k-fold cross-validation (Module 9) to check that results are stable across different splits.
The cardinal sin: test-set leakage If any information from the test set influences training — a duplicated example, a preprocessing statistic computed on all the data, or a researcher repeatedly tweaking the model to raise the test score — the test result becomes an overestimate of real performance. The validation set exists precisely so that all tuning happens there, leaving the test set untouched until the single final measurement.

2.6 Overfitting and underfitting

Underfitting: the model is too simple (or under-trained) to capture the real pattern — it performs poorly even on the training data. Overfitting: the model captures the training data too well, memorizing its noise and quirks, and so fails on new data. The goal is neither: it is generalization.

The classic diagnostic: as training proceeds, training loss keeps falling while validation loss bottoms out and starts rising. The model is now improving its memorization of the training set at the expense of everything else.

The fix families (each gets its own treatment later): more data, data augmentation (Module 4 — Paper 1 augments its rare-disease examples heavily), regularization such as dropout and weight decay (Module 3.5), and early stopping (Module 3.6).

Everyday analogy A student who memorizes last year's exam answers word-for-word scores perfectly on those exact questions and collapses on a fresh paper. A student who understood the subject does well on both. Overfitting is memorizing the answer key; generalizing is understanding the subject. Your examiners will happily accept this analogy — then ask you for the technical symptom (train loss ↓, validation loss ↑).

2.7 First evaluation metrics

Accuracy — the fraction of predictions that are correct — is intuitive but dangerously misleading under class imbalance. If 99% of screened patients are healthy, a "model" that predicts healthy for everyone scores 99% accuracy while detecting zero sick patients. Both papers face imbalance (rare diseases; unevenly sampled materials), so they must report better metrics.

The bookkeeping tool is the confusion matrix. For two classes ("positive" = has the disease):

Predicted positivePredicted negative
Actually positiveTP (true positive)FN (false negative — a miss)
Actually negativeFP (false positive — a false alarm)TN (true negative)

From it:

precision = TPTP + FP     recall = TPTP + FN     F1 = 2 · precision · recallprecision + recall

Precision: of everything I flagged, how much was real? Recall: of everything real, how much did I flag? F1 is their harmonic mean — it is dragged toward the smaller of the two, so a model can't hide a terrible recall behind a great precision.

Worked example A variant classifier makes 100 predictions: TP = 40, FP = 10, FN = 20, TN = 30. Precision = 40/(40+10) = 0.80. Recall = 40/(40+20) ≈ 0.67. F1 = 2·(0.80·0.67)/(0.80+0.67) ≈ 0.73. Accuracy = (40+30)/100 = 0.70 — note how each metric tells a different story about the same model.
In the papers Paper 1 reports 94.7% accuracy and a macro F1 of 0.93 and an AUC-PR of 0.98 — precisely because accuracy alone would be uninformative with rare disease classes. Deeper metrics (ROC and PR curves, AUC, specificity, per-class analysis) arrive in Module 9.
Exam warm-up — say it out loud
  1. Define supervised learning in one sentence, and say what the "features" and "labels" are in each paper.
  2. Why can't we train directly on accuracy?
  3. What is each of the three data splits for, and what exactly goes wrong if the test set leaks into training?
  4. Your training loss is still falling but validation loss has been rising for five epochs. Diagnose it and name two fixes.

Module 2 Quiz

10 questions. Splits, losses, and metrics come up in nearly every oral exam.

← Previous
Module 1: Math Foundations