Sub topic 7
Avoiding training-serving skew
Prerequisites
This chapter assumes you know how a model is trained and how batch and real-time prediction differ. It also assumes you can read basic pandas code. Earlier chapters covered where features come from at prediction time. This one covers what goes wrong when those features differ from the ones used in training.
What skew is
Training-serving skew is a difference between the features a model saw during training and the features it receives when serving predictions. The model code is unchanged and nothing crashes. The inputs are simply different, so accuracy drops quietly.
Skew can occur in batch and in real-time serving. It is more common in real-time serving, because the online path is often written separately from the training pipeline.
How skew arises
These are the usual causes:
- Duplicated logic. A data scientist computes
monthly_spendin SQL for training. An engineer reimplements it in application code for the API. Small differences in rounding, time windows or null handling follow. - Different missing-value handling. Training fills missing values with the median. The service fills them with zero.
- Different data sources. Training reads a cleaned warehouse table. The online service reads a raw operational database.
- Time leakage. Training features include information that is only known after the prediction moment, such as tickets opened next week. Serving can never supply that information.
- Feature order or type mismatch. The service passes columns in a different order than training used, or sends a string where training used a number.
- Stale features. A feature is refreshed daily in the online store but was computed point-in-time during training.
Using one definition of features
The most reliable fix is to write feature logic once and import it everywhere. The training script, the batch job and the API all call the same function.
import pandas as pd
FEATURE_COLUMNS = ["tenure_months", "monthly_spend", "support_tickets"]
def build_features(raw: pd.DataFrame) -> pd.DataFrame:
out = pd.DataFrame(index=raw.index)
out["tenure_months"] = raw["tenure_months"].fillna(0.0).astype(float)
out["monthly_spend"] = raw["monthly_spend"].fillna(0.0).astype(float)
out["support_tickets"] = raw["support_tickets"].fillna(0).astype(int)
return out[FEATURE_COLUMNS]
# Batch path: many rows at once.
batch = pd.DataFrame(
{
"tenure_months": [12, None],
"monthly_spend": [40.5, 80.0],
"support_tickets": [1, None],
}
)
print(build_features(batch))
# Online path: one request, wrapped as a one-row frame and sent through the same function.
request = {"tenure_months": 12, "monthly_spend": 40.5, "support_tickets": 1}
print(build_features(pd.DataFrame([request])))Both paths apply the same fill rules, types and column order. The function fixes FEATURE_COLUMNS, so the order can never drift.
Other safeguards
- Share a feature store or library. A feature store serves the same definitions offline for training and online for requests. A versioned Python package achieves much the same with less infrastructure.
- Compute features point-in-time. When building training data, use only values known at each example's prediction timestamp.
- Log serving features. Save the exact feature values the service used. You can train on these logs later, and you can compare them with training data.
- Compare distributions. Check the mean, range and null rate of each feature in serving against training. A sudden shift in one feature often points to a pipeline bug.
- Test for parity. Run the same raw records through the training path and the serving path in a unit test. Assert that the outputs match.
Skew versus drift
Skew is a mismatch between training and serving at the same moment in time. Drift is a change in real-world data over time, even when the pipelines are identical. Skew is fixed by correcting the pipeline. Drift is usually fixed by retraining. The monitoring chapter covers drift in more detail.
Exercise
A churn model scores well offline. After launch, the online service performs much worse, but batch scoring with the same model performs as expected. The API computes monthly_spend with its own code. Name the likely cause and one way to confirm it.
Show answer
The likely cause is training-serving skew from duplicated feature logic in the API, such as different null handling or time windows. To confirm it, log the features the API actually used, run the same raw records through the training feature code, and compare the two outputs. To fix it, make the API call the shared feature function.