Chapter 01
Tracking experiments
Experiments, runs and what you log to them: parameters, metrics, tags, artefacts and datasets.
What MLflow is
- What
- An open-source platform for the machine learning life cycle: tracking experiments, packaging models, a model registry, serving, evaluation, and tracing for GenAI apps.
- Why
- It replaces spreadsheets of results and folders named
model_final_v3.pklwith a searchable record of every run and model.
Experiments and runs
- What
- A run is one execution of training or evaluation code. Runs are grouped into experiments, usually one per problem or model family.
- Why
- Every result becomes traceable to the code, settings and data that produced it.
import mlflow
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("churn-model")
with mlflow.start_run(run_name="rf-baseline") as run:
mlflow.log_params({"n_estimators": 200, "max_depth": 8})
clf.fit(X_train, y_train)
mlflow.log_metric("val_auc", score_auc(clf, X_val, y_val))
print(run.info.run_id)Watch out
Logging everything to the Default experiment, so unrelated runs pile up in one list.
Parameters
Also called log_param, log_params
- What
- Inputs to a run, such as hyperparameters and settings, stored as strings. A parameter is written once per run and cannot be changed afterwards.
- Why
- Parameters are what you filter and compare on when asking "which settings gave the best score?".
Watch out
Logging the same parameter twice with a different value, which raises an error; log changing values as metrics.
Metrics
Also called log_metric, step
- What
- Numeric results logged over time. Each metric keeps its full history, with an optional
stepsuch as the epoch, and the UI plots it. - Why
- Learning curves reveal overfitting and divergence that a single final number hides.
for epoch in range(epochs):
train_loss = train_one_epoch(model, train_loader)
val_loss = evaluate(model, val_loader)
mlflow.log_metrics(
{"train_loss": train_loss, "val_loss": val_loss},
step=epoch,
)Artefacts
Also called artifacts, log_artifact
- What
- Output files attached to a run: plots, reports, feature lists, model files, sample predictions. They go to the artefact store, not the database.
- Why
- Everything needed to understand a run sits beside its metrics instead of on someone's laptop.
mlflow.log_artifact("reports/confusion_matrix.png")
mlflow.log_artifacts("reports/", artifact_path="reports")
mlflow.log_dict({"features": feature_names}, "features.json")
mlflow.log_text(classification_report(y, y_pred), "report.txt")
mlflow.log_figure(fig, "plots/roc.png")Dataset tracking
Also called log_input, mlflow.data
- What
mlflow.datawraps a DataFrame or table with its source, name, digest and schema;log_inputrecords it on the run with a context such as training or evaluation.- Why
- You can tell exactly which data version trained a model, and spot two runs that silently used different data.
dataset = mlflow.data.from_pandas(
train_df,
source="s3://data/churn/2026-09.parquet",
name="churn-train",
targets="churned",
)
mlflow.log_input(dataset, context="training")System metrics
- What
- With
log_system_metrics=Trueon a run (thepsutilpackage is needed, plusnvidia-ml-pyfor GPUs), MLflow samples CPU, memory, disk and GPU use during training. - Why
- You see whether a slow job was starved of GPU memory or stuck on data loading.