Databricks Machine Learning · Topic 5 of 6
MLflow Runs, Manual Logging and the UI
Databricks ML Associate: log metrics, params, artifacts and models by hand in MLflow, find the best run with search_runs, and read the MLflow UI.
- 5 min
- log_param for inputs, log_metric for numeric results (with optional step), log_artifact for local files, mlflow.<flavor>.log_model for models.
- log_metrics and log_params take dictionaries; log_dict and log_figure save objects as artifacts.
- MlflowClient().search_runs returns Run objects; mlflow.search_runs returns a pandas DataFrame.
What it is
An MLflow run is one execution of training code. It records parameters (inputs such as max_depth), metrics (results such as rmse), tags, artifacts (files) and models, and runs are grouped into experiments.
Autologging captures much of this for supported libraries. The exam asks you to log by hand, to find the best run with the client API, and to know what the MLflow UI shows.
How it works
Manual logging inside a run
import mlflow
with mlflow.start_run(run_name="rf_depth_8") as run:
mlflow.log_param("max_depth", 8)
mlflow.log_metric("rmse", 4.27)
mlflow.log_artifact("residuals.png")
mlflow.sklearn.log_model(model, "model")
| Call | Logs |
|---|---|
| mlflow.log_param / log_params | One or a dict of input settings |
| mlflow.log_metric / log_metrics | One or a dict of numeric results; log_metric also takes a step for curves over epochs |
| mlflow.log_artifact | A local file (log_artifacts for a whole directory) |
| mlflow.log_dict / log_figure / log_text | A dictionary, a matplotlib or plotly figure, or text, saved straight to an artifact file |
| mlflow.set_tag / set_tags | Free form labels such as team or dataset version |
| mlflow.<flavor>.log_model | The model in that flavor, such as mlflow.sklearn or mlflow.pyfunc |
Parameters are logged once per run, while a metric can be logged many times with increasing steps. A model logged with a signature and input_example records its expected input and output schema; Unity Catalog registration requires the signature.
Calls outside a with block act on the active run, and mlflow.end_run() closes it. The with form closes the run for you, even on errors.
Finding the best run with the client API
MlflowClient().search_runs filters and sorts runs in one or more experiments.
from mlflow import MlflowClient
client = MlflowClient()
best = client.search_runs(experiment_ids=[exp_id],
filter_string="metrics.rmse < 5 AND params.max_depth = '8'",
order_by=["metrics.rmse ASC"], max_results=1)[0]
print(best.info.run_id, best.data.metrics["rmse"])
It returns a list of Run objects; best.info holds the run_id and status, best.data holds metrics, params and tags. mlflow.search_runs takes similar arguments but returns a pandas DataFrame with columns such as metrics.rmse and params.max_depth.
Sort direction depends on the metric: ASC for errors and losses (RMSE, log loss), DESC for scores (accuracy, F1, AUC). Parameter values are strings, so filters quote them.
Worked example
Three runs log rmse of 4.9, 4.3 and 5.2 and r2 of 0.81, 0.86 and 0.78. order_by=["metrics.rmse ASC"] with max_results=1 returns the second run. Ordering by metrics.r2 DESC returns the same run here, because it also has the highest r2.
What the MLflow UI shows
| UI area | What you find |
|---|---|
| Experiment runs table | Every run with start time, duration, user, status, source notebook, and columns for chosen params and metrics |
| Run detail page | Parameters, metrics, tags, Git commit or notebook revision, and the logged datasets |
| Metric charts | Each metric over steps, for example loss per epoch |
| Artifacts tab | Logged files and the model folder: MLmodel file, environment files (conda.yaml, requirements.txt), signature and input example |
| Compare runs | Side by side table plus parallel coordinates, scatter and contour plots across runs |
| Registered model link | The model version created from the run, if it was registered |
From the run page you can register the logged model directly, and the artifacts tab shows code to load it.
How the exam asks about this
- "Return the run with the highest validation AUC in an experiment." Use search_runs with order_by=["metrics.val_auc DESC"] and max_results=1.
- "Save a confusion matrix image created during training with the run." Use mlflow.log_artifact on the saved file, or mlflow.log_figure on the figure object.
- "Where do you check which Python packages a logged model needs?" The Artifacts tab, in the model's requirements.txt or conda.yaml.
What to remember for the exam
- log_param for inputs, log_metric for numeric results (with optional step), log_artifact for local files, mlflow.<flavor>.log_model for models.
- log_metrics and log_params take dictionaries; log_dict and log_figure save objects as artifacts.
- MlflowClient().search_runs returns Run objects; mlflow.search_runs returns a pandas DataFrame.
- order_by=["metrics.x DESC"] with max_results=1 finds the top run; use ASC for error metrics.
- Filters use metrics., params. and tags. prefixes, and param values are compared as strings.
- The UI shows params, metrics, tags, source, duration, artifacts, the model's MLmodel and environment files, and comparison plots.
Do not sort an error metric DESC. "Best run" for RMSE, MAE or log loss means the lowest value, so order ascending.
Common mistakes
Logging a metric with log_param. Parameters are inputs fixed per run and stored as strings. A result such as accuracy must be logged with log_metric so it can be charted, sorted and compared.
Passing an in-memory object to log_artifact. log_artifact uploads a file from a local path. For a figure or dictionary in memory use log_figure or log_dict, or save it to a file first.
Expecting search_runs to return a DataFrame. The client method returns a list of Run objects. Only the fluent mlflow.search_runs returns a pandas DataFrame by default.
Check yourself
Which call returns the best run by F1 as a Run object?
MlflowClient().search_runs(experiment_ids=[...], order_by=["metrics.f1 DESC"], max_results=1), taking the first element of the returned list.
How do you log a chart file saved to disk during training?
Call mlflow.log_artifact with the local file path inside the active run; it appears in the run's Artifacts tab.
What are three things the MLflow UI shows for a run?
Its parameters and metrics, the source notebook with duration and user, and the artifacts, including the logged model's MLmodel and environment files.
Topic 5 of 6 in Databricks Machine Learning
Test yourself on Databricks Machine Learning
The full Databricks Machine Learning Associate question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Databricks. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.