Sub topic 11
CI/CD for Machine Learning: Code, Data and Model Checks
This chapter assumes you can train and evaluate a supervised model, use basic Git and command-line commands, and understand how a web service is deployed.
Why ordinary CI/CD is not enough
Continuous integration (CI) automatically checks changes whenever someone commits code. Continuous delivery or deployment (CD) moves a verified change toward production.
For ordinary software, the main changing artifact is source code. Machine learning systems also depend on training data, feature logic, environment settings, and model files. A code change can produce a different model even when the application tests still pass. A data change can reduce accuracy without changing any code.
An ML CI/CD pipeline therefore checks the complete chain:
| Area | Example check | Failure it catches |
|---|---|---|
| Code | Unit and integration tests | Broken feature or serving logic |
| Data | Schema, missing values, and range checks | Invalid or unexpected input |
| Model | Accuracy, fairness, and latency thresholds | Unsafe or weak candidate model |
| Reproducibility | Dependency, seed, and artifact checks | Results that cannot be rebuilt |
A typical pipeline
A pipeline often runs in this order:
- Validate the code and configuration.
- Validate the training data and record its version.
- Train a candidate model in a controlled environment.
- Evaluate it against a fixed test set and the current production model.
- Register the candidate if it passes.
- Deploy it to staging or a small production audience.
- Monitor the deployment and roll back if needed.
The pipeline should keep evaluation data separate from training data. Otherwise, the release gate can report an optimistic score because the model has already seen the examples.
def should_promote(candidate_accuracy, production_accuracy,
min_gain=0.0, floor=0.90):
"""Return whether a candidate satisfies the release policy."""
if candidate_accuracy < floor:
return False
return candidate_accuracy >= production_accuracy + min_gain
print(should_promote(0.94, 0.92)) # True
print(should_promote(0.89, 0.85)) # False: below the floor
print(should_promote(0.93, 0.95)) # False: worse than productionThis gate is deliberately small. A real policy may also require limits for false-negative rate, subgroup performance, model size, prediction latency, or security scans.
Checks must be traceable
Store the commit identifier, data version, dependency lockfile, training configuration, model artifact, evaluation results, and approval decision together. This lineage lets you answer which inputs produced a deployed model and why it was released.
Pipeline flow
Exercise
Why should a model candidate be compared with the production model as well as an absolute accuracy threshold?
Show answer
The absolute threshold prevents unacceptable models from shipping, while the comparison prevents a candidate from replacing a better production model.