Sub topic 1
What hyperparameter optimisation is and why automate it
Prerequisites
This chapter assumes you can write Python functions and handle exceptions. It also assumes you have trained and validated a model with scikit-learn, and that you know the difference between parameters and hyperparameters. Finally, it assumes you understand train/validation splits and cross-validation.
The problem
A parameter is a value the model learns from data, such as a weight in a linear model. A hyperparameter is a setting you choose before training, such as the learning rate, tree depth, or regularisation strength. Hyperparameter optimisation (HPO) is the search for the hyperparameter values that give the best validation score.
Formally, you have a function from a hyperparameter configuration to a score. Evaluating it means training and validating a model, so it is expensive. It has no gradient you can follow, and it is noisy, because a different data split or random seed changes the score.
Why manual tuning breaks down
Tuning by hand works for one or two settings. It fails as the search grows, for three reasons:
- Settings interact. The best learning rate depends on the batch size, so you cannot tune each setting alone.
- You forget what you tried. Without a record, you repeat runs and cannot say why a configuration was chosen.
- Your attention is limited. You try a handful of configurations, and a machine can try hundreds.
Grid search automates the loop but scales badly.
from itertools import product
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC
X, y = load_iris(return_X_y=True)
best_score, best_config = 0.0, None
for c, gamma in product([0.1, 1, 10, 100], [0.001, 0.01, 0.1, 1]):
score = cross_val_score(SVC(C=c, gamma=gamma), X, y, cv=3).mean()
if score > best_score:
best_score, best_config = score, (c, gamma)
print(best_score, best_config)Two hyperparameters with four values each cost 16 evaluations. Five hyperparameters with four values each cost 1,024. A grid also spends equal effort on every region, even after it is clear that some regions are poor.
What an automated search does
An HPO tool replaces the fixed grid with a loop that learns from earlier results. Optuna is a Python library built around this loop.
The later chapters cover each stage: the objective that trains and scores, the search space, the samplers that propose configurations, and the pruners that stop weak runs early. The loop also stores every result, so you can inspect and resume the search.
Common errors to avoid from the start
- Searching too wide or too narrow. A range that excludes the best value cannot find it. A range that is far too wide wastes trials.
- Treating the best score as the true score. The winner of many trials is partly lucky. Its validation score is optimistic, so keep a separate test set for the final estimate.
- Tuning what does not matter. A few hyperparameters usually dominate the result. Spending trials on the rest adds cost and little gain.
Exercise
A grid search tunes six hyperparameters with five values each. How many models must it train, and why is random or adaptive search often a better choice here?
Show answer
It trains 5^6 = 15,625 models. The grid spends equal effort on every combination, including regions that are clearly poor. Random or adaptive search uses a fixed trial budget and, in the adaptive case, concentrates later trials near configurations that scored well.