We are going to design a training pipeline that is reproducible. The key aspects are:
Data snapshots: We need to version the data used for training, validation, and testing.
Seeds: Set seeds for random number generators (Python, NumPy, TensorFlow/PyTorch, etc.) to ensure consistent initialization and shuffling.
Environment pinning: Use a virtual environment and pin all dependencies (with exact versions) to avoid changes in libraries affecting the results.
Artifact tracking: Track all artifacts (models, logs, metrics, etc.) and their versions.
Let's break down the steps:
Step 1: Data Snapshotting
We can use a data versioning tool like DVC (Data Version Control) or simply store a copy of the data in a versioned directory (e.g., using timestamp or git hash) and record the version in the code.
Alternatively, we can use a hash of the dataset to identify the version.
Step 2: Setting Seeds
We will set seeds for the random number generators in the environment (Python's random, numpy, and the deep learning framework we are using, e.g., TensorFlow or PyTorch).
Also, if we are using any other library that has randomness (like random in data loading), we set those too.
Step 3: Environment Pinning
Use a virtual environment (conda or venv) and export the exact package versions (using pip freeze or conda list) to a file (requirements.txt or environment.yml).
We can also use Docker to containerize the entire environment.
Step 4: Artifact Tracking
Use a tool like MLflow, Weights & Biases, or simply a directory structure with timestamps and hashes to track artifacts.
We should log the following:
Model checkpoints
Training logs (e.g., TensorBoard, text logs)
Evaluation metrics (on validation and test sets)
Configuration file (hyperparameters, data version, etc.)
Implementation Plan:
We'll create a configuration file (e.g., YAML) that specifies:
Data version (path to the data snapshot)
Hyperparameters
Seed value
Paths for logging and saving artifacts
We'll write a script that:
a. Reads the configuration.
b. Sets the seeds.
c. Loads the data snapshot (from the specified path, which is versioned).
d. Preprocesses the data (in a deterministic way, if any randomness, set seed for that too).
e. Builds the model (with fixed initialization due to seed).
f. Trains the model, logging artifacts (model checkpoints, logs) to a directory that is named by a combination of:
- The configuration hash (to avoid duplicate runs with same config)
- Timestamp (to avoid overwriting)
- Or use a run ID from an experiment tracking tool.
g. Evaluates the model and saves the metrics.
We'll also provide a way to reproduce the environment:
We'll have a requirements.txt or environment.yml that is used to create the environment.
Optionally, we can use Docker to build an image from a Dockerfile that specifies the base image and installs the dependencies.
We can also use a tool like DVC to manage the data and model artifacts, but we'll keep it simple for now.
Let's write a skeleton code for the training script:
However, note: the question asks for making the pipeline reproducible, so we are going to design the steps and code accordingly.
Example for a PyTorch project:
Steps in code:
Parse config (from YAML file, for example).
Set seeds.
Set up device (GPU/CPU) and make it deterministic (if using CUDA, set cudnn benchmark and deterministic flags).
Load dataset (using the data snapshot path from config).
Create data loaders (with fixed seed for shuffling).
Initialize model.
Define loss and optimizer.
Train and validate, saving checkpoints and logs.
Test and save final metrics.
Let's write a sample code structure:
Note: We are going to assume a PyTorch project for this example.
To build a fully reproducible ML training pipeline, implement these four interconnected pillars systematically:
1. Data Snapshots & Versioning
Goal: Immutable, traceable datasets.
Tools:
DVC (Data Version Control) – Git-like versioning for data/models.
LakeFS – Branching/versioning for object storage (S3, GCS).
Pachyderm – Data pipeline versioning.
Implementation:
bash
# Freeze dataset state
dvc add data/raw
dvc push # Stores snapshot in remote storage (S3/GCS)
Automate checks: Add CI step that reruns a subset with fixed seed and asserts metric consistency.
Common Pitfalls to Avoid
Non-deterministic ops: Some GPU ops (e.g., torch.nn.Dropout in training) are stochastic by design – acceptable if seed-controlled.
Data shuffling: Always set shuffle=True + seed in DataLoader.
External randomness: Disable TensorFlow’s allow_growth or PyTorch’s cudnn.benchmark.
Unpinned dependencies:pip install package vs pip install package==1.2.3.
Data leakage: Ensure preprocessing (normalization, augmentation) is identical between training/inference.
Quick Checklist
Data versioned (DVC/LakeFS) with immutable snapshot ID in config.
Global seed set for Python, NumPy, framework, and DataLoader.
Environment pinned via Docker/conda (exact CUDA, framework, lib versions).
All artifacts logged with config hash, Git commit, and environment metadata.
Pipeline validated by rerunning → identical results.
By combining these practices, you achieve bit-for-bit reproducibility (barring hardware differences) and full experiment traceability. Start with DVC + seed setting + Docker as a minimal viable setup, then add experiment tracking (MLflow) for scalability.