Train on exactly what serving saw
Two copies of the same feature can quietly disagree.
Two correct implementations can disagree by a day
A member in New York last played at 23:30 local time, and asks again at 01:00. Training counts days in UTC and serving in local time. What do they say?
| Days since last play, New York, UTC − 5 | In New York | In UTC |
|---|---|---|
| last play | Mar 1, 23:30 | Mar 2, 04:30 |
| the request | Mar 2, 01:00 | Mar 2, 06:00 |
| training: SQL, days cut at midnight UTC | 0 days | |
| serving: Java, days in the member's time zone | 1 day |
Training computes days since last play in SQL, with day boundaries in UTC. Serving computes it in Java, in the member's time zone. Each passes its tests. For some members at some hours, they disagree by one.
That's one of four ways a mismatch passes its unit tests:
- Two implementations. The day boundary above.
- Missing values. Offline, a missing value becomes NULL, and the training code fills 0. Online, the client sends
-1. - Time. Training joins today's feature table onto last month's labels, so every row sees the future.
- Defaults. Online, a store times out and the request gets a default. Offline, the recomputed value is always there, so the model has never seen the default.
Google's Rules of Machine Learning defines training-serving skew as a difference between performance during training and performance during serving. It names three causes:
- a difference between how the training and serving pipelines handle data
- a change in the data between when you train and when you serve
- a feedback loop between your model and your algorithm.
Feature serving owns the first two: the code half and the data half.
Netflix's 2016 post on its feature time machine explains why writing a feature twice keeps failing. Two implementations are hard to match exactly, especially because any difference between the offline and online data sources can change the model's output in ways nobody expected.
Netflix's fix was shared code. Its feature encoders run on the exact same Java objects in offline experiments and in production. Uber got there with a feature DSL, a small language for writing features. The same expressions run at training time and at prediction time, so the model gets the same features in both.
One definition removes the code half of skew. The data half needs timestamps.
In the editor, write days_since(last_utc, now_utc, offset_hours), the count serving makes. last and now are the New York member's last play and request from the table, in UTC.
- 1
Write
days_since(last_utc, now_utc, offset_hours): the calendar days between the two moments, counted on the member's local calendar.offset_hoursis -5 for New York.
Run your code to check it. Output shows here.