🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Hyperparameter Tuning: Grid Search, Random Search and Optuna

Module 8: Tree-Based ModelsHyperparameter Tuning🟢 Free Lesson

Advertisement

Hyperparameter Tuning: Grid Search, Random Search and Optuna

ℹ️Module 8 — Tree-Based Models

This lesson covers systematic approaches to hyperparameter optimization, from exhaustive grid search to intelligent Bayesian methods and modern tools like Optuna.

1. Hyperparameters vs Parameters

Understanding the distinction between hyperparameters and learned parameters is fundamental to model optimization.

Parameters are learned from data during training — weights in a neural network, split thresholds in a decision tree. Hyperparameters control the learning process itself and must be set before training begins.

AspectParametersHyperparameters
Set whenDuring trainingBefore training
Learned fromDataManual / search
ExamplesWeights, biases, split pointsLearning rate, max depth, n_estimators
Optimized viaGradient descent, EMGrid search, random search, Bayesian

Formal Definition

A machine learning model has parameters learned from training data via:

Hyperparameters govern the learning process:

The goal of hyperparameter tuning is to find:

where is the validation loss — we never use test data for this.

Why Tuning Matters

A well-tuned model can outperform a more complex model with default settings. The bias-variance tradeoff is directly controlled by hyperparameters:

  • Too restrictive (e.g., max_depth=2): high bias, underfitting
  • Too flexible (e.g., max_depth=50): high variance, overfitting
  • Just right: optimal generalization

2. Grid Search — Exhaustive Exploration

Grid Search is the most straightforward approach: define a discrete set of values for each hyperparameter and evaluate every possible combination.

Algorithm

GridSearch Algorithm FlowchartInputmodel, param_grid, X, y, cvInitializebest_score = -∞, best_params = nullGenerate Gridproduct(param_grid)For EachCombination?YesCross Valscore = CrossValScore(model, params, cv)Comparescore > best_score?if yes → updateUpdate Bestbest_score = scoreNoReturnbest_params, best_scoreSearch Space VisualizationBestmax_depth →n_estimators ↑ComplexityWith d params and k values each:k^d combinations5 params × 5 values = 3,125 evaluations

Python Implementation

The Curse of Dimensionality in Grid Search

With hyperparameters and values per parameter, grid search evaluates combinations:

HyperparametersValues eachCombinationsTime (10s each)
25254 min
3512521 min
456251.7 hours
553,1258.7 hours
6515,6254.3 days

Grid Search vs Random Search

The visual below demonstrates how random search covers the search space more efficiently. Each axis represents one hyperparameter, and colored dots represent evaluations.

Grid Search vs Random SearchGrid SearchBest8 x 8 = 64 evaluationsRandom SearchBest15 random evaluations

Key Insight: Grid search wastes budget on unimportant hyperparameters. If max_depth matters more than subsample, grid search still evaluates all subsample values for every max_depth.

3. Random Search — Efficient Sampling

Random search samples hyperparameter combinations from specified distributions. Bergstra and Bengio (2012) showed random search is more efficient than grid search when only a few hyperparameters are truly important.

Why Random Search Wins

The key insight: if one hyperparameter (e.g., learning rate) dominates performance, grid search wastes evaluations per dimension. Random search explores the dominant dimension more effectively.

Log-Uniform Distributions for Learning Rate

Learning rates span orders of magnitude, so uniform sampling is suboptimal:

Comparison: Grid vs Random

CriterionGrid SearchRandom Search
CoverageUniform gridStratified sampling
Curse of dimensionalityExponentialLinear in n_iter
Important dimensionsWasted budgetBetter coverage
ReproducibilityDeterministicDepends on seed
ParallelizationDifficultTrivial
Budget efficiencyLowHigh

4. Bayesian Optimization — Intelligent Search

Bayesian optimization builds a surrogate model of the objective function and uses an acquisition function to decide where to sample next.

The Loop

Bayesian Optimization LoopnextTrue objectiveSurrogate (GP)ObservationsNext evalAcquisition

Gaussian Process Surrogate

The surrogate model is typically a Gaussian Process (GP):

where is the mean function and is the kernel (covariance function). Given observations , the posterior predictive is:

Acquisition Functions

The acquisition function balances exploration and exploitation:

