Sub topic 2
Experiments and runs: the core model
This chapter assumes you can write basic Python, including context managers, and that you have trained a scikit-learn model in a Databricks notebook. Everything later in the tutorial builds on two objects: the experiment and the run.
The two containers
An experiment is a named collection of related runs. You usually create one per modeling problem, such as "iris classifier".
A run is one execution of your training code. It is the unit MLflow records. Each run stores:
- Parameters: input values you set, such as
C=1.0. Each key is logged once per run. - Metrics: numeric results, such as
accuracy. A metric can be logged many times, for example once per epoch, and MLflow keeps the history. - Tags: free-form labels, such as
stage=exploration. You can change them after the run. - Artifacts: files, such as plots, data samples, and model files.
Each run also gets a unique run_id, a start time, an end time, and a status.
The run lifecycle
A run moves through a few states. mlflow.start_run() creates it as RUNNING. When the run ends normally, its status becomes FINISHED. If an exception escapes the with block, the status becomes FAILED.
Use start_run as a context manager. The run then closes even when your code fails.
A first run
The code below selects an experiment, starts a run, and logs one value of each simple kind. Replace <your-email> with your Databricks user name.
import mlflow
mlflow.set_experiment("/Users/<your-email>/iris-tracking")
with mlflow.start_run(run_name="baseline") as run:
mlflow.log_param("C", 1.0)
mlflow.log_metric("accuracy", 0.93)
mlflow.set_tag("stage", "exploration")
print(run.info.run_id)Here is what each line does:
set_experimentmakes the named experiment the active one. If it does not exist, MLflow creates it.start_runopens a run inside that experiment.run_nameis a display label only. It does not need to be unique.log_param,log_metricandset_tagwrite to the active run.run.info.run_idis the identifier you use later to find the run, load its model, or register it.
The active run
MLflow keeps one active run per thread. Logging calls such as log_metric attach to it. If no run is active, log_metric starts one for you. Such an implicit run is not closed automatically in a plain script. Always open runs yourself with with, so the start and end are clear.
Calling start_run while another run is active raises an error. The exception is nested=True, which creates a child run. Nested runs are covered later in this tutorial.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Run ... is already active` | You called `start_run` inside another run, or a previous run was never ended. | Use `with`, or call `mlflow.end_run()` first. |
| Run is stuck at `RUNNING` | The notebook was interrupted before the run closed. | End it with `mlflow.end_run()`, or reattach with `start_run(run_id=...)`. |
| Parameter value changed error | You logged the same parameter key again with a different value in one run. | Log it once, or use a new key. Use a metric for values that change. |
| Metrics appear under an unexpected experiment | The experiment was not set before the run started. | Call `set_experiment` first. |
Exercise
A teammate logs mlflow.log_param("epochs", 5) and later, in the same run, calls mlflow.log_param("epochs", 10). What happens, and what would be a better way to record a value that changes over time?
Show answer
The second call raises an error, because a parameter key cannot be overwritten with a different value in the same run. Values that change over time, such as loss per epoch, should be logged as metrics. Each call to log_metric adds a new point to the metric's history.