Machine Learning Fundamentals

Generalization, Evaluation, and Optimization

In this chapter, we explore the core principles that distinguish successful deep learning models from those that fail in production. We look beyond raw accuracy to understand **Generalization**—the ability of a model to perform on unseen data.

We'll examine these through the lens of high-stakes environments where 'memorizing' the training data isn't just a technical fail, but a safety risk.

Generalization and the 'Gap'

Generalization is the model's ability to perform on unseen data. The Generalization Gap is the difference between training performance and validation performance. In Autonomous Vehicles, a model must generalize across cities, lighting conditions, and weather—not just memorize the streets it was trained on.

To ensure generalization, we monitor for Overfitting (memorizing noise) and Underfitting (failing to capture the signal).

Advanced Evaluation: Group K-Fold & Leakage

Standard random splitting is often dangerous in Medical AI. If a patient has 10 X-rays, and we put 8 in training and 2 in testing, the model might "recognize the patient" rather than the pathology. This is **Data Leakage**.

PythonRuns entirely in your browser — nothing is sent to a server.

  • Group K-Fold: Ensures that all data from a single group (e.g., one patient or one specific vehicle journey) stays together in either the training or testing set.
  • Stratified CV: Ensures that rare classes (like a rare tropical disease) are represented equally in every fold of our cross-validation.

Regularization: Penalizing Complexity

To prevent a model from becoming too complex and overfitting, we use **Regularization**:

  • L1/L2 Regularization: Penalizes large weights, forcing the model to stay "simple."
  • Dropout: Randomly "turning off" neurons during training, which forces the network to find multiple independent paths to the same answer—a form of redundancy that mirrors biological neural systems.
  • Early Stopping: Automatically halting training the moment validation performance begins to degrade.

Practice Questions

Question 1

Why is Group K-Fold Cross-Validation essential for medical imaging datasets?

  • It makes the training process faster on GPUs
  • It prevents data leakage by ensuring all scans from one patient are in the same fold
  • It automatically labels the images
  • It reduces the resolution of the images to save space

Question 2

What is the primary purpose of Dropout during training?

  • To delete unimportant data from the disk
  • To prevent overfitting by forcing the network to learn redundant, robust features
  • To shut down the computer when it gets too hot
  • To make the final predictions faster