# Transform

> Ensures identical feature transformation logic at training and serving time

- **Category**: Machine Learning
- **Subcategory**: Data and Feature Representation
- **Canonical URL**: https://designpattern.fyi/patterns/transform/

---

## Description
**Intent**: Prevent training/serving skew by guaranteeing that exactly the same transformation logic executes at both training time and serving time.

**Context**: The code that turns raw fields into model-ready features (normalizing, bucketing, crossing) often gets written twice: once for training and again for serving, sometimes in different languages. This gap is a common source of skew where features are computed differently in production than during training.

**Solution**: Keep three things explicitly separate: raw input, transformation logic, and transformed features. Package the transformation logic as part of the deployed model artifact rather than as separately maintained application code. Version the transformation logic together with the model weights it was trained against.



## Use Cases
- Production ML systems with nontrivial preprocessing
- Feature pipelines that need consistency across training and serving
- Models deployed to different environments (cloud, edge, mobile)
- Any system where training/serving skew has caused issues






## Trade-offs


### Advantages

- Eliminates training/serving skew from transformation mismatches

- Transformation logic deployed as versioned artifact with model

- Consistent feature computation across all environments

- Easier rollback of both model and transformations together




### Considerations & Drawbacks

- Requires infrastructure to execute same code in training and serving

- Upfront engineering investment if infrastructure doesn't exist

- Adds complexity to model artifact packaging







---
**Reference**: [Original Source](https://github.com/GoogleCloudPlatform/ml-design-patterns)

