Keras中的有状态LSTM:是否可以进行拟合,评估和预测? [英] Stateful LSTM in Keras: reset with fit, evaluate, and predict?

查看:459
本文介绍了Keras中的有状态LSTM:是否可以进行拟合,评估和预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展一下何时重置状态的问题.

I'd like to expand on this question of when to reset states.

状态LSTM:何时重置状态?

假设我像这样训练一个有状态模型:

Suppose I train a stateful model as such:

for i in range(epochs):
    model.fit(X_train, y_train, epochs=1, batch_size=1, shuffle=False)
    model.reset_states()

我的训练和测试集来自一个时间序列数据集,测试集紧随训练集之后.

My training and test sets are from one time-series data set, with the test set following immediately after the training set.

接下来,我要评估测试集并获得一组预测.

Next, I want to evaluate the test set and get an array of the predictions.

score = model.evaluate(X_test, y_test, batch_size=1, verbose=True)

prediction = model.predict(X_test, batch_size=1)

我觉得在训练循环结束时重置模型状态至少会导致评估或预测步骤错误,至少是在集合开始时.是这样吗?如果数据继续按顺序进入测试集中,是否应该不重置最后一个时期的状态?

I feel as though resetting the model state at the end of the training loop will cause the evaluate or predict steps to be wrong, at least at the beginning of the set. Is that so? Should I not reset the state for the last epoch if the data continues sequentially into the test set?

此外,在对测试集进行评估之后,是否需要将模型的状态恢复到训练集结束时的状态,然后才能尝试进行预测?我应该复制模型吗?保存并重新加载吗?

Also, after I evaluate on the test set, do I need to restore the model's state to what it was at the end of the training set before I try to predict? Should I copy the model? Save and reload it?

推荐答案

实际上,如果在评估测试集之前重置状态,则将假定测试序列是一个全新的序列.它将从头开始.如果整个序列的总体行为没有随时间变化,则错误可能不会太大.但是我不会冒险.

Indeed, if you reset the states before evaluating the test set, it will assume that the test sequence is a whole new sequence. It will start it from the beginning. If the general behavior of this entire sequence is not changing with time, maybe the error will not be too significant. But I'd not risk it.

如果测试序列继续训练序列,则它应该以正确的状态开始以获得最佳结果.

If the test sequence is continuing the training sequence, then it should start with proper states for best results.

但是我想你应该这样做:

But I'd say you should do this:

  • 重置状态(这些状态受在最后一个时期开始时尚未受训练的权重的影响)
  • 评估火车顺序(以创建新的最新状态)
  • 评估测试顺序

然后这个:

  • 重置状态(评估需要更改状态)
  • 预测火车顺序(再次创建状态)
  • 预测测试顺序

未回答:我不知道evaluate方法是否会将状态恢复到以前的状态.但是我确实相信不会.您可能需要评估足够长以填充内存的序列,然后必须分批评估.

Not answered: I don't know if the evaluate method will bring the states back to where they were before. But I do believe it won't. You may need to evaluate sequences that are long enough to fill your memory, and then you'd have to evaluate in batches.

题外话:链接问题中的误解:

在喀拉拉邦,样本序列.循环图层的批次尺寸为:

In keras, samples are sequences. The dimensions in a batch for recurrent layers are:

  • (sequences, timeSteps, features),其中序列数,样本数和批次大小完全相同. (请查看文档,以确保第二维顺序是步骤": https://keras.io /layers/recurrent/)
  • (sequences, timeSteps, features), where the number of sequences, the number of samples and the batch size are exactly the same thing. (Check the documentation to confirm that the second dimension is "steps" in a sequence: https://keras.io/layers/recurrent/)

这篇关于Keras中的有状态LSTM:是否可以进行拟合,评估和预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