Sub topic 13
What a run records for reproducibility
This chapter assumes you can log parameters, metrics and models in a notebook, as covered in the earlier chapters. It explains what MLflow captures about a run, what it leaves out, and how to read that record back.
Reproducibility has four parts
To rerun a result, you need to know four things: the code, the configuration, the data, and the software environment. A run stores some of these automatically and the rest only if you log them.
What is recorded automatically
MLflow adds system tags to every run. On Databricks, the tags include the following.
| Information | Where it lives | Notes |
|---|---|---|
| Start time, end time, status | Run info | Status shows whether the run finished or failed. |
| User | `mlflow.user` tag | The identity that started the run. |
| Source | `mlflow.source.name` and `mlflow.source.type` tags | For a notebook run, this is the notebook path. |
| Git commit | `mlflow.source.git.commit` tag | Present when the notebook lives in a Git folder. |
| Python version, flavor | `MLmodel` file in the model artifact | Written when you log a model. |
| Package versions | `requirements.txt`, `python_env.yaml` and `conda.yaml` in the model artifact | Written when you log a model. |
A notebook run also stores a snapshot of the notebook revision, so you can open the code as it was at run time.
Reading the record back
You can inspect a run through MlflowClient. This example creates a run and prints its recorded information.
import mlflow
from mlflow import MlflowClient
with mlflow.start_run(run_name="repro-demo") as run:
mlflow.log_param("C", 1.0)
mlflow.log_param("random_state", 42)
finished = MlflowClient().get_run(run.info.run_id)
print(finished.info.status, finished.info.start_time, finished.info.end_time)
print(finished.data.params)
for key, value in finished.data.tags.items():
if key.startswith("mlflow."):
print(key, "=", value)The mlflow.-prefixed tags are the system tags. The tags you set yourself have no prefix.
What you must log yourself
MLflow cannot see everything that affects a result. Two gaps cause most failed reproductions.
- Random seeds. A seed is not recorded unless it is a parameter. Log it with
mlflow.log_param("random_state", 42). Autologging only captures seeds that are arguments of the estimator, so a seed passed totrain_test_splitis missed. - Data. A run does not store your training table. Record which data you used with
mlflow.log_input, which takes a dataset built bymlflow.data.from_pandasor a similar helper. The dataset entry stores a name, a digest and a source. If the underlying table changes later, the digest tells you the data is different.
Package versions are recorded only when you log a model. A run that logs metrics but no model has no environment files.
A reproducibility checklist
- Run from a notebook in a Git folder so the commit is captured.
- Log every parameter that changes the result, including seeds.
- Log the training dataset with
mlflow.log_input. - Log the model, so the environment files are stored.
- Add a tag for anything else that matters, such as the source table version.
Exercise
A colleague cannot reproduce your run's accuracy. The run has a logged model and a Git commit tag, but you split the data with train_test_split(X, y, random_state=7) and never logged that value. Which part of reproducibility is missing, and how do you fix it in future runs?
Show answer
The configuration is incomplete, because the split seed is not recorded. Autologging only captures arguments of the estimator, not arguments of train_test_split. Fix it by calling mlflow.log_param("split_seed", 7) inside the run. Also log the dataset with mlflow.log_input so the data can be checked.