Expected Improvement (EI):

where is the best observed value, and are the standard normal CDF and PDF.

Upper Confidence Bound (UCB):

where controls exploration. Higher = more exploration.

Thompson Sampling: Sample and optimize the sample.

Exploration vs Exploitation

The acquisition function encodes a fundamental tradeoff:

  • Exploration: sample where uncertainty is high (learning the landscape)
  • Exploitation: sample where predicted value is good (refining the optimum)

EI naturally balances both: high increases exploitation, high increases exploration (only when is near ).

5. Optuna — State-of-the-Art Optimization

Optuna uses Tree-structured Parzen Estimator (TPE) and supports advanced features like pruning, conditional hyperparameters, and rich visualization.

TPE Algorithm

TPE models instead of — a key departure from Gaussian Process approaches:

  1. Split observations into "good" () and "bad" () using quantile
  2. Model good observations:
  3. Model bad observations:
  4. Maximize ratio:

TPE is non-parametric (uses kernel density estimation), scales better than GP-based methods, and naturally handles conditional hyperparameters.

Basic Usage

Conditional Hyperparameters

Optuna handles conditional spaces natively — parameters that only apply when another parameter takes a specific value:

Pruning — Early Termination of Bad Trials

Pruning terminates unpromising trials early, saving computational budget:

Pruning Strategies

PrunerMechanismBest For
MedianPrunerPrune if below median of previous trialsGeneral purpose
SuccessiveHalvingPrunerEliminate bottom fraction each roundLarge search spaces
HyperbandPrunerBudget allocation with early stoppingResource-constrained
PatientPrunerWait N trials before pruningNoisy objectives

Optuna Visualization

Optuna Optimization ProcessTrial HistoryT1T2T3prunedT4T5T6prunedT7T8T9T10prunedT11T12Best Value0.820.850.880.91Trials

6. Learning Rate Schedules

Learning rate scheduling reduces the learning rate during training, allowing fast convergence early and fine-grained updates late.

Common Schedules

where is the initial learning rate and is the current step.

ScheduleFormulaCharacteristics
Step DecayReduce by factor every steps
Exponential DecaySmooth continuous decay
Cosine AnnealingPeriodic warm restarts
Linear Warmup for Avoid early instability
Polynomial DecayFlexible power control
Learning Rate Schedules0.000.050.100.150.200.250.30Step DecayExponentialCosine AnnealingWarmup + DecayEpochLearning Rate

Practical Implementation

7. Early Stopping

Early stopping halts training when validation performance stops improving, preventing overfitting and saving compute.

Mathematical Formulation

Let be the validation loss at epoch . Training stops when:

where is the patience parameter and is a tolerance threshold.

Implementation

Early Stopping for Gradient Boosting

Patience and Overfitting

Early Stopping: Patience = 10beststopTrain lossVal lossEpoch

8. Implementation in Python — Complete Pipeline

End-to-End Tuning Pipeline

Multi-Objective Optimization

Saving and Loading Studies

Key Takeaways

ℹ️Summary

Grid Search is simple but exponential — use only when search space is small (3-4 parameters).

Random Search is efficient for low-effective-dimension problems — always better than grid with equal budget.

Bayesian Optimization is sample-efficient — best when evaluations are expensive (deep learning, hyperparameter tuning of expensive models).

Optuna with TPE is the modern standard — handles conditional parameters, pruning, multi-objective, and scales to hundreds of trials.

Early stopping is cheap insurance — always use it for iterative learners like gradient boosting.

Learning rate schedules enable fast convergence with fine-tuned solutions — cosine annealing with warm restarts is often a strong default.

References

  1. Bergstra, J., and Bengio, Y. (2012). Random search for hyper-parameter optimization. JMLR, 13, 281-305.
  2. Snoek, J., Larochelle, H., and Adams, R. P. (2012). Practical Bayesian optimization of machine learning algorithms. NeurIPS.
  3. Akiba, T., et al. (2019). Optuna: A next-generation hyperparameter optimization framework. KDD.
  4. Li, L., et al. (2018). Hyperband: A novel bandit-based approach to hyperparameter optimization. JMLR, 18(185), 1-52.
  5. Smith, L. N. (2017). Cyclical learning rates for training neural networks. WACV.

Need Expert Data Science Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement