◢◤ ARKANE_
SYS_TIME UTC+0
Work with me

Chapter 17

Hyperparameter Optimisation

Every machine learning algorithm has knobs, levers, and dials that must be set before training starts. How many trees. How deep. How fast the model should learn. These hyperparameters aren’t learned from data. They’re chosen. Choose poorly and a potentially excellent algorithm underperforms. Choose wisely and a merely adequate algorithm will beat a badly-tuned state-of-the-art one. This chapter is about searching that space systematically, since there’s no derivative of “validation accuracy with respect to number of trees” to follow.

17.1 Motivation

Hyperparameter optimisation is a search problem. There’s a function that can be evaluated (train a model, measure validation performance) but not differentiated, in a space that’s high-dimensional and expensive to explore. Each candidate configuration means training and validating a full model. In practice, random search beats grid search for the same compute budget. That’s counterintuitive until you understand the reason: hello again, dimensionality. In high-dimensional spaces most hyperparameters barely matter and a few dominate, so grid search wastes trials on exhaustive coverage of irrelevant dimensions. Random search samples the important ones more densely by not being tied to a fixed grid at all.

17.2 Exposition

17.2.1 Parameters vs. Hyperparameters

Parameters (model weights, tree split thresholds) are learned during training via optimisation. Hyperparameters (learning rate, n_estimators, max_depth) are set before training begins and aren’t differentiable with respect to the training process. They have to be searched, not solved for.

17.2.2 Search Strategies

Grid search enumerates every combination in a defined grid. Five values across four hyperparameters is 54=6255^4 = 625 training runs. Adding a fifth hyperparameter with five values pushes that to 3,125. This is the curse of dimensionality applied to hyperparameter search. It’s only practical with three or fewer hyperparameters and small grids.

Random search (Bergstra and Bengio 2012) samples random combinations from the search space instead of iterating through a fixed grid. With the same budget as grid search, it covers more of the dimensions that matter. If most hyperparameters are unimportant, then grid search wastes most of its budget holding the few important ones fixed. Random search doesn’t have that structural weakness. That paper is one of the most influential practical ML papers of the last fifteen years. It’s about 25 pages, and dense. For a Solutions Architect, understanding the impact is much more important than reading the paper.

Bayesian optimisation builds a probabilistic surrogate model of the objective function. That surrogate is a cheap stand-in for “validation score as a function of hyperparameters,” fast to query, unlike the real thing: training and validating an actual model. It uses that surrogate to decide where to sample next. It trades off exploitation (sample near the best known point) against exploration (sample where uncertainty is high). Two surrogate choices are common. A Gaussian Process models the objective directly, and grows more confident near points already tried. A Tree-structured Parzen Estimator (TPE) instead models which hyperparameter values tend to produce good results versus bad ones. TPE is the default surrogate used by Optuna, the hyperparameter-search library this chapter’s procedures build on. The surrogate updates after every evaluation. This is more efficient than random search when each evaluation is expensive. Optuna also gives automatic pruning (stopping bad trials early using intermediate results). Pruning is orthogonal to the search strategy. It can be layered onto random search or Bayesian optimisation.

17.2.3 The Validation Set Is Sacred

During hyperparameter search, the validation set scores every candidate configuration and becomes part of the optimisation process. That means the reported validation score after tuning is biased upward. Many configurations have been tried and the best-scoring one on that validation set was chosen. This is a selection process, not a neutral estimate. A held-out test set that never touches any part of the search is required to get an honest final estimate of performance.

17.3 Trade-offs and Limits

Strategy Pros Cons Use when
Grid Search Exhaustive over the grid, reproducible Exponential in hyperparameter count ≤\leq 3 hyperparameters, fast model
Random Search Simple, parallelisable, surprisingly effective No guidance from past trials Most situations, good default
Bayesian (Optuna) Most sample-efficient, prunes bad trials Trials run one after another, so less parallelism Expensive-to-evaluate models

17.4 Procedures

Procedure 17.1 (Define a Search Space). When to use: before running any search strategy.

Listing 17.1: Grid, distribution, and Optuna search-space definitions
# Grid: discrete lists
param_grid = {
    'n_estimators': [100, 200, 300],
    'max_depth': [3, 5, 7],
}

# Random: distributions
from scipy.stats import loguniform, randint
param_distributions = {
    'n_estimators': randint(50, 500),
    'learning_rate': loguniform(0.01, 0.3),
}

# Optuna: suggest_* calls inside the objective function
def objective(trial):
    n_estimators = trial.suggest_int('n_estimators', 50, 500)
    lr = trial.suggest_float('learning_rate', 0.01, 0.3, log=True)
    # ... train and return the metric

