Sub topic 1
Why Define Training as Code
A model training workflow often begins as a notebook. You load data in one cell, clean it in another, try several models, and save the best result manually. This is useful for exploration, but it becomes risky when other people must repeat the work or when the model needs regular retraining.
A training pipeline is a series of named steps that produces a model from data and configuration. Defining that pipeline as code means that the steps, their order, and their settings live in version control, such as Git. A person can inspect, review, test, and run the same workflow again.
Prerequisites
This chapter assumes that you know basic Python functions and modules, Git, and the usual machine learning steps: splitting data, training a model, and measuring its performance. You should also be familiar with a library such as scikit-learn.
The problem with manual workflows
Notebook cells keep state. A cell may use a variable created several cells earlier, even if the notebook is opened halfway through. Running cells in a different order can produce a different result. A person may also change a parameter without recording the change.
Manual workflows create questions that are difficult to answer:
- Which data version produced this model?
- Which preprocessing steps ran?
- Which parameter values were used?
- Did the train and test split use a fixed seed?
- Can another developer produce the same result?
- Can a failed step be rerun without repeating unrelated work?
A pipeline defined in code makes these details visible. A code review can show exactly what changed. A commit identifies the version of the workflow. Automated tests can check that the workflow still behaves correctly.
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
def load_data():
return load_iris(return_X_y=True)
def train_and_evaluate(seed=42):
X, y = load_data()
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=seed, stratify=y
)
model = RandomForestClassifier(n_estimators=100, random_state=seed)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
return accuracy_score(y_test, predictions)
if __name__ == "__main__":
print(f"accuracy={train_and_evaluate():.3f}")This example is small, but its behavior is explicit. The data-loading, splitting, training, and evaluation actions occur in a known order. The seed is an argument rather than an undocumented notebook setting. Someone can run the file from a clean process and inspect the result.
Versioning creates an audit trail
When pipeline code is committed to Git, each run can be associated with a code revision. The revision does not record everything by itself, so a production workflow should also record data versions, configuration values, library versions, metrics, and output artifacts. However, versioned code provides the starting point for that record.
| Manual workflow | Pipeline as code |
|---|---|
| Cell order may matter | Execution order is defined |
| Settings may be hidden in cells | Configuration is visible |
| Repetition depends on memory | A command runs the same steps |
| Changes are hard to compare | Git shows the exact change |
| Failures require manual recovery | Individual stages can be tested and rerun |
Code also creates a boundary between exploration and execution. You can use a notebook to investigate an idea, then move the stable logic into functions and modules. The notebook becomes a place to inspect results, rather than the only place where the workflow works.
A pipeline is a shared contract
A useful pipeline does more than put notebook cells into one long script. It gives each stage a clear purpose and a clear input and output. For example, a data-loading stage returns features and labels. A training stage receives training data and returns a fitted model. An evaluation stage receives the model and test data and returns metrics.
These boundaries let developers test one stage without running the entire workflow. They also make future automation possible. A continuous integration system can run tests on every change. An orchestrator can run independent stages in the correct order. A later run can reuse an unchanged result instead of repeating expensive work.
What belongs in code
Put the workflow logic in code: loading and validating inputs, transforming data, training, evaluating, and writing outputs. Keep tunable values, such as a test size or tree count, in configuration rather than scattering them through functions. Record the values used for each run.
Do not assume that code alone guarantees identical results. Reproducibility also depends on fixed random seeds, stable data versions, pinned dependencies, and controlled execution environments. These concerns build on the basic decision to make the workflow executable and reviewable.
Exercise
A teammate says, “The notebook is in Git, so the training process is already reproducible.” What important problem can remain?
Show answer
Git versions the notebook file, but it does not guarantee cell execution order, data and dependency versions, hidden state, or the parameter values used for a particular run.