Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 40 ยท Hyperparameter Tuning

Bayesian Search with Optuna

Go beyond grid and random search: define an Optuna study, sample hyperparameters with TPE, prune bad trials and read the results for XGBoost and LightGBM models.

Grid search tries every combination. Random search tries random ones. Both ignore what earlier trials taught them. Bayesian optimization keeps a model of "which hyperparameters tend to give good scores" and uses it to choose the next trial. Optuna is the most popular Python library for this, and it works well with XGBoost and LightGBM.

This lesson builds an Optuna study step by step. It assumes you know the basic tuning workflow from A Sensible Tuning Workflow and the main parameters from Tree Complexity Parameters.

The three pieces of an Optuna study

  1. An objective function that receives a trial, asks it for hyperparameter values, trains a model and returns a score.
  2. A study that owns the history and decides which values to try next.
  3. A sampler (the default is TPE, the Tree-structured Parzen Estimator) that is the actual search strategy.
flowchart LR
  S["Study + sampler"] -->|"suggests params"| T["Objective: train and score"]
  T -->|"returns score"| S
  S --> B["best_params, best_value"]

The sampler uses all previous scores to propose better parameters.

You ask the trial for values with suggest_* methods: suggest_int, suggest_float (with log=True for rates and penalties that span orders of magnitude), and suggest_categorical.

A first study

In the site's runner Optuna is installed with micropip. On your machine pip install optuna is enough.

Python Tune LightGBM with 12 TPE trials

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the choices that make this work well: log-scale ranges for rates and penalties, a small space of seven numbers, and a fixed seed for the sampler so the study is repeatable.

Reading the history

Every trial is stored. study.trials_dataframe() returns them as a table you can sort, filter and plot:

Python Compare early trials with late trials

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In one run the mean AUC of the first six trials was about 0.889 and of the last six about 0.908. Typically the later trials cluster around better regions than the first ones, because TPE had history to learn from. The first few trials (by default ten, set to 5 here because the budget is small) are random "start-up" trials. In a tiny study like this the gain is modest; TPE shines with 50 or more trials.

Pruning hopeless trials

Training many full models wastes time on settings that are clearly bad after a few rounds. Optuna can prune a trial: you report an intermediate value during training and call trial.should_prune(). For XGBoost and LightGBM there are ready-made callbacks in the optuna-integration package (XGBoostPruningCallback, LightGBMPruningCallback). The pattern looks like this (read-only here, because it needs a full native training loop):

Python
def objective(trial):
    params = {"objective": "binary:logistic", "eval_metric": "auc",
              "eta": trial.suggest_float("eta", 0.02, 0.3, log=True),
              "max_depth": trial.suggest_int("max_depth", 2, 9)}
    pruning = optuna.integration.XGBoostPruningCallback(trial, "validation-auc")
    booster = xgb.train(params, dtrain, num_boost_round=2000,
                        evals=[(dvalid, "validation")],
                        early_stopping_rounds=50, callbacks=[pruning], verbose_eval=False)
    return booster.best_score

study = optuna.create_study(direction="maximize", pruner=optuna.pruners.MedianPruner())

MedianPruner stops a trial whose intermediate score is worse than the median of earlier trials at the same step. Pruning shines when each trial takes minutes.

Practical tips

  • Search ranges matter more than the sampler. Start wide, look at the best trials, then narrow.
  • Do not tune the number of trees. Use early stopping inside each trial and return the best validation score. Store best_iteration with trial.set_user_attr.
  • Use cross-validation or a fixed validation set, never the test set. With many trials, even the validation score becomes optimistic, so confirm the final choice on untouched data.
  • Budget. 50 to 200 trials is typical. More than the problem needs wastes time and overfits the validation set.
  • Reproducibility. Seed the sampler and the model. Parallel studies are not perfectly repeatable.
  • Persistence. optuna.create_study(storage="sqlite:///study.db", study_name="...", load_if_exists=True) lets you stop and resume a study.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the TPE sampler do that random search does not?

    TPE models which regions of the search space produced good scores and samples more from them. It gives no guarantee of the global optimum.
  2. 2. Which suggest call is best for a learning rate searched between 0.01 and 0.3?

    Rates differ by orders of magnitude, so a log-uniform range gives small and large values a fair chance.
  3. 3. How should the number of boosting rounds be handled inside an Optuna objective?

    Early stopping finds a good tree count for each trial, so the search space stays small and the test set stays untouched.
  4. 4. What does a pruner do?

    Pruners such as MedianPruner stop a trial whose intermediate result is poor, saving compute for better candidates.

Summary

  • Optuna runs an objective function many times; a sampler (TPE by default) uses past scores to propose new parameters.
  • Use log-scale ranges for learning rates and penalties, and integer ranges for depth and leaves.
  • Use early stopping inside each trial instead of tuning the tree count.
  • Pruners cut hopeless trials short; storage lets you resume studies.
  • Keep a separate test set, because heavy tuning overfits the validation score.

Next: Tuning LightGBM and XGBoost Side by Side