← 中文资源

简体中文版本

如何实现特征版本化与时点正确性

版本化特征并时点正确地计算训练集,使模型只看到每次预测时刻可知的信息。

阅读英文原文

引言

在泄漏未来信息的特征上训练的模型离线表现极好、生产失败。典型错误是用最新特征值计算训练集,使上月的行被一个直到今天才存在的特征值标注。时点正确性通过把每个标注样本连接到该样本时间戳时的特征值来修复。特征版本化是配套:当特征定义变化,旧值必须对在其上训练的模型仍可复现。

时点正确性意味什么

对每个训练样本,特征必须是预测时实际可用的值,而非现在已知的值。若特征是客户生命周期消费,一月的训练样本必须用一月时的生命周期消费,而非当前总额。用当前总额把未来泄漏进过去。

同样适用于标签:标签是预测窗口后已知的结果,特征时间与标签时间的切分必须被尊重。搞错会产出离线准确、生产失望的模型,因为离线时它在作弊。

时点连接

时点正确性是一种 as-of 连接:对时间 T 的每个样本,找时间戳最大且小于等于 T 的特征记录。基于事件的特征天然 as-of;快照特征需要带 valid-from 时间戳的历史表。连接于是取 T 时有效的版本,而非最新。

高效 as-of 连接用 (entity_id, valid_from) 上的索引,并对每个样本查找不超过 T 的最新行。在数仓 SQL 中这是横向连接或窗口查找;关键是连接条件包含时间界,而非仅实体。

特征版本化

特征不只是名字;它是定义。当定义变化,如用新规则计的分流失分,特征含义变了而名字可能没变。版本化意味每个特征有版本、每个值记录产生它的版本,使在 v1 上训练的模型仍能读 v1 值。

版本化防止模型读取它未被训练的定义所产值的微妙灾难。新定义成为新特征版本;模型显式采用它。旧版本保持可计算或至少可读,直到每个用过它们的模型被退役。

训练—服务一致性

训练用的特征必须以与服务时相同的方式计算。在 SQL 中算特征的训练流水线与在应用代码中算的服务路径会漂移,且漂移在模型表现不佳前可能不可见。共享计算,或校验两者在样本上一致,至关重要。

常见防护是记录服务的特征向量并在事后与训练流水线为该时间戳计算的同一特征比较。偏离指向两条路径之一的 bug,在其使模型劣化前。这是保持训练与服务诚实的反馈回路。

回填与迟到数据

当某特征的历史值因 bug 被算错,你必须回填:用修正逻辑重算受影响历史,并重训用过它的模型。只有特征是源数据的确定性函数时才可能,因此保留原始输入。

迟到数据使 as-of 连接复杂:本应在 T 时可知的值可能在 T 后到达。显式决定规则:要么模型必须用 T 时可得的内容(严格,但值可能缺失),要么流水线必须知道哪些迟到数据本会可得。多数系统选严格 as-of,接受不用某些迟到数据,因为用它就会泄漏。

服务新鲜度与缓存

在线服务必须快速提供截至现在或请求时的特征值。特征存储缓存最新值以供低延迟服务,并读历史以供 as-of 训练,这就是特征存储分离在线与离线视图的原因。两个视图必须一致、从同一源计算。

为服务缓存最新值可以,但缓存必须有过期上界,且训练集绝不能从服务缓存读。保持离线存储为特征的真相之源,并把在线存储当作服务用最新值的物化视图。

常见陷阱与小结

不要从最新特征值构建训练集;那泄漏未来。不要使训练与服务路径以不同方式计算特征。不要在同一版本下改特征定义。不要静默忽略迟到数据;决定规则。也不要忘记标签也有时间,特征与标签的切分必须被尊重。

时点正确性把每个训练样本连接到其时间有效的特征值,特征版本化在定义变化后保持值可读。用 as-of 连接构建训练集、版本化每个特征、从一源保持训练与服务一致、逻辑变时回填、并为迟到数据选显式规则。于是模型的离线指标在生产中也有意义。

代码示例

# the leakage
training row: event at 2026-01-15, label = did they churn by 2026-02-15
features computed:  lifetime_spend = current_value (2026-10)
=> the model saw 9 months of future spend; offline metrics are inflated
# point-in-time: join features AS OF 2026-01-15, label from the window after
-- As-of join: the feature row valid at the example's time, per entity.
SELECT e.entity_id, e.event_time, e.label,
       f.lifetime_spend
FROM training_examples e
LEFT JOIN LATERAL (
  SELECT lifetime_spend
  FROM customer_features f
  WHERE f.entity_id = e.entity_id
    AND f.valid_from <= e.event_time       -- the as-of bound
  ORDER BY f.valid_from DESC
  LIMIT 1
) f ON true;
-- Feature rows carry a definition version.
CREATE TABLE customer_features (
  entity_id      bigint NOT NULL,
  feature_name   text   NOT NULL,
  feature_version int   NOT NULL,
  valid_from     timestamptz NOT NULL,
  value          double precision,
  PRIMARY KEY (entity_id, feature_name, feature_version, valid_from)
);
-- a model records the (feature_name, feature_version) set it was trained on
# training-serving skew guard
serving:  compute + log feature vector (with entity + event_time)
later:    recompute from the offline store AS OF that event_time
compare:  served vs recomputed -> must match within a tolerance
# a mismatch => the two paths disagree; fix before it hurts the model
# backfill + late data
backfill: recompute history with fixed logic -> retrain models on it
          (possible only if features are deterministic from raw inputs)
late data: a value that arrives after T
  strict as-of: use only what was available at T (may be missing) -> no leak
  lenient:      use late data -> risks leaking the future
# choose strict; document that late data is not used in training
# online vs offline feature views
online store: latest value per entity, low latency, bounded staleness
              -> used at inference only
offline store: full history with valid_from, as-of joins
              -> used to build training sets (never the serving cache)
# both computed from the SAME source; validate they agree on a sample