该代码确实采用了滑动窗口机制,窗口步长(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)。这意味着:
这种单位步长滑动显著增加了训练样本量,并保留了细粒度时序依赖关系,是深度时序模型(如ConvLSTM)的标准预处理方式。
⚠️ 注意事项:
综上,该代码实现了典型的单步长滑动窗口采样,是面向短时序预测任务的合理设计。