Sub topic 1
What reproducible means for an ML project
Prerequisites
This chapter assumes basic Python and pip usage, comfort on the command line, basic Git, and familiarity with training or running a model in PyTorch or scikit-learn.
What reproducible means
A reproducible ML project produces the same results when someone else runs it with the same inputs. Four things must stay fixed: the code, the data, the environment (installed packages and system libraries), and the randomness used during training.
How environments drift
Drift is the slow change of installed packages over time. An unpinned install of torch today may get a different version than the same command run next month. A new release can change a default value, a numerical routine, or the order of floating-point operations. Identical code then reports a different accuracy.
| Source of change | Example | Effect on results |
|---|---|---|
| Unpinned package | `pip install torch` installs a new minor version | Different numerical output |
| Different system libraries | Another BLAS or C library on the host | Small numeric differences |
| Changed data | Rows added to a CSV file | A different model |
Declared versus resolved dependencies
A declared dependency is a name in your project file, such as numpy>=1.24. It states a range of acceptable versions. A resolved dependency is the exact version the installer selects, including every transitive dependency (a package that one of your packages needs). Two installs from the same declaration can resolve to different versions on different days.
Isolating projects with virtual environments
A virtual environment is a folder with its own Python interpreter and packages, separate from other projects. It prevents conflicts between projects but does not record exact versions by itself.
python -m venv .venv
source .venv/bin/activate
pip install numpy scikit-learn> Note: the source command applies to Linux and macOS. On Windows, run .venv\Scripts\activate.
Lock files: freezing the full dependency tree
A lock file lists every package in the resolved tree, each pinned to one exact version. The installer then reproduces that exact tree. Generate one from a short list of direct dependencies:
pip install pip-tools
pip-compile --generate-hashes -o requirements.lock requirements.in
pip install --require-hashes -r requirements.lockThe requirements.in file holds only your direct dependencies. The requirements.lock file holds the full pinned tree with hashes.
Exercise
You run pip install numpy on Monday and again on Friday, and the second install gives a different numpy version. Which file would prevent this, and what command installs from it?
Show answer
A lock file such as requirements.lock. Install from it with pip install --require-hashes -r requirements.lock.