CSC3104 · Module 3 of 9

Training & Evaluation

Train, validate & diagnose classifier behaviour

~56 min to study properly

Training is a loop: forward pass to get predictions, compute a loss such as cross-entropy, backpropagate gradients, and update the weights with an optimiser like SGD or AdamW. Repeat for many epochs.

Checkpoint · the examiner asks

Why does Batch Normalization behave differently during training versus evaluation mode?

In one minute

Training is forward → loss → backprop → optimiser step, repeated over epochs. Tune on validation, touch test once, and guard against leakage. Read the curves: train falls but val rises means overfitting; both high means underfitting. On imbalanced data accuracy lies — use precision, recall, F1 and the confusion matrix.

Traps examiners look for
  • Reporting accuracy on a 99%-negative dataset.
  • Forgetting model.eval(): BatchNorm and Dropout behave differently at inference.
  • Tuning hyper-parameters on the test set — that's leakage too.
Read

Study notes

01The training loop

Every supervised network trains with the same loop, repeated over epochs (passes through the training set) in mini-batches:

  1. Forward pass — compute predictions for a batch.
  2. Loss — measure how wrong they are; for classification, cross-entropy = −log(probability of the true class).
  3. Backward pass — backpropagation computes the gradient of the loss with respect to every weight.
  4. Update — an optimiser moves the weights against the gradient: SGD with momentum, or AdamW (Adam with decoupled weight decay).
for x, y in loader:
    opt.zero_grad()
    loss = F.cross_entropy(model(x), y)
    loss.backward()
    opt.step()

The learning rate is the most important hyper-parameter: too high and the loss jumps around or diverges; too low and training crawls. Warm-up plus cosine decay is a robust default schedule.

02Splits and leakage

  • Training set — the model learns from it.
  • Validation set — you choose hyper-parameters, architecture, and the epoch to stop at.
  • Test set — touched once, at the end, to report performance.

Because you make many decisions using the validation set, it slowly becomes "seen"; the test set is the honest estimate.

Leakage in vision is often subtle: the same patient's scans in train and test, near-duplicate frames from one video split across sets, or augmenting before splitting. Split by the unit that must generalise — patient, video, location — not by image.

03Diagnosing with learning curves

Plot training and validation loss per epoch:

PatternDiagnosisFixes
Train ↓, validation ↓ then ↑Overfitting (high variance)more data/augmentation, dropout, weight decay, early stopping, smaller model
Both high and flat, close togetherUnderfitting (high bias)bigger model, train longer, less regularisation, better features
Jagged, not decreasingLearning rate too highlower LR, warm-up, gradient clipping
Both low and closeGood fitkeep the best-validation checkpoint

Batch normalisation behaves differently in the two modes: in training it normalises with the current batch's statistics and updates running averages; in evaluation it uses the stored running averages. Dropout is active only in training. Forgetting model.eval() at test time gives noisy, wrong results.

04Metrics that don't lie

Accuracy fails on imbalanced data: if 99% of scans are healthy, "always healthy" scores 99% and finds no disease.

From the confusion matrix:

  • Precision = TP / (TP + FP) — of my positive predictions, how many were right?
  • Recall = TP / (TP + FN) — of the real positives, how many did I find?
  • F1 = 2PR / (P + R) — their harmonic mean (punishes imbalance between them).
  • Specificity = TN / (TN + FP).

Which matters depends on the cost of each mistake: cancer screening wants high recall; a spam filter that deletes mail wants high precision. Curves (ROC, precision–recall) show the trade-off across thresholds.

Watch it done

Worked examples

Worked example 1

Metrics from a confusion matrix

A defect detector on 1,000 parts: TP = 45, FN = 15, FP = 30, TN = 910. Compute accuracy, precision, recall and F1.

    Worked example 2

    Where to stop training

    Validation loss by epoch: 0.92, 0.71, 0.58, 0.51, 0.49, 0.50, 0.54, 0.61. Training loss keeps falling. Which checkpoint do you keep, and what is happening after it?

      Vocabulary

      Key terms

      Soft labels beat hard ones

      Mixup blends two images and their labels; CutMix pastes a patch of one image onto another and mixes labels by area (e.g. 0.7 cat, 0.3 dog). Combined with label smoothing, these are standard in modern ImageNet training recipes and reduce over-confident predictions.

      Practise

      Quick check

      0 of 8 correct so far. Wrong answers come with an explanation — and the buddy can unpack them.

      Q1Train loss 0.02, validation loss 1.9 and rising. Best first fix?

      Q2Cancer screening: what matters most if missing a cancer is very costly?

      Q3Which set should be used to choose the learning rate?

      Q4Training loss falls; validation loss rises. Diagnosis?

      Q5What happens if you forget model.eval() before testing a network with BatchNorm and Dropout?

      Q6TP = 30, FP = 10, FN = 30. Recall?

      Q7How should a medical-scan dataset be split to avoid leakage?

      Q8A loss curve that jumps up and down without decreasing suggests…

      Exam-style

      Written practice

      Short answer5 marks

      Why does batch normalisation behave differently in training and evaluation mode?

      Short answer5 marks

      Explain precision and recall, and give one application where each matters most.

      Long answer12 marks

      Your defect-detection CNN reaches 98% training accuracy and 81% validation accuracy, and defects are only 4% of parts. Diagnose the situation, list the fixes you'd try in order, and explain how you would evaluate the final model honestly.

      Watch

      Videos

      Fresh picks are chosen by AI every day.
      Hands-on

      Metrics from a confusion matrix

      4 guided tasks: predict, experiment, explain — then challenge yourself. Plus 2 coding exercises you run right in the browser.

      Start the lab