Sub topic 3
Experiments and Runs
This chapter assumes you know basic Python, context managers, and scikit-learn model training. You should also understand train/test splits and common evaluation metrics. We will focus on how MLflow groups and identifies training attempts.
The two main objects
An MLflow experiment is a named container for related machine learning work. For example, an experiment named iris-baseline might contain every baseline model trained for the Iris dataset.
A run is one execution of your training code. A run can represent one model trained with one set of data, features, and hyperparameters. MLflow stores information about that execution, such as:
- Parameters used before or during training.
- Metrics produced during evaluation.
- Tags that describe the run.
- Files and models saved as artifacts.
- The run status, start time, end time, and unique run ID.
The experiment gives runs a shared purpose. The run gives one result a precise identity.
| Object | Meaning | Example |
|---|---|---|
| Experiment | A collection of related runs | `iris-baseline` |
| Run | One execution of a training attempt | Logistic regression with `C=1.0` |
| Run ID | A unique identifier for one run | `a1b2c3...` |
A script can create many runs in the same experiment. This lets you compare changes without mixing unrelated projects.
How experiments contain runs
The relationship is hierarchical:
When you call mlflow.set_experiment, MLflow selects the experiment for later runs. If the experiment does not exist, MLflow creates it. The name is easier for people to read, while the experiment also has an internal numeric identifier.
A run usually starts with mlflow.start_run(). The with statement is important because it closes the run when the block ends, even when the block exits normally. MLflow records the run's final status as FINISHED when the block completes successfully. If an exception escapes the block, MLflow marks the run as failed.
Starting and identifying a run
The following example assumes a local MLflow tracking server is running at http://127.0.0.1:5000. It creates or selects an experiment, trains a model, and prints the run ID.
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("iris-baseline")
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
)
with mlflow.start_run(run_name="logreg-c1") as run:
model = LogisticRegression(C=1.0, max_iter=200)
model.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
mlflow.log_param("C", 1.0)
mlflow.log_metric("test_accuracy", accuracy)
print("run ID:", run.info.run_id)
print("experiment ID:", run.info.experiment_id)The object returned by start_run contains metadata about the active run. The run ID is the stable key you can use to locate that run later. The experiment ID identifies the collection that owns it.
A run is active only inside its context. MLflow's logging functions use the active run automatically. Starting a second ordinary run while one is active can cause an error or unexpected nesting behavior. For independent runs, allow the first context to finish before starting the next one.
Run identity and reproducibility
A run ID does not make an experiment reproducible by itself. It points to the recorded result. Reproduction also depends on recording the information needed to rebuild the result:
- The training and evaluation data, or a version of each.
- Feature preparation and split settings.
- Hyperparameters and random seeds.
- The source code and dependency versions.
- The model and useful evaluation artifacts.
For example, two runs can both use C=1.0 but produce different results if they use different data or splits. Treat the run as a record of a complete execution, not merely a row containing a score.
Common errors
A common error is forgetting to select an experiment. MLflow may place the run in a default experiment, making it difficult to find later. Set the experiment explicitly near the start of each training script.
Another error is starting a run but logging outside its context. Those log calls may fail because no active run exists. Keep model training and logging inside the with mlflow.start_run() block.
A third error is creating a new experiment for every parameter value. This removes the grouping that makes comparison useful. Keep related trials together and distinguish them with parameters or run names.
Exercise
You train four models with different values of C for the same dataset and evaluation split. Should you create four experiments or four runs, and why?
Show answer
Create four runs in one experiment. The models are related trials in one comparison, so a shared experiment keeps them together while each run records one value of C and its resulting metrics.