使用LSTM进行时间序列预测-批处理列车和实时预测

时间:2018-08-28 13:53:14

标签: python tensorflow keras time-series lstm

我正在研究一个路径预测问题,在这个问题上,我预测了向前一步的路径(纬度,经度)。我拥有近1500个“事件” 的路径数据,这些数据用于训练LSTM模型。为了进行训练,由于我先验地知道了路径,因此将时间序列移了一步,并将其用作目标向量。例如: 事件1

Lat (t), Lon (t)-> Lat (t + 1), Lon (t + 1)

拉特(t + 1),拉特(t + 1)-> 拉特(t + 2),(t + 2)

但是,由于路径是先验未知的,因此为了进行测试,我采用了经过训练的LSTM模型,一次预测一个时间步,然后将预测值作为下一时间步的输入。以下是我的代码片段-

  private static bool IsInside(GPS Point1, GPS Point2)
        {
            GPS VectorV = new GPS()
            {
                Longitude = Point1.Longitude - Point2.Longitude,
                Latitude = Point1.Latitude - Point2.Latitude
            };

            GPS VectorW = new GPS()
            {
                Longitude = -1 / VectorV.Longitude,
                Latitude = -1 / VectorV.Latitude
            };

            double W = Math.Sqrt(Convert.ToDouble(VectorW.Latitude * VectorW.Latitude) + Convert.ToDouble(VectorW.Longitude * VectorW.Longitude));

            GPS NewVector = new GPS()
            {
                Longitude = Convert.ToDecimal(Convert.ToDouble(VectorW.Longitude) / W),
                Latitude = Convert.ToDecimal(Convert.ToDouble(VectorW.Latitude) / W),
            };

            decimal D = 5;

            GPS DisplacmentVector = new GPS()
            {
                Longitude = (D / 2) * NewVector.Longitude,
                Latitude = (D / 2) * NewVector.Latitude
            };

            GPS BPoint1 = new GPS() { Longitude = Point1.Longitude + DisplacmentVector.Longitude, Latitude = Point1.Latitude + DisplacmentVector.Latitude };
            GPS BPoint2 = new GPS() { Longitude = Point1.Longitude - DisplacmentVector.Longitude, Latitude = Point1.Latitude - DisplacmentVector.Latitude };
            GPS BPoint3 = new GPS() { Longitude = Point2.Longitude + DisplacmentVector.Longitude, Latitude = Point2.Latitude + DisplacmentVector.Latitude };
            GPS BPoint4 = new GPS() { Longitude = Point2.Longitude - DisplacmentVector.Longitude, Latitude = Point2.Latitude - DisplacmentVector.Latitude };
}

 public partial class GPS
    {
        public decimal Longitude { get; set; }
        public decimal Latitude { get; set; }

        public GPS() { }

        public GPS(decimal longitude, decimal latitude) {
            Longitude = longitude;
            Latitude = latitude;
        }
    }

模型训练得很好(请参见下图)

enter image description here

enter image description here

当我使用经过训练的模型并对测试数据进行 predict_on_batch 时,它会很好用。但是,实际上我们不会提前知道时间序列。因此,当我一次为测试集预测一个实例,并将其用作下一个时间步的输入时,它就无法正常工作。我怀疑我遗漏了一些东西,并且每当我预测对网络的呼叫时,都会更改受过训练的网络的状态/权重。

# 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])

有什么想法或建议吗?

0 个答案:

没有答案
相关问题