Intermediate

Hyperparameter Optimization

Hyperparameter Optimization

Estimated duration
35 minutes
Related questions
8
Related algorithms
4
Status
Log in to track

Lesson objectives

  • Know the main HPO methods.
  • Design a suitable search space.
  • Compare metaheuristic methods with strong baselines.

Explanation

Model performance depends heavily on parameters that are not learned by training: the learning rate, depth, regularization... Tuning them is a costly black-box optimization problem.

Methods: grid search is exponentially expensive; random search (Bergstra & Bengio 2012) is often more efficient because only a few hyperparameters really matter; Bayesian optimization builds a surrogate model (Gaussian Process or TPE) and picks points with an acquisition function; multi-fidelity methods (Successive Halving and Hyperband) stop weak trials early; and evolutionary and swarm algorithms (GA, PSO, DE and CMA-ES) handle mixed spaces easily.

Space design: logarithmic for the learning rate, weight decay, C and gamma; integer for the number of layers and units; categorical for the optimizer and activation function; and conditional when needed.

Evaluation rules: an equal evaluation budget across methods, several seeds, a Train/Validation/Test split that never touches the test set, and reporting the total time.

Grid searchBergstra & BengioParameter 2
With the same number of trials, random search covers more values of each important parameter.

Mathematics

The problem

\lambda^* = \arg\min_{\lambda\in\Lambda} \frac{1}{K}\sum_{k=1}^{K}\mathcal{L}\left(A_\lambda(D_{train}^{(k)}), D_{val}^{(k)}\right)

Python code

import numpy as np
from sklearn.datasets import load_wine
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
X, y = load_wine(return_X_y=True)
rng = np.random.default_rng(0)
best = None
for _ in range(20):  # random-search baseline with a 20-evaluation budget
    p = dict(n_estimators=int(rng.integers(50, 300)), max_depth=int(rng.integers(2, 12)))
    s = cross_val_score(RandomForestClassifier(**p, random_state=0), X, y, cv=5).mean()
    best = max(best or (s, p), (s, p), key=lambda z: z[0])
print(best)

Simplified teaching code that works with NumPy only; for research experiments use a documented library and fix the random seed.

Practical exercise

In the Neural Networks Lab optimize the learning rate, number of units and Dropout with PSO, then compare with random search under the same budget.

Common mistakes

  • Comparing PSO with grid search instead of random search.
  • Searching the learning rate on a linear scale.
  • Tuning hyperparameters and then reporting the same validation result as the final result.

Summary

HPO is a costly black-box problem; random search is a mandatory baseline, and the final evaluation is on an untouched test set.

References

There are no references recorded for this lesson.

Short quiz

5 questions are chosen at random from 8 questions linked to this lesson, and the quiz is graded on the server.

Links within the lab

Algorithms
Models
Optimization types
Related lessons
In the paths