Hyperparameter tuning with Optuna
Databricks Machine Learning Associate · Lesson 13 of 19 · 1:44:06
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Machine Learning Associate exam. In this lesson: grid, random, and Bayesian search, Optuna studies, objectives, and trials, parallel trials for single-node models on the executors of a cluster, and tuning Spark ML models. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Idle workers
The damage classifier of Juniper Rides is a scikit-learn gradient boosting model, with five hyperparameters to set. A data scientist tries values by hand, one run at a time, on the driver, while the eight workers of the cluster stay idle. This lesson covers grid, random, and Bayesian search, how Optuna runs a search, how to spread trials across the executors, and how to tune Spark ML models.
Grid, random, and Bayesian search
Hyperparameters, like the depth of a tree or the learning rate, are set before training, and tuning searches for the values that give the best validation score. Grid search tries every combination of a list of values, which is thorough, but its cost multiplies with each new hyperparameter. Random search samples a fixed number of combinations from ranges, and often finds results as good as a grid, with far fewer trials. Bayesian search uses the results of past trials to choose the next values, so it moves toward promising regions, and the default sampler of Optuna, TPE, works this way. In scikit-learn, GridSearchCV and RandomizedSearchCV run the first two, and in Spark ML, ParamGridBuilder builds a grid.
Optuna
Optuna is an open source library for hyperparameter tuning, which Databricks recommends, along with Ray Tune, now that Hyperopt is gone from recent ML runtimes. You write an objective function that takes a trial, suggests values with methods like suggest_int, suggest_float, and suggest_categorical, trains a model, and returns the score to optimize. A study then runs the search: create_study sets the direction, minimize by default, or maximize for a score like F1, and optimize calls the objective for a number of trials. The default sampler, TPE, is Bayesian, and the default pruner, MedianPruner, stops unpromising trials early. And best_params and best_value return the winning values and their score.
Parallel trials for single node models
A scikit-learn or XGBoost model trains on a single node, so a loop of trials uses only the driver, while the executors of the cluster stay idle. The MLflow integration of Optuna fixes this: MlflowStorage uses the MLflow tracking server as the shared storage of the study, and MlflowSparkStudy runs trials in parallel on the PySpark executors. Its optimize method takes the objective, the number of trials, and n_jobs, the number of trials that run at the same time. More parallel trials finish sooner, but each new trial knows fewer finished results, so a Bayesian search adapts less. Ray Tune, included in the ML runtime, is another way to spread trials across a cluster.
Tuning Spark ML models
A Spark ML model is already distributed: its training spreads across the executors. So it is tuned with the tools of Spark ML: ParamGridBuilder for the grid, and CrossValidator or TrainValidationSplit, which lesson fourteen covers. Their parallelism parameter evaluates several models at once, and a value up to about ten is usually enough. A plain Optuna study can also tune a Spark ML model, one trial at a time, since each trial already uses the whole cluster.
Recap
Let's recap. Grid search tries every combination, random search samples them, and Bayesian search learns from past trials. In Optuna, an objective function takes a trial, suggests values, and returns a score, and a study optimizes it in the right direction. MlflowSparkStudy, with MlflowStorage, runs the trials of single node models in parallel on the executors, with n_jobs. More parallel trials are faster, but a Bayesian search adapts less. And a distributed Spark ML model is tuned with a parameter grid and CrossValidator, with its parallelism parameter.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A team tunes six hyperparameters, each with ten candidate values, and has a budget of about 100 trials. Which search fits best?
- A full grid search of every combination
- A grid of the first value of each
- Manual tuning, one at a time
- A Bayesian search with about 100 trials
Show the answer
A Bayesian search with about 100 trials
A full grid would need a million trials, while a Bayesian search spends its hundred trials where results look promising. “A full grid search of every combination” is ten to the power of six combinations, far beyond the budget. “A grid of the first value of each” tests a single combination. And “Manual tuning, one at a time” ignores how hyperparameters interact, and needs many manual runs.
An objective function returns the F1 score of the damage classifier, and the study was created with the default settings. What goes wrong?
- The study minimizes F1, seeking the worst model
- Optuna cannot optimize a classification metric
- The default sampler is a plain grid search
- best_params returns the last trial, not the best
Show the answer
The study minimizes F1, seeking the worst model
The direction is minimize by default, so the study must be created with direction set to maximize for a score like F1. “Optuna cannot optimize a classification metric” is wrong, since the objective can return any metric. “The default sampler is a plain grid search” is wrong, since the default sampler, TPE, is Bayesian. And “best_params returns the last trial, not the best” is wrong, since best_params returns the values of the best trial.
A data scientist tunes a scikit-learn random forest with an Optuna study, on a cluster of eight workers, but only the driver is busy. What should they change?
- Switch to GridSearchCV
- Raise n_trials in optimize
- MlflowSparkStudy, with n_jobs above 1
- Add workers to the cluster
Show the answer
MlflowSparkStudy, with n_jobs above 1
MlflowSparkStudy runs trials in parallel on the executors, and n_jobs sets how many run at the same time. “Switch to GridSearchCV” would still train on the driver, and try more combinations. “Raise n_trials in optimize” adds trials, but they still run one after another on the driver. And “Add workers to the cluster” adds workers that would stay idle too.