Procedure 17.2 (Run GridSearchCV). When to use: three or fewer hyperparameters, fast model, small grid.

Procedure 17.3 (Run RandomizedSearchCV). When to use: the default for most situations: same API, wider coverage for the same budget.

Procedure 17.4 (Set Up an Optuna Study). When to use: expensive models where each evaluation costs real time.

Listing 17.4: TPE-based Bayesian search with Optuna
import optuna
optuna.logging.set_verbosity(optuna.logging.WARNING)

study = optuna.create_study(
    direction='maximize',
    sampler=optuna.samplers.TPESampler(seed=42),
)
study.optimize(objective, n_trials=100)

print(study.best_params, study.best_value)

Common mistake: evaluating the final model on the same validation folds used during the search and reporting that number as “the” performance. Always hold out an untouched test set and evaluate on it exactly once, after the search concludes.

17.5 Worked Example

Running grid search, random search, and Optuna’s TPE sampler head-to-head on a synthetic, harder binary classification problem: 3,000 samples, 40 features, with label noise and overlapping classes, built so overfitting is a real risk. All three tune the same six XGBoost hyperparameters: n_estimators, max_depth, learning_rate, subsample, colsample_bytree, and min_child_weight. Grid search only has room for two values at the edge of each range. That’s 26=642^6 = 64 corner combinations. Random search and Optuna get the same 64-evaluation budget. But they sample continuously across the same six ranges, including the interior grid search can never reach:

Listing 17.5: Grid, random, and Optuna search under a matched 64-evaluation budget across six hyperparameters
param_grid = {
    'n_estimators': [100, 1000], 'max_depth': [2, 12],
    'learning_rate': [0.01, 0.3], 'subsample': [0.5, 1.0],
    'colsample_bytree': [0.5, 1.0], 'min_child_weight': [1, 10],
}  # two values per dimension: 2**6 = 64 corner combinations
gs = GridSearchCV(model, param_grid, cv=5, scoring='roc_auc', n_jobs=-1)

# Same ranges as the grid. Unlike Optuna below, this samples
# learning_rate uniformly, not on a log scale.
param_dist = {
    'n_estimators': randint(100, 1001), 'max_depth': randint(2, 13),
    'learning_rate': uniform(0.01, 0.29), 'subsample': uniform(0.5, 0.5),
    'colsample_bytree': uniform(0.5, 0.5), 'min_child_weight': randint(1, 11),
}
rs = RandomizedSearchCV(
    model, param_dist, n_iter=64, cv=5, scoring='roc_auc',
    random_state=42, n_jobs=-1,
)

# objective samples the same six ranges (learning_rate on a log
# scale), one trial at a time, with its 5 CV folds in parallel
study.optimize(objective, n_trials=64)

Measured results:

Strategy Best CV AUC Test AUC Wall time
Grid search 0.9185 0.8933 55.5s
Random search 0.9198 0.8942 18.5s
Optuna (TPE) 0.9225 0.8949 171.5s

This time the measurement matches the theory it’s testing. Random search beats grid search on both CV AUC (0.9198 vs. 0.9185) and test AUC (0.8942 vs. 0.8933). It’s a modest edge, but a consistent one. It comes from a real structural weakness in the grid. With only two values per dimension, every one of grid search’s 64 combinations sits at a corner of the search space: max or min subsample, max or min max_depth, and so on, in every combination. It can never place a trial in the interior, where the best setting for a hyperparameter often sits. Random search and Optuna sample continuously. They can reach the interior. Optuna’s TPE sampler does best of the three on both metrics: CV AUC 0.9225, test AUC 0.8949. That’s what the Trade-offs table predicts for the one strategy that uses past trials to guide the next one, instead of sampling blind.

Optuna’s full 64-trial run also took the longest: 171.5 seconds, against random search’s 18.5 and grid’s 55.5. Judged on that number alone, Optuna looks like the expensive choice. Most of that gap is parallelism, not Optuna itself. Grid and random search spread all 320 fits across every core at once. Optuna runs one trial at a time, five folds in parallel, because each trial depends on the results before it. TPE’s own overhead is well under a second. But its real advantage isn’t wall time at a matched budget. It’s needing fewer evaluations to reach a given quality. Tracked trial by trial, Optuna reached random search’s final CV AUC (0.9198) after just 5 trials, in 5.7 seconds. That’s faster than random search’s full run, and almost ten times faster than grid’s. This is the sample efficiency the Trade-offs table promises for expensive-to-evaluate models. Stop Optuna once it’s caught up, and it wins on accuracy and time both. Run it to a budget sized for a cruder strategy instead, and its sequential trials cost time with nothing to show for it.

