Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Machine Learning Professional

Distributed tuning with Optuna and MLflow

Databricks Machine Learning Professional · Lesson 6 of 22 · 41:24

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.

In this lesson

Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: Optuna objectives and studies, MlflowStorage for the study state, MlflowSparkStudy to run trials on Spark executors, parallelism, and getting the best model back. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Two hundred trials

Pebblecove suggests a nightly price to every host, with a gradient boosted model trained on one machine. The team wants two hundred tuning trials of five minutes each, so running them one after another would take more than sixteen hours. This lesson covers Optuna, and the two MLflow classes that spread its trials across the cluster, and log every one of them.

Objective and study

Optuna is an open source library for hyperparameter tuning. You write an objective function that receives a trial, asks it for values, trains a model, and returns the score to minimize or maximize. The trial suggests each value from the search space: suggest_float for a number, with log set to true for a scale like the learning rate, suggest_int for a whole number, and suggest_categorical for a choice from a list. A sampler picks the next values, and the default, TPE, learns from the trials already finished, so the search focuses on the promising regions. And a pruner, by default the median pruner, stops a trial early when the intermediate scores it reports are worse than the median of earlier trials at the same step.

MlflowStorage

To run trials in parallel, they need a shared storage, where each trial can read what the others found. MlflowStorage provides it: it makes the MLflow tracking server the storage of Optuna, inside the experiment that you pass by its identifier. It groups its writes into batches, flushed every second or every hundred items by default, so that many trials at once do not overload the tracking server. So the study and its trials are recorded in the MLflow experiment, where you can compare them like any other runs.

MlflowSparkStudy

MlflowSparkStudy wraps an Optuna study, and launches its trials on the PySpark executors of the cluster. You create it with a study name and the storage, and you can pass a sampler and a pruner, or keep the defaults, TPE and the median pruner. Then optimize takes the objective, n_trials, the total number of trials, and n_jobs, the number of trials that run at the same time. When it finishes, best_params gives the best combination, and every trial stays in the experiment for comparison.

Parallelism and the best model

Choosing n_jobs is a trade off. More trials at once finish the study sooner, but an adaptive sampler learns less between them, so a value well below n_trials keeps the search smart, and the cluster sets the upper limit. Each trial runs on an executor, so the objective trains a single node model, like scikit-learn or LightGBM, on data that fits in the memory of a worker. When the study ends, train the final model with the best parameters on the full training data, log it to MLflow, and register it in Unity Catalog. A Spark ML model already uses the whole cluster for each fit, so it is tuned from the driver, one trial at a time, or with the CrossValidator of lesson two.

Recap

Let's recap. The objective asks the trial for values with the suggest methods, and returns a score. MlflowStorage makes the MLflow tracking server the shared storage of the study. MlflowSparkStudy runs the trials on the Spark executors, with n_trials in total and n_jobs at once. More trials at once finish sooner, but TPE learns less between them. And the pruner stops weak trials early, before the best parameters are refit and registered.

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.

  1. The objective tunes a learning rate between 0.001 and 0.3. Which call samples it on a logarithmic scale?

    • trial.suggest_int("lr", 0.001, 0.3)
    • trial.suggest_float("lr", 0.001, 0.3, log=True)
    • trial.suggest_categorical("lr", [0.001, 0.3])
    • trial.suggest_float("lr", 0.001, 0.3, step=0.01)
    Show the answer

    trial.suggest_float("lr", 0.001, 0.3, log=True)

    Suggest_float with log set to true samples the rate on a logarithmic scale, so small values get as much attention as large ones. “trial.suggest_int("lr", 0.001, 0.3)” is for whole numbers, which a rate is not. “trial.suggest_categorical("lr", [0.001, 0.3])” would only ever try the two values in its list. And “trial.suggest_float("lr", 0.001, 0.3, step=0.01)” is a linear grid of steps, which spends most trials on the large rates.

  2. The team has a budget of 64 trials and sets n_jobs to 64, so all of them start at once. What is the main drawback?

    • The trials no longer use the MLflow storage
    • Each trial gets fewer folds of the data
    • The pruner is turned off for every trial
    • The sampler cannot learn from earlier trials
    Show the answer

    The sampler cannot learn from earlier trials

    When every trial starts at once, none has finished, so TPE has no results to learn from, and the search is close to random. “The trials no longer use the MLflow storage” is wrong, since the trials still share MlflowStorage. “Each trial gets fewer folds of the data” is wrong, since n_jobs does not change how each trial splits its data. And “The pruner is turned off for every trial” is wrong, since the pruner still compares the trials that report intermediate scores.

  3. A trial reports its validation score after each boosting round, and it is far behind the other trials at the same round. What stops it early?

    • The pruner, when the trial checks should_prune
    • The sampler, when it picks the next values
    • MlflowStorage, when it flushes a batch
    • n_trials, once the budget is reached
    Show the answer

    The pruner, when the trial checks should_prune

    The median pruner compares the reported score with earlier trials at the same step, and should_prune tells the objective to stop. “The sampler, when it picks the next values” only chooses the values of the next trials. “MlflowStorage, when it flushes a batch” only stores the results. And “n_trials, once the budget is reached” would end the whole study, not one weak trial.