Packaging Feature Retrieval Specifications
This lesson explains training-serving skew, how the managed feature store defines and materializes features, and how a feature retrieval specification packaged with a model prevents skew.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what causes training-serving skew.
- Describe feature sets, entities and materialization.
- Explain how point-in-time joins prevent leakage.
- Describe the role of feature_retrieval_spec.yaml.
Training-serving skew
A model learns from features: values such as a customer's number of transactions in the last 30 days. If the inference code computes that value differently from the training code, for example over a different window or with a different treatment of missing data, the model receives inputs it was not trained on and its predictions quietly degrade. This is called training-serving skew, and it is one of the most common causes of models that test well and perform badly in production.
A feature store prevents skew by defining each feature once and serving the same values to training and inference. The feature retrieval specification, covered below, is what ties a particular model to the exact features it needs.
The managed feature store
The Azure Machine Learning managed feature store is a special kind of workspace that holds feature definitions for an organization. Its main objects are listed in the following table.
| Object | Description | Example |
|---|---|---|
| Entity | The key that features describe, with its index columns | account indexed by account_id |
| Feature set | A versioned group of features with a source, transformation code, an entity and a timestamp column | transactions version 1: 7-day and 30-day spend |
| Materialization | A scheduled or backfill job that computes feature values and stores them | Recompute every 3 hours |
| Offline store | Azure Data Lake Storage Gen2 storage of feature history | Used to build training data and batch scoring data |
| Online store | Low-latency store backed by Redis | Used for real-time lookups in online endpoints |
Training data is built with a point-in-time join: for each labelled observation, the feature store returns the feature values as they were at the observation's timestamp, not as they are today. This prevents data leakage, in which a model trains on information that would not have been available when the prediction was needed.
The feature retrieval specification
A feature retrieval specification is a YAML file, feature_retrieval_spec.yaml, that lists the feature store, the feature sets and versions, and the individual features a model uses, with the index columns needed to look them up. It is generated with the feature store SDK from the list of features chosen during experimentation.
from azureml.featurestore import FeatureStoreClient
from azure.identity import DefaultAzureCredential
fs = FeatureStoreClient(credential=DefaultAzureCredential(), subscription_id=SUB,
resource_group_name="rg-features", name="fs-contoso")
transactions = fs.feature_sets.get("transactions", "1")
accounts = fs.feature_sets.get("accounts", "1")
features = [transactions.get_feature("spend_7d"), transactions.get_feature("spend_30d"),
accounts.get_feature("account_age_days")]
fs.generate_feature_retrieval_spec("./fraud_model/feature_retrieval_spec", features)The training pipeline uses the specification with the built-in feature retrieval component to produce training data. The training step then copies the specification into the model's output folder, so that when the model is registered, the specification is part of the model artifact.
Using the specification at inference
Packaging the specification with the model means inference needs nothing but the model to know which features to retrieve.
-
Batch scoring
A batch pipeline passes the registered model to the feature retrieval component, which reads the specification from the model artifact and retrieves the features for the observations to score from the offline store.
-
Online scoring
The scoring script's
init()reads the specification from the model folder and initializes online lookup;run()receives the entity keys, such as an account ID, retrieves the current features from the online store, and calls the model. -
New model version
A model trained on different features carries a different specification, so deploying it changes the features retrieved automatically.
The endpoint's identity needs access to the feature store and its online store, and the online store must be materialized for the features used, or online lookups return no values.
Skew between batch and real time
A fraud model performs well in nightly batch scoring but poorly in a new real-time endpoint. Investigation shows the real-time scoring script computes the 30-day spend feature itself from a transactions database, while training used the feature store's spend_30d. The team wants both paths to use the same features with the least ongoing maintenance.
Generate a feature retrieval specification for the model's features, package it in the model artifact during training, and change the online scoring script to read the specification from the model folder and retrieve features from the feature store's online store, with the feature sets materialized online. Batch scoring already uses the offline store, so both paths now serve the same feature definitions.
Fixing the database query to match the training logic would work until the feature definition changes again. Retraining the model on the database-computed values would align the online path but break the batch path. Increasing the endpoint's instance count does not affect feature values.
What does packaging a feature retrieval specification with a model achieve?
Show answer
It records which feature sets and features the model was trained with, so inference retrieves exactly the same features from the feature store, which prevents training-serving skew.
Exam Tips
Remember for the exam
- Training-serving skew happens when inference computes features differently from training.
- Feature sets have entities, a timestamp column and transformation code; materialization fills offline and online stores.
- Point-in-time joins prevent data leakage when building training data.
- Package feature_retrieval_spec.yaml in the model artifact so inference retrieves the same features.