In TFDV, when is a feature's `skew_comparator` evaluated versus its `drift_comparator`, and why can an `infinity_norm` threshold miss a numeric feature?
answer
- two control datasets, two arguments
- training against serving, today against yesterday
- configured per feature in the schema
- which histogram each threshold reads
- Jensen-Shannon covers numeric too
basics
~20 sThe skew_comparator runs only when validate_statistics receives serving_statistics (training against serving); the drift_comparator only when it receives previous_statistics (current against an earlier batch). An infinity_norm threshold reads categorical value counts, so numeric features need jensen_shannon_divergence.
solid answer
~40 sBoth comparators are set per feature in the schema, and each is evaluated only when its control statistics are passed to `tfdv.validate_statistics`. With `serving_statistics=`, the `skew_comparator` compares the training statistics in `statistics` with serving statistics; with `previous_statistics=`, the `drift_comparator` compares the current statistics with an earlier batch, such as yesterday's training data. Pass neither and neither runs, whatever the schema says. An `infinity_norm` threshold measures the largest gap between normalised value frequencies in the categorical rank histogram, so the TFDV guide says it detects only categorical features; a `jensen_shannon_divergence` threshold also reads numeric histograms and covers both. Every compared feature's raw distance lands in `drift_skew_info` in the returned `Anomalies`, threshold crossed or not.
code
python · 22 linesimport tensorflow_data_validation as tfdv
schema = tfdv.load_schema_text(schema_path)
train_stats = tfdv.generate_statistics_from_tfrecord(data_location=train_today_path)
prev_stats = tfdv.generate_statistics_from_tfrecord(data_location=train_yesterday_path)
serving_stats = tfdv.generate_statistics_from_tfrecord(data_location=serving_log_path)
# categorical feature: L-infinity over value frequencies
tfdv.get_feature(schema, "ad_slot").skew_comparator.infinity_norm.threshold = 0.01
# numeric feature: L-infinity would never see it, so use Jensen-Shannon
tfdv.get_feature(schema, "dwell_ms").drift_comparator.jensen_shannon_divergence.threshold = 0.05
anomalies = tfdv.validate_statistics(
statistics=train_stats,
schema=schema,
previous_statistics=prev_stats, # evaluates drift_comparator
serving_statistics=serving_stats, # evaluates skew_comparator
)
for info in anomalies.drift_skew_info:
print(info)
if anomalies.anomaly_info:
raise RuntimeError("TFDV skew or drift anomaly; stopping this step")go deeper
Remember that skew means training against serving and drift means one batch against an earlier one, and that each needs its own statistics passed in.
Explain which argument triggers which comparator, which histogram each threshold type reads, and why infinity_norm leaves numeric features unchecked.
Show you would set comparator types by feature type in the reviewed schema, log drift_skew_info daily, and treat a missing serving-statistics feed as a gap in the check.
Weigh the cost of collecting serving statistics for every model against the skew it reveals, and decide who owns comparator settings when several teams' features meet in one schema.
## Two comparisons, selected by two arguments Besides checking a batch against the schema, TFDV can compare the **statistics of two datasets**, feature by feature. Which comparison runs is decided by two things together: a **comparator configured on the feature in the schema**, and the **control statistics passed** to `tfdv.validate_statistics(statistics, schema, environment=None, previous_statistics=None, serving_statistics=None)`. | Comparator on the feature | Evaluated when you pass | Treatment (`statistics`) | Control | Typical use | |---|---|---|---|---| | `skew_comparator` | `serving_statistics` | training statistics | serving statistics | training data against what the live model is sent | | `drift_comparator` | `previous_statistics` | current statistics | an earlier batch's statistics | today's training data against yesterday's | - Both are **opt-in twice**: a comparator without its matching argument is never evaluated, and an argument passed for a feature with no comparator compares nothing for that feature. - Both arguments can be passed in the same call; TFDV evaluates each comparison it has inputs for. - When either comparison runs, the raw measurement for every compared feature is written to `drift_skew_info` in the returned `Anomalies` proto, whether or not it crossed the threshold. - When the control statistics are present but lack a feature, the anomalies reference lists `COMPARATOR_CONTROL_DATA_MISSING`, with the `infinity_norm` thresholds as its schema fields. ## Threshold types and what each one can see A comparator holds threshold messages, and they do not read the same part of the statistics. That is why one of them silently ignores numeric features: | Threshold | Reads | Detects on | Anomaly raised | |---|---|---|---| | `infinity_norm.threshold` | the `rank_histogram` of string statistics | categorical features only | `COMPARATOR_L_INFTY_HIGH` | | `normalized_abs_difference.threshold` | the `rank_histogram` of string statistics | categorical value counts | `COMPARATOR_NORMALIZED_ABSOLUTE_DIFFERENCE_HIGH` | | `jensen_shannon_divergence.threshold` | numeric `STANDARD` histograms and the `rank_histogram` | numeric and categorical features | `COMPARATOR_JENSEN_SHANNON_DIVERGENCE_HIGH` | The TFDV guide says it directly: the L-infinity norm "will only detect skew for the categorical features", while a `jensen_shannon_divergence` threshold detects skew for both numeric and categorical features. Setting `infinity_norm` on a numeric feature such as `dwell_ms` therefore produces no anomaly however far its distribution moves. How each distance behaves and where a sensible threshold comes from are statistics questions of their own; the tool question is which field reads which histogram. ## What a skew anomaly looks like The TFDV guide's example shows the shape of a finding. With an `infinity_norm` threshold of 0.01 on the categorical `payment_type`, serving data holding many more `Cash` values produces an entry whose short description is "High L-infinity distance between serving and training" and whose long description gives the distance (0.0435984 in the example), the threshold it exceeded, and the feature value with the maximum difference. That last part is the useful one in an incident: it names the category that moved. A Jensen-Shannon finding arrives as `COMPARATOR_JENSEN_SHANNON_DIVERGENCE_HIGH`, and both kinds sit in `anomaly_info` beside ordinary schema anomalies, so one check of that map covers schema, skew and drift together. ## A dataset-level drift check Row counts drift too. `schema.dataset_constraints.num_examples_drift_comparator` takes a `min_fraction_threshold` and a `max_fraction_threshold`. When previous statistics are passed and today's example count divided by the previous count falls below or above them, TFDV raises `COMPARATOR_LOW_NUM_EXAMPLES` or `COMPARATOR_HIGH_NUM_EXAMPLES`. ## The label that serving never sends By default every dataset is validated against one schema, so a label such as `clicked`, present in training and absent from serving data, shows up as an anomaly when serving statistics are validated against the schema. **Schema environments** handle it: 1. add `'TRAINING'` and `'SERVING'` to `schema.default_environment`; 2. append `'SERVING'` to `tfdv.get_feature(schema, 'clicked').not_in_environment`; 3. validate serving statistics with `environment='SERVING'`. ## Wiring it into a daily retrain 1. Generate today's training statistics, keep yesterday's, and compute statistics over the serving data the live model received. 2. Configure each feature's comparators once, in the reviewed schema, choosing `jensen_shannon_divergence` for numeric features. 3. Pass `previous_statistics` for drift and `serving_statistics` for skew, and fail the step when the returned `Anomalies` proto holds any entry. 4. Log `drift_skew_info` every day, so a slow rise below the threshold is visible before it becomes an anomaly. Why training and serving data come apart in the first place is a separate subject. TFDV's part is to measure the gap once both sets of statistics exist, and to measure it only where you configured a comparator that can see the feature's type.
- What does validate_statistics record when a compared feature stays under its threshold?Whenever drift or skew detection runs, the raw measurement for every compared feature is written to `drift_skew_info` in the returned `Anomalies` proto, whether or not an anomaly fired. Logging it daily turns a pass/fail check into a series you can watch creep towards the threshold.
- The label `clicked` is present in training data but absent from serving data. How do you stop TFDV flagging serving statistics?Use schema environments: add `'TRAINING'` and `'SERVING'` to `schema.default_environment`, append `'SERVING'` to `tfdv.get_feature(schema, 'clicked').not_in_environment`, and call `tfdv.validate_statistics(serving_stats, schema, environment='SERVING')`. The label is then still required in training and not expected at serving.
- Can a TFDV drift check catch a day whose row count halved?Yes, at dataset level: set `schema.dataset_constraints.num_examples_drift_comparator.min_fraction_threshold` (and `max_fraction_threshold` for growth). With `previous_statistics` passed, a current-to-previous example-count ratio outside the bounds raises `COMPARATOR_LOW_NUM_EXAMPLES` or `COMPARATOR_HIGH_NUM_EXAMPLES`.
- A skew_comparator in the schema is checked on every validate_statistics call
- In TFDV, drift means training data compared with serving data
- An infinity_norm threshold works the same on numeric and categorical features
- skew_comparator and drift_comparator are two names for one check
- TFDV comparators need the raw examples of both datasets, not statistics