在一天的预测之前初始化LSTM状态

时间:2019-05-17 08:45:23

标签: python tensorflow keras lstm

我想使用 var emails = ['person1@email.com', 'person2@email.com'] model.updateMany({ email: { $in: emails }, }, { $pull: { tasks: { $elemMatch: { uuid: uuid } } } }) 来预测与季节密切相关的每日时间序列。作为测试数据,使用了不同季节的天数。在对测试数据进行预测之前,我想用前一天的训练数据初始化LSTM状态。

LSTM

如在源代码中标记为model = Sequential() model.add(LSTM(hidden_size, input_shape=(sequence_size, inputs), return_sequences=True) model.add(Dense(units=2) model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy']) for _ in range(100): model.fit(X_train, Y_train, batch_size=batch_size, epochs=1, validation_data=None, shuffle=True) for day in range(num_test_days): #TODO: initialize state of LSTM with the training data of previous day y_pred = model.predict(X_test[day].reshape(1, sequence_size, inputs), batch_size=1) 一样,我想准备#Todo来预测日期,以便使状态适合于预测考试日期。有办法吗?

1 个答案:

答案 0 :(得分:-1)

如果要将先前批次的状态设为当前批次的状态,只需在LSTM层中添加选项stateful=True。还要添加model.reset_states()以避免累积状态,并且仅从前一批中获取状态。

model = Sequential()
model.add(LSTM(hidden_size, input_shape=(sequence_size, inputs), return_sequences=True, stateful=True)
model.add(Dense(units=2)
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

for _ in range(100):
    model.fit(X_train, Y_train, batch_size=batch_size, epochs=1, validation_data=None, shuffle=False)
    for day in range(num_test_days):
        #TODO: initialize state of LSTM with the training data of previous day
        model.reset_states()
        y_pred = model.predict(X_test[day].reshape(1, sequence_size, inputs), batch_size=1)

我还建议使时间序列平稳,进行预测,然后进行相反的运算以获得真实值