Reproducibility and versioning
Version the data, the recipe, the settings and the model so that any result can be rebuilt and explained.
Lesson 63 of 6610 minBeginner
You will learn to
- List what must be versioned to reproduce a model
- Use immutable artifacts and lineage to trace a prediction back to its sources
- Reproduce a past result, and explain why a rerun can differ slightly
Before this lesson
Intuition
A cook who writes "a bit of salt, bake until done" can never make the same cake twice. A baker who records the exact flour batch, the weights, the oven temperature and the time can make it again next year, and can explain why last month's cake was different.
A machine-learning result is a cake with many ingredients. To reproduce it means rebuilding the same model, or at least getting the same score, from a record. To version things means giving each ingredient a number or an identity so the record can point at it.
Four kinds of ingredient need a version:
- Data. A dataset changes as rows are added or fixed. Name the exact version used.
- Recipe. The steps that turn data into a model: cleaning, features, algorithm, hyperparameters. Changing a step is a new version of the recipe.
- Settings. The random seed, the number of folds, the train and test split. These decide which rows land where.
- Environment. The versions of the libraries that ran the code. A new library release can change numbers slightly.
The finished model, plus a record of the four ingredients above, forms a lineage: the chain from raw data to the prediction a person saw. Lineage is what lets you answer "which data trained the model that approved this loan?" months later.
Two principles make the record trustworthy. Immutable artifacts: once a model version is created, it never changes; a fix is a new version. Recorded links: each step stores which inputs it used.
Picture it
The math
Reproducibility does not need a formula, but randomness needs care. Many steps are random: how rows are split, which rows a tree sees, the starting weights of a neural network. A pseudo-random number generator produces a sequence that looks random but is fully determined by a starting number, the seed. Same seed, same sequence.
If a score depends on data , recipe , seed and environment , then
You reproduce by fixing all four. If you fix , and but change , you get a different draw of the same experiment. The spread of those draws tells you how much of a score difference is just luck.
Worked example
We trained a random forest of 100 trees on a public breast cancer dataset (569 rows, scikit-learn) with a stratified 80/20 split, so that 114 rows are held out. We repeated it for the seeds 1 to 5, using the same seed for the split and for the model. We computed the accuracy on the 114 held-out rows. Each seed was run twice.
| Seed | Accuracy, first run | Accuracy, second run | Correct rows of 114 |
|---|---|---|---|
| 1 | 0.9561 | 0.9561 | 109 |
| 2 | 0.9737 | 0.9737 | 111 |
| 3 | 0.9737 | 0.9737 | 111 |
| 4 | 0.9825 | 0.9825 | 112 |
| 5 | 0.9649 | 0.9649 | 110 |
Two lessons come from this table.
- Same seed, same result. In every row, the two runs agree exactly. This is reproducibility: with the data, recipe and seed recorded, you can rebuild the number.
- Different seeds, different results. The scores range from 0.9561 to 0.9825, a spread of 2.6 percentage points, which is three rows out of 114. A model that scores 0.9825 with seed 4 is not necessarily better than one that scores 0.9561 with seed 1. When two models differ by less than this spread, you cannot tell them apart from one run. Record the seed, and compare using cross-validation or several seeds.
Now imagine that six months later someone cannot match a reported score of 0.8412 on a customer model. The record shows the model was trained on dataset version 2, while today's retrain used version 3 with 2,000 more rows. That is the explanation, found in minutes because the dataset version was recorded. (This second case is illustrative.)
How mlflow does it
- Dataset versions. A dataset keeps its versions, and a retrain uses the latest version of its data source and records which one.
- Pipeline versions. Each retrain or promotion creates a new pipeline version in the same family. The Versions tab of an endpoint lists them with their SHA-256 hash, and old versions stay available.
- Run settings. A run records its context, including Task, Target, Dataset, Plan, Train size, CV folds and Seed.
- Registry versions. Registered models keep numbered versions with a status of staging, production or archived. Rolling back flips which version is current without changing any version.
- Lineage. The Lineage view under Models shows edges such as a model being trained on a dataset or a deployment being deployed from a model.
- Taking a model out. You can download a model with its requirements and schema, so the environment is recorded too.
Common mistakes
- Not recording the seed. Then a result can never be reproduced exactly. Record it, and when comparing models, do not read differences smaller than the seed-to-seed spread as real.
- Editing a dataset in place. If the file changes under the same name, old results stop matching. Create a new version.
- Overwriting the production model. A model that changes under a fixed name cannot be audited. Create a new version, and keep the old one.
- Forgetting the environment. A library upgrade can change numbers. Keep requirements with the model.
- Believing that reproducible means correct. You can reproduce a wrong model perfectly. Reproducibility makes a result checkable; it does not make it right.
Try it in mlflow
Follow a model back to its sources with lineage
- Open Models and click a registered model.
- Read the Versions list: status (staging, production or archived) and the primary metric of each version.
- Choose View lineage on a version.
- Click a node to focus on it and read the edges: which data and run produced this model, and which deployment serves it.