Skip to content

Level 6 5C 预测子块

Abstract

入口:

python
index_list = self._get_index(...)
batch_maker = RollingForecastEvalBatchMaker(...)
predicts = model.batch_forecast(horizon, predict_batch_maker)

这一层解释:

_eval_batch(...) 的预测子块,怎样把测试区间组织成 rolling 批量预测请求,并交给 model.batch_forecast(...)

1. 上一层与当前层位置

上一层是:

同层兄弟节点已经有:

当前层是:

  • Level 6:细分 5C 预测子块

1.5 这一层的关系类型说明

这一层是在继续下钻:

  • Level 5 里的 5C 预测子块

所以这里的 6C-1 / 6C-2 / 6C-3 / 6C-4

  • 预测子块内部的逻辑分块
  • 不是 Level 5 的新同级主节点

1.6 同层文件关系

这一层和下面两篇是同层兄弟文档:

三者之间没有直接调用顺序。
它们分别细分的是 25 里的三个不同兄弟子块:

  • 5B
  • 5C
  • 5D

2. 当前层第一性

5C 预测子块 的第一性是:

把“测试区间上的一系列 rolling 预测位置”组织成 batch 形式的预测请求,再统一交给模型的 batch_forecast(...)

这一层不是在算 metric,也不是在训练。
它负责的是:

  1. 决定在哪些位置做 rolling 预测
  2. 把这些位置包装成 batch maker
  3. 调模型做批量预测
  4. 把多批次预测拼回 all_predicts

3. 当前命令的最小例子

当前例子的关键参数是:

  • series.shape = (17420, 7)
  • train_length = 13936
  • test_length = 3484
  • horizon = 24
  • stride = 24
  • num_rollings = 48
  • batch_size = 4
  • seq_len = 96

所以这一层从:

python
index_list = self._get_index(13936, 3484, 24, 24)
index_list = index_list[:48]

开始,再进入:

python
model.batch_forecast(24, predict_batch_maker)

4. 先看顺序图

5. 抽象索引树

6. 职责树

6.1 6C-1 预测位置生成

职责:

  • 根据 train_length / test_length / horizon / stride 生成 rolling 起点列表

输出:

  • index_list

6.2 6C-2 预测请求包装

职责:

  • 把整条序列和 rolling 位置包装成 batch maker

输出:

  • batch_maker
  • predict_batch_maker

6.3 6C-3 调模型预测

职责:

  • 逐批调用 model.batch_forecast(...)
  • 统计 total_inference_time

输出:

  • all_predicts(list of np.ndarray)

6.4 6C-4 合并全部预测结果

职责:

  • 把多次 batch_forecast(...) 的结果拼成一个总数组

输出:

  • all_predicts: np.ndarray

7. 输入输出接口

7.1 本层入口接口

这一层在 _eval_batch(...) 中的入口变量是:

  • train_length
  • test_length
  • horizon
  • stride
  • num_rollings
  • target4batch
  • covariates4batch
  • model

7.2 关键中间变量

  • index_list
  • batch_maker
  • predict_batch_maker
  • all_predicts
  • total_inference_time

7.3 本层输出

  • all_predicts: np.ndarray
  • total_inference_time: float

8. 函数 / 文件关系图

9. 关键代码对应关系

9.1 预测位置生成

python
index_list = self._get_index(train_length, test_length, horizon, stride)
index_list = index_list[:num_rollings]

这一步的语义是:

在测试区间上找出所有 rolling 预测起点,再只保留前 num_rollings 个。

9.2 批量包装

python
batch_maker = RollingForecastEvalBatchMaker(
    target4batch,
    index_list,
    covariates4batch,
)
predict_batch_maker = RollingForecastPredictBatchMaker(batch_maker)

这一步的语义是:

把“序列 + rolling 位置”变成一个可以持续吐出预测 batch 的对象。

9.3 调模型做批量预测

python
while predict_batch_maker.has_more_batches():
    predicts = model.batch_forecast(horizon, predict_batch_maker)
    all_predicts.append(predicts)

这一步的语义是:

每次从 batch maker 拿一批 rolling 请求,送进模型,收集预测结果。

10. 当前例子的具体落地

10.1 index_list

当前参数下:

  • train_length = 13936
  • test_length = 3484
  • horizon = 24
  • stride = 24

所以:

python
index_list = _get_index(13936, 3484, 24, 24)

生成的是从 13936 开始、每次步长 24 的 rolling 起点序列,再截成前 48 个。

10.2 batch 组织

因为:

  • batch_size = 4

所以 predict_batch_maker 会按 4 个 rolling 位置一组,反复生成 batch。

10.3 最终预测结果

每次 model.batch_forecast(...) 返回一批 shape 类似:

  • (real_batch_size, horizon, feature_dim)

最后:

python
all_predicts = np.concatenate(all_predicts, axis=0)

得到的是:

  • 所有 rolling 窗口的预测结果总表

11. 当前层最重要的认知

这一层最重要的认知是:

5C 不是直接“拿 test_data 一次性预测”,而是把测试区间拆成很多 rolling 位置,再通过 batch maker 一批一批送进 batch_forecast(...)

12. 下一层入口

如果继续 DFS 下去,这一层最自然的下一入口是:

python
model.batch_forecast(horizon, predict_batch_maker)

也就是后面可以单独细分:

  • deep_forecasting_model_base.py : batch_forecast(...)

对应下层文档:

13. 只留一句

Level 6 的 5C 只看一件事:rolling 预测位置怎样被组织成 batch 请求,再交给 model.batch_forecast(...)

*记录并在线阅读我的笔记*