如何判断代码是否使用了滑动窗口及确定其步长

作者:袖梨 2026-06-25

该代码确实采用了滑动窗口机制,窗口步长(stride)为1帧,即相邻样本的历史序列起始位置相差1个时间步,从而生成高度重叠的时序片段,适用于convlstm等时序建模任务。

该代码确实采用了滑动窗口机制,窗口步长(stride)为1帧,即相邻样本的历史序列起始位置相差1个时间步,从而生成高度重叠的时序片段,适用于convlstm等时序建模任务。

在 generate_dataset 函数中,核心逻辑通过嵌套循环构建输入-输出对,明确体现了滑动窗口的设计思想:

def generate_dataset(data, date, n_samples, past_history, future_target):    # ... 数据标准化与reshape ...    date_data_n_frames = []    for i in range(n_samples - past_history - future_target - future_target):  # 注意:此处减法存在冗余(见后文说明)        # 构建历史窗口 [i, i+1, ..., i+past_history-1]        for t in range(past_history):            hist_data = data_4d[i + t, :, :, :]            # ...        # 构建未来目标窗口 [i+future_target, i+future_target+1, ..., i+2*future_target-1]        for f in range(future_target):            next_data = data_4d[i + (f + future_target), :, :, :]            # ...

关键在于外层循环变量 i 的取值范围:
range(n_samples - past_history - future_target - future_target)
该表达式实际等价于 range(n_samples - past_history - 2 * future_target)。虽然命名中 future_target 被重复减去两次略显冗余(可能是笔误),但不影响滑动本质——i 每次递增 1,导致每个新样本的历史段起始索引比前一个样本提前1帧。

✅ 因此,滑动步长(stride)为 1,而非 past_history(24)或 future_target(24)。这意味着:

  • 若 past_history = 24,则第0个样本使用时间步 [0, 1, ..., 23] 作为输入;
  • 第1个样本使用 [1, 2, ..., 24];
  • 第2个样本使用 [2, 3, ..., 25];
  • ……依此类推。

这种单位步长滑动显著增加了训练样本量,并保留了细粒度时序依赖关系,是深度时序模型(如ConvLSTM)的标准预处理方式。

⚠️ 注意事项:

  • 外层循环上限存在潜在逻辑瑕疵:n_samples - past_history - 2 * future_target 可能过于保守。标准滑动窗口通常只需 n_samples - past_history - future_target(确保历史段 + 预测段不越界)。当前写法虽安全但会损失部分合法样本。
  • 所有内部循环(for t in range(past_history) 和 for f in range(future_target))均以步长1遍历,无跳帧行为,进一步确认 stride=1。
  • 最终输出的 hist_data_5d 形状为 (N, past_history, 32, 32, 1),其中 N 即滑动窗口总数,由 i 的迭代次数决定。

综上,该代码实现了典型的单步长滑动窗口采样,是面向短时序预测任务的合理设计。

相关文章

精彩推荐