Machine learning with football · Chapter 11 · Production ML systems

When to transform data

Transform the data before training or inside the model, and keep training and serving exactly alike.

Estimated time: 10 minutes · page 4 of 8

Before training, or inside the model

Raw data needs feature engineering, and there are two places to do it. Before training, a separate step transforms the data once and stores the result. You can study the whole dataset to choose the transforms. But you have to repeat exactly the same transforms when serving, often in different code. The gap that opens up is training-serving skew, and it's the classic production bug.

Inside the model, the transforms are part of the model code. Training and serving are then the same by construction, and changing a transform doesn't mean rebuilding the stored data. The costs are that the transforms run for every batch and add latency.

Watch one trap on the board. Z-scores need a mean and a standard deviation. Compute them per batch and the same 4-goal match gets a different z-score in every batch. Press Another batch and compare. The fix is to compute the mean and SD once over the whole training set and use them as fixed numbers, in training and when serving.

Check yourself

A model z-scores goals with the mean and SD of each batch of 10 matches. What goes wrong?
Features are transformed by one program before training and by a different one when serving. What is the danger called?

Leagues