You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LSTM递归多步预测结果异常排查:问题根源何在?

LSTM多步预测效果差的问题定位与修正

一、核心数据标签错误

你的df_to_X_y函数中,标签取的是data[i + window_size, 0],对应x列,但你要预测的是z,而data是np.column_stack((x,y,z)),z的索引应为2(从0开始计数)。这直接导致模型训练目标完全偏离,学的是预测x而非z。

修正后的df_to_X_y函数:

def df_to_X_y(data, window_size=9):
    X = []
    y = [] 
    for i in range(len(data) - window_size):  
        row = data[i:i + window_size]         
        X.append(row)                         
        label = data[i + window_size, 2]  # 改为索引2,对应z列
        y.append(label)                       
    return np.array(X), np.array(y)

二、递归预测的输入更新错误

在forecast方法中,你把预测值更新到了输入的第0列(x的位置),但应更新到z所在的第2列,错误的输入更新会导致后续递归预测完全失效。

修正后的forecast方法关键代码:

def forecast(self, initial_input, num_steps):
    predictions = []
    current_input = initial_input.clone()

    for _ in range(num_steps):
        next_output = self.forward(current_input)  
        predictions.append(next_output.unsqueeze(1))
        
        next_input = current_input.clone()
        next_input[:, :-1, :] = current_input[:, 1:, :]
        next_input[:, -1, 2] = next_output.squeeze(1)  # 改为索引2,更新z列
        current_input = next_input

    return torch.cat(predictions, dim=1)

三、其他优化建议

  • 归一化数据:时间序列数据未做归一化会大幅降低模型收敛效率,建议用StandardScaler或MinMaxScaler对输入数据做归一化,预测后再反变换回原尺度。
  • 调整超参数:
    • 学习率0.04过高,建议下调至0.001~0.01区间,避免训练震荡。
    • 3层LSTM易引发过拟合,可先尝试1层或2层,观察训练/测试损失变化。
    • 训练轮数50可能不足,可增加到100~200轮,同时加入早停机制(Early Stopping)防止过拟合。
  • 缓解递归误差累积:多步递归预测本身会累积误差,若x、y的未来值已知,可尝试滚动预测(每一步用真实x、y输入,仅预测z);或直接训练模型输出多时间步的z值,替代递归方式。

内容的提问来源于stack exchange,提问作者NGA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 22:03:11