使用LSTM的基本时间序列预测

时间:2019-08-21 12:56:04

标签: keras lstm prediction lstm-stateful

我有一个序列,我想做一个最简单的LSTM来预测序列的其余部分。 意思是我想只使用上一步来预测下一个,然后添加更多步骤。 我也想将预测值用作输入。 因此,我相信我想要的是实现Understanding Keras LSTMs答案中提到的多对多。

我已经阅读了有关stackoverflow主题的其他问题,但仍然无法使其正常工作。在我的代码中,我在这里使用https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/教程和函数create_dataset来创建两个数组,而只需要一步就可以完成。

这是我的代码和我得到的错误。

"Here I'm scaling my data as advised"
scaler = MinMaxScaler(feature_range=(0, 1))
Rot = scaler.fit_transform(Rot)

"I'm creating the model using batch_size=1 but I'm not sure why this is necessary"

batch_size = 1
model = Sequential()
model.add(LSTM(1,batch_input_shape=(batch_size,1,1),stateful=True,return_sequences=True,input_shape=(None,1)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

"I want to use only the previous value for now"
look_back = 1
"as len(Rot) = 41000 I'm taking 36000 for training"
train_size = 36000
X,Y = create_dataset(Rot[:train_size,:],look_back)
X = numpy.reshape(X,(X.shape[0], X.shape[1], 1))
Y = numpy.reshape(Y,(X.shape[0], X.shape[1], 1))

And now I train my network as advised by @Daniel Möller. 

epochs = 10

for epoch in range(epochs):
    model.reset_states()
    model.train_on_batch(X,Y)

" And I get this error "
" PartialTensorShape: Incompatible shapes during merge: [35998,1] vs. [1,1]
     [[{{node lstm_11/TensorArrayStack/TensorArrayGatherV3}}]]."

您知道我为什么会出现上述错误吗?

1 个答案:

答案 0 :(得分:0)

在此LSTM网络中,batch_size = 1,因为它是有状态的。当stateful = True时,train_set大小和test_set大小除以batch_size的模数应为零。

batch_input_shape =(batch_size,1,1)已经定义,那么为什么再次输入input_shape =(None,1)

当return_sequences = True时,另一个LSTM跟随现有LSTM层。但是这里不是。