Sub topic 12
Keeping Training and Serving Computations Consistent
Assume you understand features, labels, joins, timestamps, and basic pandas or SQL. This chapter focuses on training-serving skew, which is a difference between how a feature is calculated during training and how it is calculated when a model makes a live prediction.
A model learns from the exact feature definitions in its training data. If production calculates those features differently, the model receives values with different meanings. Offline scores can look good while live predictions become unreliable.
Where skew comes from
Common causes include:
| Cause | Training computation | Serving computation |
|---|---|---|
| Time window | Uses the previous 30 calendar days | Uses the last 30 events |
| Missing values | Fills missing values with the global mean | Uses zero |
| Event timing | Includes all rows in a historical table | Uses only rows already ingested |
| Aggregation | Calculates an average over completed data | Reads a cached value with a different refresh time |
| Categories | Uses a fixed vocabulary | Handles unknown categories differently |
The most important rule is to define a feature once and reuse its semantics everywhere. The execution may differ. Training might use SQL over historical data, while serving might read an online feature store. Both paths must answer the same question.
For example, define avg_spend_30d as:
- For one customer and an as-of time, include purchases whose event time is within the previous 30 days.
- Include only purchases available to the system by that as-of time.
- Return the arithmetic mean of the purchase amounts.
- Return a documented missing value when no purchase qualifies.
Use one feature contract
A feature contract is a written definition of a feature. It specifies its name, type, time semantics, allowed missing values, window, and refresh behavior.
The training and serving implementations should both follow the contract. A shared library is useful when both paths use the same programming language. When they use different systems, test the implementations against the same input cases.
Share the time-aware computation
The function below accepts an as_of time and an available_at time. The historical training call uses the label's prediction time as both values. The serving call uses the current time. The same filtering and aggregation rules apply in both cases.
from datetime import datetime, timedelta, timezone
events = [
{"customer_id": "c1", "event_ts": datetime(2026, 1, 5, tzinfo=timezone.utc),
"ingested_ts": datetime(2026, 1, 5, 1, tzinfo=timezone.utc), "amount": 40.0},
{"customer_id": "c1", "event_ts": datetime(2026, 1, 20, tzinfo=timezone.utc),
"ingested_ts": datetime(2026, 1, 25, tzinfo=timezone.utc), "amount": 60.0},
{"customer_id": "c1", "event_ts": datetime(2026, 2, 10, tzinfo=timezone.utc),
"ingested_ts": datetime(2026, 2, 10, 1, tzinfo=timezone.utc), "amount": 80.0},
]
def avg_spend_30d(events, customer_id, as_of, available_at):
start = as_of - timedelta(days=30)
amounts = [
event["amount"]
for event in events
if event["customer_id"] == customer_id
and start <= event["event_ts"] <= as_of
and event["ingested_ts"] <= available_at
]
return sum(amounts) / len(amounts) if amounts else None
prediction_ts = datetime(2026, 1, 30, tzinfo=timezone.utc)
training_value = avg_spend_30d(events, "c1", prediction_ts, prediction_ts)
serving_ts = datetime(2026, 2, 10, 12, tzinfo=timezone.utc)
serving_value = avg_spend_30d(events, "c1", serving_ts, serving_ts)
assert training_value == 40.0 # The 60.0 event arrived after prediction_ts.
assert serving_value == 70.0 # The 60.0 and 80.0 events are in the live window.
print(training_value, serving_value)The second purchase has an event time before the training prediction, but its ingestion time is later. Excluding it prevents training from using information that was unavailable at that time.
Validate parity with fixtures
Create small, fixed test cases for boundaries, missing data, late ingestion, duplicate events, and time zones. Run those cases through both the historical and serving implementations. Compare values with an allowed floating-point tolerance.
A useful production check compares feature distributions between training and serving. Large changes in null rates, averages, or percentiles can reveal a broken refresh job or a semantic mismatch. Distribution checks do not replace point-in-time tests, but they help detect drift after deployment.
Practical checklist
- Store the feature definition with its version and source columns.
- Use UTC timestamps and explicit boundary rules.
- Apply the same filters, windows, units, and missing-value policy.
- Test event-time and ingestion-time behavior.
- Compare batch and online results before deploying a model.
- Monitor null rates and feature distributions after deployment.
Exercise
A training feature averages all purchases in the 30 days before prediction, but serving averages the customer's last 30 purchases. What kind of problem is this, and how should you fix it?
Show answer
It is training-serving skew. Choose one precise feature contract, then make both paths use the same time window and aggregation semantics. Add shared fixture tests that compare their outputs.