Train on what serving saw
This guide builds a training set from the values serving actually returned, so that a model trains on what it will see in production.
A value served from a precomputed store can differ from the same feature computed in batch: the store lags behind late events, or was written by an older definition. A model trained on the batch value then meets other values in production. This is training/serving skew. Logging each call removes it: the log holds the inputs and the output as served, the precomputed values read included.
Before you start, have the feature served by a prepared statement , and a place for the logs: an object store, files, or stdout:// read by a collector ( Where to write ).
1. Serve the precomputed value through the batch feature
Write the rule once, over the batch feature, and serve a variant that reads the stored value instead. SPEND_30D is computed from the payments; STORED stands for the reader of a hook's sink, which you get from SHOW CREATE SINK … READER ( Reading back ). Merchant 2's stored value has not seen its last payment yet:
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.TRAIN AS
PERSIST
merchants := ENTITY(),
merchant_id := INPUT(BIGINT#merchants),
payments := ENTITY(),
payment_id := INPUT(BIGINT#payments),
payments_batch := INLINE_COLUMNS(
payment_id BIGINT#payments BIND TO payment_id,
merchant_id BIGINT#merchants,
amount DECIMAL(12,2)
FROM CSV(
payment_id,merchant_id,amount
1,1,40
2,1,20
3,2,90
4,2,30
)
),
spend_30d := RELATED(
SUM(payments_batch[amount]) ON payments_batch[merchant_id] = merchant_id
),
stored := INLINE_COLUMNS(
merchant_id BIGINT#merchants BIND TO merchant_id,
spend_30d DECIMAL(12,2)
FROM CSV(
merchant_id,spend_30d
1,60
2,90
)
),
spend_30d_stored := stored[spend_30d],
high_spender := spend_30d >= 100
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.TRAIN.HIGH_SPENDER | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.MERCHANTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.MERCHANT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.PAYMENTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.PAYMENTS_BATCH | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.PAYMENT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.SPEND_30D | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.SPEND_30D_STORED | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.STORED | CREATED | Feature created as not exists |
VARIANT() replaces the sum with the stored value, everywhere the rule reads it ( Variants ):
CREATE OR REPLACE FEATURE FM.TUTORIALS.TRAIN.HIGH_SPENDER_SERVED AS
VARIANT(
FM.TUTORIALS.TRAIN.HIGH_SPENDER
REPLACING FM.TUTORIALS.TRAIN.SPEND_30D
WITH FM.TUTORIALS.TRAIN.SPEND_30D_STORED
);| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.TRAIN.HIGH_SPENDER_SERVED | CREATED | Feature created as not exists |
The two versions disagree for merchant 2: a high spender in batch (120), not when served (90). A model trained on HIGH_SPENDER would learn from answers serving never gave:
RETURN
fm.tutorials.train.merchant_id,
fm.tutorials.train.spend_30d,
fm.tutorials.train.spend_30d_stored,
fm.tutorials.train.high_spender,
fm.tutorials.train.high_spender_served
FOR
fm.tutorials.train.merchant_id := BIND_VALUES(ARRAY(1, 2)),
NESTED fm.tutorials.train.payment_id := BIND_VALUES(ARRAY(1, 2, 3, 4))
ORDER BY fm.tutorials.train.merchant_id
;| MERCHANT_ID BIGINT | SPEND_30D DECIMAL(38,2) | SPEND_30D_STORED DECIMAL(12,2) | HIGH_SPENDER BOOLEAN | HIGH_SPENDER_SERVED BOOLEAN |
|---|---|---|---|---|
| 1 | 60.00 | 60.00 | FALSE | FALSE |
| 2 | 120.00 | 90.00 | TRUE | FALSE |
2. Log every call, with the values it read
The prepared statement serves the variant and lists the stored value in CAPTURE: it is computed in the same call, from the same read, and never returned to the caller. A logger on the statement writes each call's inputs, output and captures ( Feature logging ):
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.TRAIN AS
PERSIST
high_spender_ps := PREPARED_STATEMENT(
high_spender_served
USING merchant_id
CAPTURE (spend_30d_stored)
),
training_logs := SINK(
'file:///tmp/featuremesh_logs'
WITH (
FORMAT = 'jsonl'
)
),
high_spender_log := LOGGER(ON high_spender_ps TO training_logs)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.TRAIN.HIGH_SPENDER_LOG | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.HIGH_SPENDER_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.TRAIN.TRAINING_LOGS | CREATED | Feature created as not exists |
The caller gets the served rule only:
FM.TUTORIALS.TRAIN.HIGH_SPENDER_PS{"input_1": [[1], [2]]} | MERCHANT_ID BIGINT | HIGH_SPENDER_PS BOOLEAN |
|---|---|
| 1 | FALSE |
| 2 | FALSE |
3. Check what the log holds
SHOW SERVING LOGS shows the last rows each logger kept, without opening the sink. Each row holds the stored value the call read, next to the answer:
SHOW SERVING LOGS LIKE 'FM.TUTORIALS.TRAIN.HIGH_SPENDER_LOG' LIMIT 2;| LOGGER VARCHAR | SINK VARCHAR | SINK_URL VARCHAR | CALL_ID VARCHAR | CALL_INDEX BIGINT | CALL_TIME TIMESTAMP | CALL_DETAILS ROW(status VARCHAR, error_code VARCHAR, error_message VARCHAR, latency_ms DOUBLE, row_count BIGINT, server_version VARCHAR, boot_id VARCHAR, path VARCHAR, source ROW(partition INTEGER, "offset" BIGINT, drain BIGINT)) | RECORD VARCHAR |
|---|---|---|---|---|---|---|---|
| FM.TUTORIALS.TRAIN.HIGH_SPENDER_LOG | FM.TUTORIALS.TRAIN.TRAINING_LOGS | file:///tmp/featuremesh_logs | OK | "fm.tutorials.train.spend_30d_stored": |
4. Join the logs to labels
Labels come later, from your own tables. Join the logs on the key, keep the label observed after the call, and keep the calls that succeeded. In DuckDB, over Parquet logs:
SELECT l."fm.tutorials.train.merchant_id" AS merchant_id,
l."fm.tutorials.train.spend_30d_stored" AS spend_30d,
l."fm.tutorials.train.high_spender_ps" AS high_spender,
c.charged_back
FROM read_parquet('s3://acme-ml/serving-logs/fm.tutorials.train.high_spender_log/*/*/*/*.parquet', hive_partitioning = true) l
JOIN chargebacks c ON c.merchant_id = l."fm.tutorials.train.merchant_id" AND c.observed_at > l.call_time
WHERE l.call_details.status = 'OK'; The folders and columns of a logged row are in A logged row . A logged call can also be replayed in a query: bind the inputs it logged in FOR, the request time included, and compare ( Replaying a call ).
Values a hook computed
A hook stores only the latest value of each key: its sink is not a history. To keep every value a hook computed, put a logger on the hook's statement. Its rows name the hook and the record or drain behind each call in call_details ( A logged row ).
Train on the serving statement's log, not on the hook's: the serving log holds what the endpoint answered, read from the store at the time of the call.
Common gaps
- A log is best effort. A logger keeps what it delivers: a crash loses the open file, and a full queue drops calls ( Delivery ). Watch the drop metrics ( Monitor and recover ).
- No call, no row. A record that does not decode makes no call, so a hook's logger never sees it.
- A sample biased by time. Filter the rows a logger keeps by a hash of the key (
WHERE HASH01(…) < 0.5), not at random, so that a key's history is either logged whole or not at all ( The logger ).
Related
- Late, corrected and missing events : why a stored value lags behind the batch one.
- Port a batch feature : the same
VARIANT()pattern, for a mapping.