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:

FeatureQL
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
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.TRAIN.HIGH_SPENDERCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.MERCHANTSCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.MERCHANT_IDCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.PAYMENTSCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.PAYMENTS_BATCHCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.PAYMENT_IDCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.SPEND_30DCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.SPEND_30D_STOREDCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.STOREDCREATEDFeature created as not exists

VARIANT() replaces the sum with the stored value, everywhere the rule reads it ( Variants ):

FeatureQL
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
);
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.TRAIN.HIGH_SPENDER_SERVEDCREATEDFeature 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:

FeatureQL
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
;
Result
MERCHANT_ID BIGINTSPEND_30D DECIMAL(38,2)SPEND_30D_STORED DECIMAL(12,2)HIGH_SPENDER BOOLEANHIGH_SPENDER_SERVED BOOLEAN
160.0060.00FALSEFALSE
2120.0090.00TRUEFALSE

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 ):

FeatureQL
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)
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.TRAIN.HIGH_SPENDER_LOGCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.HIGH_SPENDER_PSCREATEDFeature created as not exists
FM.TUTORIALS.TRAIN.TRAINING_LOGSCREATEDFeature created as not exists

The caller gets the served rule only:

Prepared
FM.TUTORIALS.TRAIN.HIGH_SPENDER_PS
Call 1
{"input_1": [[1], [2]]}
MERCHANT_ID BIGINTHIGH_SPENDER_PS BOOLEAN
1FALSE
2FALSE

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:

FeatureQL
SHOW SERVING LOGS LIKE 'FM.TUTORIALS.TRAIN.HIGH_SPENDER_LOG' LIMIT 2;
Result
LOGGER VARCHARSINK VARCHARSINK_URL VARCHARCALL_ID VARCHARCALL_INDEX BIGINTCALL_TIME TIMESTAMPCALL_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_LOGFM.TUTORIALS.TRAIN.TRAINING_LOGSfile:///tmp/featuremesh_logsOK"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';
sql

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