使用LSTM进行时间序列预测--批量训练和实时预测。

问题描述 投票:3回答:1

我正在研究一个路径预测问题,我要提前一步预测路径(纬度、经度)。我有近1500条的路径数据。"事件",我用它来训练LSTM模型。训练时,由于我知道路径是先验的,我将时间序列移动一步,并将其作为目标向量。例如:事件1事件1:

拉脱维亚 (t), (t) --> 纬度 (t+1), (t+1)

纬度 (t+1), (t+1) -> 纬度 (t+2), (t+2)

然而,对于测试来说,由于路径不是先验已知的,我把训练好的LSTM模型,每次预测一个时间步,并把预测值作为下一个时间步的输入。下面是我的代码片段-----------------------------------------------。

# Extract only the Lat, Lon values to arrays
train_full = train_df[['LatNor','LonNor','LatLag1Nor','LonLag1Nor']].values
test_full = test_df[['LatNor','LonNor','LatLag1Nor','LonLag1Nor']].values
print('train_full.shape = ', train_full.shape)
print('test_full.shape = ', test_full.shape)

# Separate the Inputs and Targets
x_train_full = train_full[:,0:2]
y_train_full = train_full[:,2:4]
x_test_full = test_full[:,0:2]
y_test_full = test_full[:,2:4]


# Defining the LSTM model
model = Sequential()
model.add(LSTM(40,input_shape=(None,2), return_sequences=True))
model.add(Dropout(0.1))
model.add(LSTM(20,input_shape=(None,2), return_sequences=True))
model.add(Dropout(0.1))
model.add(Dense(2))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])
model.summary()

epochs = 50
for i in range(epochs):
    print ('Running Epoch No: ', i)
    for stormID, data in train_df.groupby('EventID'):
        train = data[['LatNor','LonNor','LatLag1Nor','LonLag1Nor']]
        train = train.values
        x_train = np.expand_dims(train[:,0:2], axis=0)
        y_train = np.expand_dims(train[:,2:4], axis=0)
        #print (x_train.shape, y_train.shape)
        model.train_on_batch(x_train,y_train)
    model.reset_states()

print('Model training done.....')

#Use the optimized weights to estimate target values for training data
train_pred = new_model.predict_on_batch(np.expand_dims(train_df[['LatNor','LonNor']].values, axis=0))
train_pred_val = x_scaler.inverse_transform(train_pred[0])

该模型的训练效果良好(见下图)。

请在此输入图片描述

请在此输入图片描述

当我使用训练好的模型,并做一个 预测批次 在测试数据上,它的效果很好。但是,在现实中,我们不会提前知道时间序列。所以,当我每次对测试集预测一个实例,并将其作为下一个时间步骤的输入时,它的工作并不顺利。我怀疑我遗漏了什么,每当我对网络进行预测调用时,都会改变训练网络的状态权重。

    x_values = TestDF[['LatNor','LonNor']].values
    x_values_scaled = x_values
    start = x_values_scaled[0,:]
    startX = start.reshape(1,1,2)
    Results = np.empty(())
    Results = x_scaler.inverse_transform(startX[0])
    for i in range(x_values.shape[0]):
        nextLoc = model.predict(startX)
        nextLoc_rescaled = x_scaler.inverse_transform(nextLoc[0])
        Results = np.vstack((Results,nextLoc_rescaled))
        startX = nextLoc

有什么想法或建议吗?

python tensorflow keras time-series lstm
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.