MLflow

Sefism.

Chapter 02

Autologging, search and the UI

Capturing runs with one line, organising hyperparameter searches, and finding the best run in code or in the UI.

Autologging

Also called mlflow.autolog

What
mlflow.autolog() hooks into supported libraries such as scikit-learn, XGBoost, LightGBM, PyTorch Lightning and Keras, and logs parameters, metrics and the model when you call fit.
Why
Complete runs with no logging code, which is ideal for notebooks and quick comparisons.
Python
import mlflow
from sklearn.ensemble import RandomForestClassifier

mlflow.autolog()
with mlflow.start_run():
    clf = RandomForestClassifier(n_estimators=200)
    clf.fit(X_train, y_train)

Watch out

Enabling autolog and also logging the same model by hand, producing two copies per run.

Library-specific autolog

What
Each integration also has its own autolog, such as mlflow.sklearn.autolog(), with options like log_models, log_datasets and disable.
Why
Fine control: log metrics automatically but register models by hand, or silence one library.
Python
mlflow.sklearn.autolog(log_models=False, log_datasets=False)
mlflow.xgboost.autolog()

Nested runs

Also called parent and child runs

What
Runs started with nested=True inside another run become its children, and the UI groups them under the parent.
Why
A hyperparameter search reads as one parent with a child per trial instead of hundreds of loose runs.
Python
with mlflow.start_run(run_name="depth-search"):
    for depth in [4, 8, 16]:
        with mlflow.start_run(run_name=f"depth={depth}", nested=True):
            mlflow.log_param("max_depth", depth)
            mlflow.log_metric("val_auc", train_and_score(depth))

Searching runs

Also called search_runs, filter_string

What
mlflow.search_runs returns a pandas DataFrame of runs matching a SQL-like filter on metrics, params, tags and attributes, in the order you ask.
Why
Pick the best run in code, for example as a pipeline step, instead of clicking through the UI.
Python
runs = mlflow.search_runs(
    experiment_names=["churn-model"],
    filter_string="metrics.val_auc > 0.85 and params.max_depth = '8'",
    order_by=["metrics.val_auc DESC"],
    max_results=5,
)
print(runs[["run_id", "metrics.val_auc"]])

Watch out

Comparing params as numbers; they are stored as strings, so quote them in filters.

MlflowClient

What
The lower-level API for reading and editing runs, experiments, metric histories and registered models by id.
Why
Scripts and CI jobs use it to inspect runs and manage the registry without an active run.
Python
from mlflow import MlflowClient

client = MlflowClient()
run = client.get_run(run_id)
print(run.data.metrics, run.data.params)
history = client.get_metric_history(run_id, "val_loss")
client.set_tag(run_id, "reviewed", "true")

The MLflow UI

What
A web app listing experiments and runs, with metric charts, side-by-side run comparison, parallel coordinates plots, artefact browsing, models and traces.
Why
Comparing a dozen runs visually is often the fastest way to see which parameter actually matters.
Shell
mlflow ui --port 5000

Organising runs

What
Clear experiment names, meaningful run names and consistent tags, plus descriptions on experiments explaining the question being explored.
Why
Six months later, "churn-model / xgb-new-features" beats "Default / dapper-owl-42".
02 · Autologging, search and the UI2 / 10