AWS SageMaker Equivalent
from sagemaker.tuner import (
    HyperparameterTuner,
    IntegerParameter,
    ContinuousParameter,
)

tuner = HyperparameterTuner(
    estimator=estimator,
    objective_metric_name='validation:auc',
    hyperparameter_ranges={
        'n_estimators': IntegerParameter(50, 500),
        'max_depth': IntegerParameter(2, 10),
        'learning_rate': ContinuousParameter(0.01, 0.3),
    },
    max_jobs=50, max_parallel_jobs=5, strategy='Bayesian',
)
tuner.fit({'train': train_data_s3})

When to prefer SageMaker Hyperparameter Tuning: when the model takes hours to train and the search needs to parallelise across managed instances. For models that train in seconds to minutes (like this chapter’s Worked Example), Optuna running locally is faster and cheaper than provisioning a fleet of training jobs.

17.6 History and Further Reading

Bergstra and Bengio’s “Random Search for Hyper-Parameter Optimization” is the paper that established random search as the strong practical default (Bergstra and Bengio 2012). Thakur, Ch. 7, covers practical hyperparameter tuning with code (Thakur 2020). Géron’s Hands-On Machine Learning, Ch. 2, shows GridSearchCV and RandomizedSearchCV directly on a full pipeline. It shows the mechanics behind the random-search argument above (Géron 2019). Murphy’s Introduction, Ch. 5.3, gives the Bayesian optimisation formalism (Murphy 2022).

17.7 How This Connects

This chapter builds on Chapter 13’s cross-validation: proper validation is what scores each candidate. It also builds on Chapter 16’s model evaluation: the metric being optimised during search matters as much as the search strategy. It enables the ensemble hyperparameters from Chapter 12 (n_estimators, max_depth) to be tuned rather than guessed. It’s often confused with feature selection: hyperparameter search asks what settings make an algorithm perform best. Feature selection asks what inputs make it perform best. Different questions about different things.

17.8 Exercises

  1. [W] Explain why grid search over 5 hyperparameters with 5 values each needs 55=3,1255^5 = 3{,}125 training runs. Explain why that number would not shrink even if only 2 of the 5 hyperparameters mattered.

  2. [W] State which search strategy is the reasonable default for: (a) tuning 2 hyperparameters on a model that trains in under a second; (b) tuning 8 hyperparameters on a neural network that takes 20 minutes per training run.

  3. [A] Using Listing 17.5’s measured results, compute the percentage-point gap between grid search’s and random search’s test AUC (0.8933 vs. 0.8942), and explain what about the grid’s construction accounts for it.

  4. [A] Using Procedure 17.4, sketch how you would add pruning to the Optuna study from this chapter’s Worked Example if XGBoost’s training reported intermediate validation AUC after every 10 boosting rounds, using optuna.pruners.MedianPruner.

  5. [E] A colleague argues Optuna should always be used “because it’s the most sample-efficient.” Using this chapter’s measured trial counts and times (Optuna matched random search’s final score in 5 trials and 5.7 seconds, but its full 64-trial run took 171.5 seconds against random search’s 18.5), write the two or three sentences you’d say back about when that advice is right and when it isn’t.

  6. [E] A team reports “we tuned hyperparameters and our validation AUC went from 0.91 to 0.97” as the headline result. Using this chapter’s Validation Set Is Sacred section, write the question you’d ask before accepting that 0.97 figure.

  7. [R] Read the Bergstra and Bengio (2012) abstract. Identify the specific dimensionality condition under which they show random search’s advantage over grid search, and relate it to why this chapter’s own six-hyperparameter, corners-only grid comparison showed the predicted gap.

17.9 Mental Model

Hyperparameter optimisation is like wine tasting: searching a space of configurations for the ones that perform well on a validation set, without being able to taste every bottle. Grid search tries every wine on the menu, in a fixed order. That’s exactly its problem: a long menu takes forever to get through. Random search grabs bottles at random. Bayesian optimisation asks the sommelier to suggest the next bottle, based on which ones were liked before. That’s an advantage when there are a lot of bottles and tasting each one is expensive. It doesn’t matter at all when the menu is short.

Bergstra, James, and Yoshua Bengio. 2012. “Random Search for Hyper-Parameter Optimization.” Journal of Machine Learning Research 13: 281–305.
Géron, Aurélien. 2019. Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems. 2nd ed. O’Reilly Media.
Murphy, Kevin P. 2022. Probabilistic Machine Learning: An Introduction. MIT Press.
Thakur, Abhishek. 2020. Approaching (Almost) Any Machine Learning Problem. Abhishek Thakur.
online /books/classical-data-science/ch17-hyperparameter-optimization Newcastle, UK v0.1.0 // rss