Sub topic 1
What MLOps is and why models need operations
Prerequisites
This chapter assumes you know basic Python, have trained and evaluated a supervised model, and can use Git and the command line. You should also understand that a trained model is a file produced by running training code on data.
What MLOps is and why models need operations
MLOps is the set of practices that keeps machine learning models reliable after they leave a notebook. It borrows its ideas from DevOps, which applies automation, testing and monitoring to software delivery. A model adds a complication that ordinary software lacks: its behavior depends on data as well as code. A model can degrade silently when the input data changes, even though no one edited the code.
The machine learning lifecycle at a glance
The lifecycle moves through stages: frame the problem, manage data, engineer features, train and evaluate, deploy, monitor, and retrain. Each stage produces artifacts such as datasets, feature definitions, trained models and metric reports.
Framing the problem and choosing success metrics
Write down the decision the model supports and the metric that reflects the cost of its errors. Accuracy alone can mislead when one class is rare. Set the release threshold before training starts, so the target does not move after you see the results.
Managing data: collection, validation and versioning
Validate each data batch before training. Check column types, value ranges and missing values. Version datasets the way you version code, so every model can name the exact data it used. A simple data version is a hash of the file contents.
Features and training-serving consistency
Training-serving skew occurs when the code that builds features differs between training and production. Put feature logic in one shared function and call it from both paths.
Making training runs reproducible
A training run is reproducible when its code version, data version, environment and random seeds are recorded together. Changing any one of them can change the result.
import hashlib
import random
import numpy as np
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
data = b"sepal_length,sepal_width\n5.1,3.5\n4.9,3.0\n"
data_version = hashlib.sha256(data).hexdigest()[:12]
print("seed:", SEED, "data version:", data_version)Exercise
Why is recording the code commit alone not enough to reproduce a model?
Show answer
The same code trained on different data, or with a different seed, produces a different model. The data version and seed must be recorded with the commit.