Sub topic 4
Why training and serving features drift apart
This chapter assumes you can write PySpark DataFrame code, know what a Delta table is, and have trained a model with scikit-learn. You do not need to know feature tables yet.
What training-serving skew is
Training-serving skew is a difference between the feature values a model saw during training and the values it receives when it makes predictions. The model code is unchanged, but its inputs are not computed the same way. Accuracy falls, and often no error is raised, so the problem is hard to notice.
Common causes
- Duplicated logic. A data scientist computes
avg_order_amount_90din a notebook. An engineer rewrites it in a service in another language. The two versions differ in small ways, such as how they treat nulls or which orders count as refunds. - Different time windows. Training data uses a window that ends at the label date. Serving code uses "now". The windows then cover different days.
- Different preprocessing. Category encodings, scaling parameters, or imputation values are refit or hard-coded separately at serving time.
- Stale values. The training table was refreshed daily, but the serving path reads a copy that is a week old.
- Label leakage. Training joins use feature values computed after the event being predicted. The model looks accurate offline and fails in production.
A small example of silent skew
The two functions below should compute the same feature. They disagree on missing values.
import pandas as pd
def train_feature(amounts: pd.Series) -> float:
# Training notebook: nulls are ignored.
return amounts.mean()
def serve_feature(amounts: pd.Series) -> float:
# Serving code: nulls are treated as zero.
return amounts.fillna(0).mean()
orders = pd.Series([100.0, None, 50.0])
print(train_feature(orders)) # 75.0
print(serve_feature(orders)) # 50.0For the same user, the model trained on 75.0 now receives 50.0. Nothing fails.
How feature tables reduce skew
The fix is to compute each feature once and have both paths read it from the same place. In Databricks, that place is a feature table in Unity Catalog. The rest of this tutorial builds that setup in steps:
- A single pipeline computes features and writes them to a feature table.
- Training reads features from that table through feature lookups.
- The logged model stores those lookups, so scoring retrieves the same features by key.
Because training and inference share one definition and one storage location, the logic cannot diverge between them.
Checking for skew
Even with shared storage, monitor your data. Compare summary statistics of training features with recent serving inputs, such as mean, null rate, and category frequencies. A large shift in any of them is a signal to investigate the pipeline or the data source.
Exercise
A model scores well offline but poorly in production. The training notebook fills missing avg_rating with the column median, while the serving service fills it with 0. Which kind of problem is this, and how would a shared feature table help?
Show answer
This is training-serving skew caused by different preprocessing. A shared feature table lets you compute and store the feature once, with one imputation rule, and both training and inference read that stored value instead of recomputing it.