LSTM与Keras进行小批量培训和在线测试

时间:2016-07-11 18:22:27

标签: keras lstm

我想在Keras中实现LSTM用于流时间序列预测 - 即,在线运行,一次获得一个数据点。 This is explained well here,但正如人们所假设的那样,在线LSTM的培训时间可能非常慢。我想在迷你批次上训练我的网络,并在线测试(运行预测)。在Keras这样做的最佳方式是什么?

例如,小批量可以是在连续时间步骤发生的1000个数据值([33, 34, 42, 33, 32, 33, 36, ... 24, 23])的序列。为了训练网络,我已经指定了一个形状为X的数组(900, 100, 1),其中有900个长度为100的序列,以及一个形状为y的数组(900, 1)。如,

X[0] = [[33], [34], [42], [33], ...]]
X[1] = [[34], [42], [33], [32], ...]]
...
X[999] = [..., [24]]

y[999] = [23]

因此,对于每个序列X[i],都有一个对应的y[i]代表时间序列中的下一个值 - 我们想要预测的内容。

在测试中我想预测下一个数据值1000到1999.我通过为1000到1999的每个步骤提供一个形状(1, 100, 1)的数组来做到这一点,模型试图在下一步预测值步骤

这是我的问题的推荐方法和设置吗?启用有状态可能是纯粹在线实现的方式,但在Keras中,这需要在培训和测试中保持一致batch_input_shape,这对于我在小批量培训和在线测试的意图不利。或者我有办法做到这一点吗?

更新:尝试将网络实施为推荐的@nemo

我在博客文章"Time Series Prediction with LSTM Recurrent Neural Networks in Python with Keras"的示例网络上运行自己的数据集,然后尝试将预测阶段实现为有状态网络。

模型构建和培训对于两者都是相同的:

# Create and fit the LSTM network
numberOfEpochs = 10
look_back = 30
model = Sequential()
model.add(LSTM(4, input_dim=1, input_length=look_back))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=numberOfEpochs, batch_size=1, verbose=2)
# trainX.shape = (6883, 30, 1)
# trainY.shape = (6883,)
# testX.shape = (3375, 30, 1)
# testY.shape = (3375,)

批量预测完成:

trainPredict = model.predict(trainX, batch_size=batch_size)
testPredict = model.predict(testX, batch_size=batch_size)

要尝试有状态的预测阶段,我运行与以前相同的模型设置和培训,但接下来是:

w = model.get_weights()
batch_size = 1
model = Sequential()
model.add(LSTM(4, batch_input_shape=(batch_size, look_back, 1), stateful=True))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
trainPredictions, testPredictions = [], []
for trainSample in trainX:
    trainPredictions.append(model.predict(trainSample.reshape((1,look_back,1)), batch_size=batch_size))
trainPredict = numpy.concatenate(trainPredictions).ravel()
for testSample in testX:
    testPredictions.append(model.predict(testSample.reshape((1,look_back,1)), batch_size=batch_size))
testPredict = numpy.concatenate(testPredictions).ravel()

为了检查结果,下面的图显示了蓝色的实际(标准化)数据,绿色训练集的预测以及红色测试集的预测。

Results for batch prediction

Results from stateful prediction

第一个数字来自使用批量预测,第二个数字来自有状态。我有什么不正确的想法吗?

1 个答案:

答案 0 :(得分:9)

如果我理解你,你会问你是否可以在训练后启用有状态。这应该是可能的,是的。例如:

net = Dense(1)(SimpleRNN(stateful=False)(input))
model = Model(input=input, output=net)

model.fit(...)

w = model.get_weights()
net = Dense(1)(SimpleRNN(stateful=True)(input))
model = Model(input=input, output=net)
model.set_weights(w)

之后,您可以以有状态的方式进行预测。

相关问题