Databricks Machine Learning Associate Question 99
Single answerYou are working on a machine learning experiment in Databricks and have logged multiple runs using MLflow. You want to programmatically identify the run with the best performance (lowest validation loss) using the MLflow Client API. Which of the following code snippets correctly identifies the best run by fetching all runs from an experiment?
- A
best_run = sorted(client.search_runs(experiment_id, "metrics.validation_loss IS NOT NULL"), key=lambda run: run.data.metrics['validation_loss'])[0]
- B
best_run = max(client.list_run_infos(experiment_id), key=lambda run: run.data.metrics['validation_loss'])
- C
best_run = min(client.search_runs(experiment_id, "metrics.validation_loss IS NOT NULL"), key=lambda run: run.data.metrics['validation_loss'])
- D
best_run = client.get_run(experiment_id, order_by=['metrics.validation_loss ASC'])[0]
Show answer and explanation
Correct answer: C
Explanation
The MLflow Client API provides the search_runs method to query runs from an experiment. To identify the best run based on the lowest validation loss, it is best to use the min function with a key that accesses the 'validation_loss' metric. This approach ensures that you efficiently identify the desired run without unnecessary sorting or incorrect method usage.
- A. Incorrect.
This option uses
sortedto order the runs but does not use theminfunction for efficiency. While it appears correct at first glance, directly sorting all runs isn't the optimal or required approach to identify the best run. - B. Incorrect.
This option incorrectly uses
list_run_infos, which only retrieves metadata about runs and does not include metrics data such as 'validation_loss'. Therefore, it would not work for identifying the best run based on metrics. - C. Correct.
This option correctly uses
minon the output ofsearch_runswith a filter for non-nullvalidation_lossmetrics and identifies the run with the lowest validation loss, making it the correct answer. - D. Incorrect.
This option attempts to use
get_runwith an incorrect argument (order_by), which is not a valid parameter for this method. The code would throw an error.