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

TensorFlow时间序列预测lambda_input输入错误排查与预测修正

问题解答

一、预测思路是否正确?

你的思路完全合理:基于15分钟粒度的时序能耗数据,用7天历史(672个时间步)预测未来24小时(96个时间步),借助TensorFlow官方的WindowGenerator类处理时序窗口、训练模型,这是时间序列预测的标准可行流程,核心问题出在模型加载后的输入格式匹配环节。

二、"ValueError: Missing data for input 'lambda_input'"错误原因

这个错误的核心是加载后的模型期望的输入结构/名称,和你传入的Dataset不匹配:

  • 训练时WindowGenerator内部可能通过lambda层处理输入数据,模型保存后,输入节点的名称被自动设为lambda_input;
  • 你手动将DataFrame转为字典格式的Dataset,字典的键名和模型期望的输入名称(lambda_input)不一致,或者字典结构和训练时的单张量输入结构不匹配,导致模型找不到对应的输入数据。

三、代码修正及绘图实现

1. 修正输入格式问题

方法1:复用WindowGenerator处理预测数据(推荐,避免格式不一致)

直接用和训练时参数完全一致的WindowGenerator来处理预测用的历史数据,确保输入格式和训练时完全匹配:

import tensorflow as tf
import pandas as pd

# 加载训练好的模型
model = tf.keras.models.load_model('你的模型保存路径.h5')

# 初始化和训练时参数相同的WindowGenerator
# input_width=7天*24小时*4个15分钟=672;label_width=24*4=96;shift=96
pred_window = WindowGenerator(
    input_width=672,
    label_width=96,
    shift=96,
    train_df=pred_history_df,  # 传入2023-05-01至05-07的AE历史数据
    val_df=pd.DataFrame(),  # 验证集、测试集无需使用,传空DataFrame
    test_df=pd.DataFrame()
)

# 生成符合模型要求的Dataset
pred_dataset = pred_window.make_dataset(pred_history_df).batch(1)

# 执行预测
predictions = model.predict(pred_dataset)

方法2:手动调整输入张量格式

如果不想复用WindowGenerator,可直接手动将预处理后的DataFrame转成模型期望的张量形状:

# 假设pred_history_df是预处理后的7天AE数据,仅包含'AE'列,共672行
input_tensor = pred_history_df['AE'].values.reshape(1, 672, 1)
# 形状说明:(批量大小, 时间步长度, 特征数),批量设为1,对应单样本预测

predictions = model.predict(input_tensor)

2. 真实值与预测值绘图对比

先获取对应预测时段(2023-05-08 00:00至23:45)的真实能耗数据,再将预测值和真实值对齐后绘图:

import matplotlib.pyplot as plt

# 准备数据
# 假设true_df是2023-05-08的AE数据,包含时间索引和'AE'列
true_values = true_df['AE'].values
pred_values = predictions.flatten()  # 将预测结果从(1,96,1)转成一维数组
time_index = true_df.index

# 绘图
plt.figure(figsize=(14, 7))
plt.plot(time_index, true_values, color='#1f77b4', label='真实能耗')
plt.plot(time_index, pred_values, color='#ff7f0e', linestyle='--', label='预测能耗')
plt.xlabel('时间')
plt.ylabel('主动能耗(AE)')
plt.title('2023年5月8日主动能耗预测 vs 真实值对比')
plt.legend()
plt.xticks(rotation=45)
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 19:17:41