Sub topic 1
What reproducible means for an ML project
This chapter defines what "reproducible" means for a machine learning project. It splits the problem into four parts: code, data, environment and randomness. The rest of the tutorial deals with the environment part, and this chapter shows where that part sits.
Prerequisites
This chapter assumes you have:
- Basic Python and pip usage, including installing packages and running scripts.
- Comfort with the command line.
- Experience training or running a model in a framework such as PyTorch or scikit-learn.
- Basic Git usage: commits, branches and tags.
No container or lock file experience is needed. Those topics are introduced in later chapters.
Why "it works on my machine" is a bigger problem in ML
A normal software bug usually fails loudly. A function throws an error, a test goes red, and you know something is wrong. ML projects often fail quietly. A training run on a different machine finishes without errors and produces a model whose accuracy is 0.4 points lower. Nothing crashed, so nobody notices until much later.
Reproducibility is the ability to repeat a piece of work and get the same result, or a result close enough that you can explain the difference. If you cannot do that, you cannot tell whether a change you made helped. You also cannot debug a regression, audit a model that is already in production, or hand a project to a colleague.
Four inputs that decide a result
An ML result is a function of four kinds of input. If any one of them changes, the result can change.
- Code. Your training script, model definition and preprocessing. Git tracks this well.
- Data. The training and evaluation files, including their exact contents, order and any filtering. Git handles large or changing data poorly.
- Environment. The Python version, the installed packages and their versions, system libraries, the operating system layer, and for GPU work the CUDA libraries and driver.
- Randomness. Weight initialization, data shuffling, dropout, augmentation, and the nondeterminism of some parallel GPU operations.
Most teams handle the first input well because they already use Git. They handle the other three unevenly. This tutorial focuses on the environment, which is the input that is easiest to control completely and the one most often left to chance.
Levels of reproducibility
"Reproducible" is not one yes-or-no property. It helps to separate three levels, because each costs a different amount of effort.
| Level | What you get | What it needs |
|---|---|---|
| Rebuildable | You can recreate the same environment from a recorded description | Pinned dependencies, pinned base image |
| Re-runnable | You can rerun the pipeline and get a result within normal run-to-run variation | Rebuildable environment, versioned data, fixed seeds |
| Bitwise identical | You get exactly the same weights and metrics, bit for bit | Re-runnable setup, deterministic algorithms, same hardware and library builds |
Most projects should aim for the second level. It is the level at which you can compare two experiments fairly. The third level is useful for debugging, regression tests and some regulated work, but it can slow training and sometimes cannot be reached across different GPU models.
Think of a result as having a range. If five runs with different seeds give accuracy between 0.912 and 0.918, then a change that moves accuracy to 0.915 is not evidence of anything. Reproducibility lets you measure that range honestly, because you know the environment is not adding variation of its own.
What counts as the environment
Many people think of the environment as "the packages in requirements.txt". That is only part of it. The environment has layers, and each layer can differ between two machines.
| Layer | Examples | Can a Python lock file pin it? |
|---|---|---|
| Python packages | numpy, scikit-learn, torch, and everything they depend on | Yes |
| Python interpreter | 3.10 versus 3.11 | No, it is only constrained |
| System libraries | glibc, OpenBLAS, libstdc++ | No |
| GPU userland | CUDA runtime, cuDNN, NCCL | Partly, through wheels that bundle them |
| GPU driver | NVIDIA driver on the host | No |
| Hardware | GPU model, CPU instruction sets | No |
A lock file, covered in later chapters, handles the first row completely. A container image handles the middle rows. The driver and hardware stay on the host, so you need to record them rather than pin them.
A small example of silent drift
The following script trains a tiny model and prints its metric. Run it in two environments with different scikit-learn versions and the printed number can differ, even with the seed fixed. The cause might be a changed default, a changed solver, or a changed numerical routine in a dependency.
import sys
import numpy as np
import sklearn
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=2000, n_features=20, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0
)
model = LogisticRegression(max_iter=200, random_state=0)
model.fit(X_train, y_train)
print("python", sys.version.split()[0])
print("numpy", np.__version__)
print("scikit-learn", sklearn.__version__)
print("accuracy", round(model.score(X_test, y_test), 6))The script prints the versions next to the result. That habit costs nothing and makes a later comparison possible: when two accuracy numbers disagree, you can check whether the version lines also disagree. Printing versions is a diagnostic, though, not a fix. It tells you that the environments differ but does not let you recreate the old one.
Where each later chapter fits
The tutorial moves from the environment outward. It first explains how environments drift and why declared dependencies differ from resolved ones. It then covers virtual environments, lock files, hash verification and the tools that create them. It then turns to what a lock file cannot pin, introduces containers, and combines the two into one workflow that CI can check. Seeds, nondeterministic operations, safe lock updates and data versioning come last.
Common mistakes when defining the goal
- Treating "same code" as "same result". A Git commit pins code only. The commit hash says nothing about packages, data or seeds.
- Fixing the seed and stopping. A seed controls random number generators. It does not control a library upgrade that changes a default.
- Aiming for bitwise identity everywhere. This can cost speed and may be impossible across GPU models. Choose the level that matches the purpose of the project.
- Recording nothing about the run. Even without full control, logging the Git commit, package versions, data identifier and seed makes a result explainable later.
Summary
Reproducibility in ML depends on four inputs: code, data, environment and randomness. Git covers code. This tutorial covers the environment in depth, using lock files for Python packages and containers for the layers below them. It also touches the randomness and data parts. Choose a target level first: rebuildable, re-runnable or bitwise identical. Then use the tools that reach that level and no further.
Exercise
A teammate reruns your training script from the same Git commit with the same seed, and gets 0.4 points lower accuracy. Name the four inputs to check, and say which one you would inspect first if the script printed no errors and the data file hash matches.
Show answer
The four inputs are code, data, environment and randomness. The commit is the same, so code is covered, and the matching data hash covers data. Inspect the environment first: compare the Python version, package versions (especially the ML framework and numerical libraries), system libraries and CUDA or driver versions. Randomness is the next suspect, but a seed that is the same across both runs points to an environment change as the more likely cause, such as a library default that changed between versions. Also check whether the hardware differs, since GPU model can change numerical results.
Exercise
Your team wants "bitwise identical" training results. Give two costs or limits of that goal, and name a cheaper level that is often enough.
Show answer
Bitwise identity usually requires deterministic algorithms, which can be slower, and it often requires the same GPU model and the same library builds, so it may be impossible across different hardware. A cheaper level that is often enough is "re-runnable": a rebuildable environment, versioned data and fixed seeds, producing results within normal run-to-run variation.