【发布时间】:2020-11-05 02:15:46
【问题描述】:
我正在尝试使用具有不同维度的 X 和 y 的 tensorflow 和 keras 进行时间序列预测:
X.shape = (5000, 12)
y.shape = (5000, 3, 12)
当我执行以下操作时
n_input = 7
generator = TimeseriesGenerator(X, y, length=n_input, batch_size=1)
for i in range(5):
x_, y_ = generator[i]
print(x_.shape)
print(y_.shape)
我得到想要的输出
(1, 7, 12)
(1, 3, 12)
(1, 7, 12)
(1, 3, 12)
...
这是因为我的数据是气象数据,我有 5000 天,用于数组 X 中的训练,我使用 7 天的滑动窗口,每天包含 12 个特征(气压、温度、湿度 a.o.)。而在目标数组y我有3天的滑动窗口,试图预测接下来的3天到7天的每个窗口。
但是当我尝试拟合模型时,由于X 和y 数组的形状不匹配而出现错误:
model = Sequential()
model.add(LSTM(4, input_shape=(None, 12)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
history = model.fit_generator(generator, epochs=3).history
ValueError: A target array with shape (1, 3, 12) was passed for an output of shape (None, 1) while using as loss `mean_squared_error`. This loss expects targets to have the same shape as the output.
那么有没有办法针对尺寸不匹配调整架构?或者有没有办法重塑X 和y 使它们与这个架构一起工作?我尝试了后期将X 重塑为(5000, 7, 12),但这也产生了维度错误。天呐
【问题讨论】:
标签: python tensorflow keras time-series regression