Aller au contenu

Leçon 6 · Experiment Tracking

Search and Query Runs

Filter and sort MLflow runs with the search syntax, quote values correctly, order results, and load runs into a table for your own analysis.

After a few hundred runs, scrolling is hopeless. MLflow has a small filter language that works in the web interface and in code. This lesson shows how to use it and which traps to avoid.

Search from Python

Python
import mlflow

runs = mlflow.search_runs(
    experiment_names=["churn-model"],
    filter_string="metrics.val_auc > 0.8 AND params.model = 'xgboost'",
    order_by=["metrics.val_auc DESC"],
    max_results=5,
)
print(runs[["run_id", "metrics.val_auc", "params.max_depth"]])

mlflow.search_runs returns a pandas table by default, with columns named like metrics.val_auc and params.max_depth. For objects instead of a table, use MlflowClient().search_runs.

Field prefixes

Prefix Meaning Value type
metrics. Logged metrics Number
params. Parameters Text
tags. Tags Text
attributes. Run info such as status or start_time Depends on the field
datasets. Input dataset name, digest or context Text

Operators and rules

  • Numbers support =, !=, >, >=, < and <=.
  • Text supports =, !=, LIKE (case matters) and ILIKE (case ignored).
  • IS NULL and IS NOT NULL work for tags and params.
  • Join conditions with AND. OR is not supported in this syntax.
  • Wrap field names that contain spaces in backticks, such as metrics.`f1 score`.
  • Put values in single or double quotes where they are text.

Parameters are text. Write params.max_depth = '10', with quotes. Asking for "depth greater than 5" does not work like a number comparison. If you need numeric comparison, also log the value as a metric, or load the results and convert the column in pandas.

Ordering and limits

order_by takes a list, such as ["metrics.val_auc DESC"]. The default direction is ascending. Without order_by, runs are listed newest first by start time. Combine ordering with max_results to get the top few.

When you need OR logic, load the runs and filter the table in pandas:

Python
table = mlflow.search_runs(experiment_names=["churn-model"])
wanted = table[table["params.model"].isin(["xgboost", "forest"])]

Why text parameters surprise people

This stand-in compares text and numbers in plain Python. The same surprise appears whenever numbers are stored as strings.

Python Stand-in: text versus number comparison

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.

Build a mini filter

To understand how a filter works, here is a small stand-in that supports only AND and a few operators. It is far simpler than the real parser.

Python Stand-in: a tiny AND-only filter

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.

Searching logged models and traces

MLflow 3 can also search logged models with mlflow.search_logged_models, and traces with mlflow.search_traces. The idea is the same: filter, order and limit. Field names differ, so read the documentation for each function.

Good habits

  1. Decide the selection metric before searching for winners.
  2. Leave out failed runs with a condition such as attributes.status = 'FINISHED'.
  3. Restrict by data version, so you compare like with like.
  4. Save the query in your notebook or script, so others can repeat your selection.

A full example workflow

Suppose you ran fifty trials and want a shortlist for review. A practical script follows four steps:

Python
import mlflow

table = mlflow.search_runs(
    experiment_names=["churn-model"],
    filter_string=(
        "attributes.status = 'FINISHED' "
        "AND tags.data_version = '7' "
        "AND metrics.val_f1 > 0.75"
    ),
    order_by=["metrics.val_f1 DESC"],
    max_results=10,
)

columns = ["run_id", "metrics.val_f1", "metrics.latency_ms", "params.model"]
print(table[columns])
table[columns].to_csv("shortlist.csv", index=False)
mlflow.log_artifact("shortlist.csv")

First, you keep only finished runs on the right data version. Second, you drop weak ones. Third, you sort and limit. Fourth, you save the shortlist as evidence of how you chose.

If a metric was never logged for a run, that column is empty in the table, so check for missing values before you compare. A run that did not log latency should not pass a latency rule just because the value is blank.

Searching experiments and models

You can also search for experiments themselves and for registered models and versions, using related search functions. These accept their own filter strings. The syntax looks similar but the supported fields differ, so test a filter on a small case first.

Debugging an empty result

When a search returns nothing, work through this list:

  1. Is the tracking URI correct?
  2. Is the experiment name spelled exactly right?
  3. Are parameter values quoted?
  4. Is a metric name misspelled, such as val_auc versus val_AUC?
  5. Do the conditions contradict each other?

Remove conditions one at a time until results appear. The last condition you removed is your clue. This is faster than staring at a long filter string.

Search is not analysis

A search selects rows. It does not tell you whether a difference is meaningful, or why a setting works. For that, load the table and plot results against parameters. Remember that parameter columns arrive as text, so convert numeric ones before plotting, for example with table["params.max_depth"].astype(int).

Keep filters readable

Long filter strings are easy to break. Build them from a list of short conditions and join them with " AND ". Then you can add or remove one line at a time, and a reviewer can read each rule on its own. Give the list a clear name, such as release_candidates, so its purpose is obvious. When you compare results from two different days, use the same saved list, so that a changed filter does not look like a changed model.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. How should you write a filter for parameter max_depth equal to 6?

    Parameters are strings, so the value is quoted.
  2. 2. Which logical operator is not supported in this filter syntax?

    Conditions can be joined with AND only. Filter in pandas when you need OR.
  3. 3. What does ordering by metrics.auc DESC do?

    DESC puts the highest values first.

Summary

  • Filter runs with prefixed fields such as metrics. and params..
  • Parameters are text, so quote their values.
  • Use AND, order_by and max_results, and handle OR in your own code.
  • Compare only runs that share data and a clear selection metric.

Next: Autologging

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.