【问题标题】:Mutli Step Forecast LSTM model多步预测 LSTM 模型
【发布时间】:2019-02-23 17:13:51
【问题描述】:

我正在尝试在 Keras 中实现多步预测 LSTM 模型。数据的形状是这样的:

X : (5831, 48, 1)
y : (5831, 1, 12)

我尝试使用的模型是:

power_in = Input(shape=(X.shape[1], X.shape[2]))


power_lstm = LSTM(50, recurrent_dropout=0.4128,
                  dropout=0.412563, kernel_initializer=power_lstm_init, return_sequences=True)(power_in)

main_out = TimeDistributed(Dense(12, kernel_initializer=power_lstm_init))(power_lstm)

在尝试像这样训练模型时:

hist = forecaster.fit([X], y, epochs=325, batch_size=16, validation_data=([X_valid], y_valid), verbose=1, shuffle=False)

我收到以下错误:

ValueError: Error when checking target: expected time_distributed_16 to have shape (48, 12) but got array with shape (1, 12)

如何解决这个问题?

【问题讨论】:

  • 你必须在y中提供48时间步长,你只有1个。
  • 是的。我没有和TimeDistributed 合作过很多。必须如何准备数据。?我拥有的数据就像 t-48, t-47, t-46, ..... , t-1 作为过去的数据和 t+1, t+2, ......, t+12 作为我要预测的值

标签: python machine-learning keras time-series lstm


【解决方案1】:

根据您的评论:

[The] 我拥有的数据就像 t-48, t-47, t-46, ..... , t-1 作为过去的数据和 t+1, t+2, ......, t+12 作为我要预测的值

您可能根本不需要使用TimeDistributed 层: 首先,只需删除 LSTM 层的resturn_sequences=True 参数。完成后,LSTM 层会将过去的输入时间序列编码为形状为(50,) 的向量。现在您可以直接将其馈送到具有 12 个单位的 Dense 层:

# make sure the labels have are in shape (num_samples, 12)
y = np.reshape(y, (-1, 12))

power_in = Input(shape=(X.shape[1:],))
power_lstm = LSTM(50, recurrent_dropout=0.4128,
                  dropout=0.412563,
                  kernel_initializer=power_lstm_init)(power_in)

main_out = Dense(12, kernel_initializer=power_lstm_init)(power_lstm)

或者,如果您想使用TimeDistributed 层并考虑到输出本身就是一个序列,我们可以通过在 Dense 层之前使用另一个 LSTM 层在我们的模型中显式地强制执行这种时间依赖性(添加在第一个 LSTM 层之后的 RepeatVector 层,使其输出长度为 12 的时序序列,即与输出时序长度相同):

# make sure the labels have are in shape (num_samples, 12, 1)
y = np.reshape(y, (-1, 12, 1))

power_in = Input(shape=(48,1))
power_lstm = LSTM(50, recurrent_dropout=0.4128,
                  dropout=0.412563,
                  kernel_initializer=power_lstm_init)(power_in)

rep = RepeatVector(12)(power_lstm)
out_lstm = LSTM(32, return_sequences=True)(rep)
main_out = TimeDistributed(Dense(1))(out_lstm)

model = Model(power_in, main_out)
model.summary()

模型总结:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         (None, 48, 1)             0         
_________________________________________________________________
lstm_3 (LSTM)                (None, 50)                10400     
_________________________________________________________________
repeat_vector_2 (RepeatVecto (None, 12, 50)            0         
_________________________________________________________________
lstm_4 (LSTM)                (None, 12, 32)            10624     
_________________________________________________________________
time_distributed_1 (TimeDist (None, 12, 1)             33        
=================================================================
Total params: 21,057
Trainable params: 21,057
Non-trainable params: 0
_________________________________________________________________

当然,在这两个模型中,您可能需要调整超参数(例如 LSTM 层数、LSTM 层的维度等),以便能够准确地比较它们并获得良好的结果。


旁注:实际上,在您的场景中,您根本不需要使用TimeDistributed 层,因为(当前)Dense layer is applied on the last axis。因此,TimeDistributed(Dense(...))Dense(...) 是等价的。

【讨论】:

  • 很好的答案。你能解释一下我如何为多变量系列扩展这个解决方案吗?
  • 我试图预测给定最后 48 个时间步的系列的下 12 个时间步。我不应该和TimeDistributed 一起去吗?
  • @SreeramTP 对于多变量情况,在第二个模型中,只需将最后一个 Dense 层的单元数更改为时间序列的特征数,即如果输出形状为(?, 12, num_feats),那么我们将拥有TimeDistributed(Dense(num_feats))(out_lstm)。让我告诉你一些事情:你根本不需要TimeDistributed 层,因为(当前)[在最后一个轴上应用密集层],所以它是多余的。只需使用Dense(num_feats)(out_lstm)
  • 感谢您的信息。那么TimeDistributed用在哪些情况下呢?
  • 多变量是指多变量特征,只预测一个变量
猜你喜欢
  • 2021-12-15
  • 2022-11-22
  • 2022-07-28
  • 2021-09-08
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多