该代码采用滑动窗口机制,步长为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)。举例说明:
- 第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的迭代次数决定,即滑动窗口总数。因此,该代码实现的是典型的单步长滑动窗口采样,对于短时序预测任务而言,设计较为合理。
侠游戏发布此文仅为了传递信息,不代表侠游戏网站认同其观点或证实其描述