Sub topic 7
Comparing runs in the UI
This chapter assumes you have a tracking server running at http://127.0.0.1:5000, as set up earlier in the tutorial. It also assumes you can log parameters, metrics and tags inside mlflow.start_run(). Menu labels and layout in the MLflow UI change between releases, so this chapter describes the features and where to find them, not exact pixel positions.
What the UI is for
Logging a run produces data. The UI is where you turn that data into a decision: which configuration is better, why, and whether the difference is real. The UI reads the same backend store that search_runs queries, so anything you see there can also be fetched in code. Use the UI to explore, and use code when you need a repeatable selection.
A comparison has four steps: narrow the runs, select a few, compare them, and write down what you concluded.
Create runs worth comparing
Comparison needs runs that differ in a few controlled ways. The script below trains logistic regression with five values of C and with or without feature scaling. It logs a second metric, test_log_loss, because accuracy on 30 test samples saturates and many runs tie.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-compare")
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
for scaled in [False, True]:
for C in [0.001, 0.01, 0.1, 1.0, 10.0]:
clf = LogisticRegression(C=C, max_iter=200)
model = make_pipeline(StandardScaler(), clf) if scaled else clf
with mlflow.start_run(run_name=f"C={C}-scaled={scaled}"):
model.fit(X_train, y_train)
mlflow.log_param("C", C)
mlflow.log_param("scaled", scaled)
mlflow.log_metric("train_accuracy", model.score(X_train, y_train))
mlflow.log_metric("test_accuracy", model.score(X_test, y_test))
mlflow.log_metric(
"test_log_loss", log_loss(y_test, model.predict_proba(X_test))
)
mlflow.set_tag("stage", "baseline")Run it, then open http://127.0.0.1:5000 and click the iris-compare experiment. You should see ten runs.
The runs table
The experiment page lists runs in a table. Each row is one run, and the columns come from the run's attributes (name, creation time, duration, source), then its metrics, parameters and tags.
- Metric columns show the latest logged value. If you logged a metric with
step, the table shows the value at the last step, not the best one. A loss that bottomed out at epoch 8 and rose again will look worse here than it was at its best. - Parameter columns are strings.
scaledappears asTrueorFalse, andCappears as0.001,1.0, and so on. Sorting a parameter column therefore sorts text, so10.0can land before2.0. - Column selection. A wide experiment can have dozens of columns. Use the columns control to show only the ones you need, for example
C,scaled,test_accuracyandtest_log_loss. Hidden columns are still stored. Hiding only changes the view. - Sorting. Click a metric header to sort. Sort by
test_log_lossascending to put the best-calibrated model first. - Nested runs. If you logged a sweep with
nested=True, children appear grouped under their parent. Expand the parent to see the trials. The parent row usually has no metrics of its own unless you logged some, so sort children, not parents.
Filtering with the search box
The search box above the table uses the same filter language as search_runs. Metrics, parameters, tags and attributes each have a prefix.
metrics.test_accuracy >= 0.9 AND params.scaled = 'True'
metrics.test_log_loss < 0.3 AND tags.stage = 'baseline'
attributes.run_name LIKE 'C=0.1%'Three rules cause most filter errors:
- Parameter values are strings, so compare them with quotes:
params.C = '1.0', notparams.C = 1.0. A numeric comparison such asparams.C > 1does not do what you expect. - Metrics are numbers and take no quotes.
- Joining conditions is done with
AND. The filter language has noOR, so run two searches or select runs by hand.
A filter that matches nothing shows an empty table, which can look the same as a wrong tracking URI. If the table is empty, clear the filter first. Then check that the experiment name in the UI matches the one you passed to set_experiment.
Selecting and comparing runs
Tick the checkbox at the start of two or more rows, then click Compare. The compare page has two parts: plots at the top and a table of values below.
The comparison table
The table has one column per run and one row per parameter, metric and tag. Two controls matter:
- Show diff only hides rows where every selected run has the same value. In the
iris-compareruns,stageand any constant parameters disappear, leavingC,scaledand the metrics. This is the quickest way to answer "what did I change between these two runs?" - The table includes the run's source, git commit and other attributes when they were recorded. Two runs with the same parameters but different commits point to a code change you did not log as a parameter.
The plots
The compare page offers several plot types. Which ones appear depends on the MLflow version, but these are common.
| Plot | Axes | Best for | Limits |
|---|---|---|---|
| Parallel coordinates | One vertical axis per chosen parameter or metric; one line per run | Seeing which parameter ranges lead to good metrics across many runs | Needs numeric axes; lines overlap with hundreds of runs |
| Scatter | One parameter or metric on each axis | Relating one setting to one outcome, such as `C` against `test_accuracy` | Only two variables at a time |
| Contour | Two parameters or metrics, with a third as color or contours | Spotting a good region in a two-parameter search | Needs enough runs to interpolate; sparse grids mislead |
| Box | Distribution of a metric across runs | Judging spread, such as variation over random seeds | Needs several runs per group |
For the iris-compare runs, select all ten and open parallel coordinates. Put C, scaled and test_log_loss on the axes. Drag along the test_log_loss axis to highlight only the lowest-loss lines, and read off which C and scaled values they pass through. You should see that scaled runs reach low loss at smaller values of C.
Viewing metrics over time
Comparing final values hides how a run got there. If you logged a metric with step, open the run (or the chart view of the experiment) to see a line chart with one line per run. Runs are colored consistently, so you can follow one run across charts.
- Hovering shows the value at that step for every visible run.
- Smoothing makes noisy curves readable but can hide a real spike. Turn it off before concluding that a run diverged.
- The x-axis can usually switch between step, wall-clock time and relative time. Use step to compare epochs. Use time to compare how quickly runs converge. Two runs with the same number of steps can take very different amounts of time.
- A metric logged without
stepis a single point and draws as a dot, not a line.
Reading a comparison correctly
A UI makes comparing easy, and that is also how you reach wrong conclusions. Check these before trusting a ranking.
- Same data and split. A run trained on a different split or dataset version is not comparable, even with a higher metric. Log a
data_versionparameter and arandom_stateso the diff table can show a mismatch. - Same metric definition.
accuracyon a validation set andaccuracyon a test set are different numbers under the same name. Prefix metric names withval_ortest_, as this chapter does. - Test set reuse. If you sort 200 runs by
test_accuracyand pick the top one, the winner is partly selected for luck on that test set. Choose by a validation metric and report the test metric once. - Noise. Tiny gaps, such as 0.967 against 0.933 on 30 samples, equal one prediction. Use the box plot over several seeds before calling one configuration better.
- Unfinished runs. A run still in the
RUNNINGstate, or one that ended asFAILED, can have partial metrics. Filter withattributes.status = 'FINISHED'when ranking.
Recording the outcome
The UI lets you edit tags on a run page, and you can also set them in code. Record the decision where the team will see it, as a tag on the run, rather than only in a chat message.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
client = mlflow.MlflowClient()
experiment = client.get_experiment_by_name("iris-compare")
runs = client.search_runs(
[experiment.experiment_id],
filter_string="attributes.status = 'FINISHED'",
order_by=["metrics.test_log_loss ASC"],
max_results=1,
)
best = runs[0]
client.set_tag(best.info.run_id, "decision", "candidate")
print(best.info.run_name, best.data.metrics["test_log_loss"])The UI selection and this script should agree. If they do not, one of them has a different filter or a different experiment.
Common problems
- The UI shows no experiments or runs. The script wrote to a different store. Check
mlflow.get_tracking_uri()in the script. Withoutset_tracking_uri, runs go to a localmlrunsdirectory, not to your server. - Compare is greyed out. You selected fewer than two runs.
- A metric column is missing. Columns appear only for metrics that at least one visible run logged. Check the spelling, since
test_accandtest_accuracyare separate metrics. - Deleted runs reappear in counts. A deleted run is hidden but remains in the store until garbage collection. Use the run state filter to view or restore deleted runs.
- Charts do not match the table. The table shows the latest value, and charts show history. Both are correct, but they answer different questions.
Exercise
In the iris-compare experiment you want only scaled runs whose test log loss is below 0.3, and you want the best of them first. Write the filter string for the search box, name the sort you would apply, and say why params.scaled = True without quotes would be wrong.
Show answer
Filter: params.scaled = 'True' AND metrics.test_log_loss < 0.3. Sort by the test_log_loss column ascending, so the lowest loss is first. Parameters are stored as strings, so their values must be quoted in the filter. Metrics are numbers and take no quotes. Without the quotes, the filter is not valid for a string parameter and will fail or match nothing.
Exercise
A teammate says run A beats run B because its test_accuracy is 0.967 against 0.933 on a 30-sample test set. Name two things you would check in the compare page before agreeing.
Show answer
First, turn on the diff-only view to confirm the runs differ only in the setting you care about, with the same random_state, data version and split. Second, judge whether the gap is meaningful: 0.967 against 0.933 is one prediction out of 30. Rerun each configuration with several seeds and use a box plot, or compare a continuous metric such as test_log_loss, before calling one better.