Sub topic 6
Making Training Runs Reproducible
Prerequisites and the reproducibility contract
Assume you know basic Python, supervised model training, Git, and command-line use.
A training run is reproducible when another person can recreate the same result from recorded inputs. The inputs include more than the training script:
- Code version: the Git commit that contains the training logic.
- Data version: the exact files, rows, filters, and preprocessing inputs.
- Environment: Python, library versions, operating system, and hardware where relevant.
- Configuration: hyperparameters, random seeds, and command-line options.
- Output: the model file, evaluation metrics, and useful logs.
A seed alone does not make a run reproducible. It controls some random operations, but results can still change when data changes, library behavior changes, or hardware uses nondeterministic operations.
Record the run inputs
A simple approach is to save a manifest beside the model. A hash is a short fingerprint calculated from file contents. If the data file changes, its hash changes too.
from pathlib import Path
import hashlib
import json
import platform
import sys
DATA_PATH = Path("train.csv")
SEED = 42
PARAMETERS = {"model": "logistic_regression", "C": 1.0}
def sha256_file(path):
digest = hashlib.sha256()
with path.open("rb") as file:
for block in iter(lambda: file.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
manifest = {
"data_file": str(DATA_PATH),
"data_sha256": sha256_file(DATA_PATH),
"python": sys.version,
"platform": platform.platform(),
"seed": SEED,
"parameters": PARAMETERS,
}
Path("run_manifest.json").write_text(
json.dumps(manifest, indent=2),
encoding="utf-8",
)
print("Wrote run_manifest.json")This script expects train.csv to exist in the current directory. In a real project, also record the Git commit and install dependencies from a lockfile such as a pinned requirements.txt:
numpy==2.1.3
scikit-learn==1.5.2Pinning versions improves repeatability, but it does not guarantee identical floating-point results across every processor or accelerator. Treat the environment specification as part of the run’s lineage, which is the record connecting an output to its inputs.
Common failure modes
| Failure | Why it causes trouble | Better practice |
|---|---|---|
| Only saving the model | The training conditions are unknown | Save code, data, parameters, and environment |
| Reusing a mutable data file | The same path can contain different rows later | Create immutable snapshots or versions |
| Setting one seed | Other libraries may use separate random generators | Set and record every relevant seed |
| Installing “latest” packages | Future versions may change behavior | Pin dependency versions |
A reproducible run should also use deterministic data splitting and preprocessing. Fit transformations on the training split only, then apply the saved transformation to validation and production data. Otherwise, a later rerun may accidentally include information that was unavailable during training.
Exercise
A model was trained with seed 7, but its data file was overwritten before the next run. Is recording the seed enough to reproduce the original model?
Show answer
No. The original data snapshot or its versioned identifier is also required. A seed can repeat random choices only when the inputs and execution behavior are the same.