Sub topic 1
Two paths to the same model: training and serving
Prerequisites
This chapter assumes you know basic Python and pandas, the train, validate, predict workflow for supervised models, and the general idea of serving a model behind an API or a batch job. You should also understand the mean, variance, and histograms.
Two paths, one model
A model reaches production through two code paths. The training path reads historical data, computes features, and fits the model. The serving path receives a live request, computes the same features, and asks the fitted model for a prediction. Both paths must produce identical feature values for the same input. When they do not, the model predicts on inputs it never learned from.
What training-serving skew is
Training-serving skew is a difference between the features a model sees during training and the features it sees in production. Skew lowers accuracy without raising an error. The model keeps returning predictions, so the failure looks like ordinary model decay until someone checks the inputs.
Skew, data drift, and concept drift
These three problems differ:
- Skew is a mismatch between the training and serving pipelines. It exists from the first day of deployment.
- Data drift is a change in the input distribution over time, while both pipelines stay correct.
- Concept drift is a change in the relationship between inputs and the correct output.
You fix skew in code. You handle drift with monitoring and, often, retraining.
Cause 1: the same feature written twice
Teams often rewrite a feature for serving because the training code lives in a notebook. A small difference in the rewrite changes the value. In this example, training fills missing ages with the mean, while serving fills them with zero.
import pandas as pd
train = pd.DataFrame({"age": [30, 40, None, 50]})
train["age"] = train["age"].fillna(train["age"].mean())
def serve_features(request: dict) -> dict:
age = request.get("age")
return {"age": 0 if age is None else age}
print(train["age"].tolist()) # [30.0, 40.0, 40.0, 50.0]
print(serve_features({"age": None})) # {'age': 0}The model learned that a missing age means about 40. In production it receives 0, a value far outside its training data.
Cause 2: statistics and vocabularies fitted at training time
Some transformations learn values from data: a median for imputation, a mean and standard deviation for scaling, and the category list for one-hot encoding. Fit these once on training data. If serving refits them on live traffic, the scale and categories change with every batch, and the same input produces different features.
Exercise
A scaler was fitted on the training set, where the mean age is 40. At serving time, your code calls fit_transform on a batch of 200 live requests. Why is this wrong, and what should you do instead?
Show answer
fit_transform recomputes the mean from the live batch, so the same input gets a different scaled value than the model saw in training. Fit the scaler once on training data, save it with the model, and call only transform at serving time.