愉快的学习就从翻译开始吧_Multivariate Time Series Forecasting with LSTMs in Keras_7_Update:Train On Multiple Lag

吐槽,CSDN的标题长度怎么不能再长点呀,小标题都没法写全了,本来还想把前面第一个洗发水的例子每篇都在加个小标题的,看来也没法弄了

Update: Train On Multiple Lag Timesteps Example

更新,在多个滞后时间步上训练的例子

There have been many requests for advice on how to adapt the above example to train the model on multiple previous time steps.

有很多提议关于如何改写上述例子来在多个提前时间步上训练模型

I had tried this and a myriad of other configurations when writing the original post and decided not to include them because they did not lift model skill.

我在写这篇文章的时候曾尝试过这个以及其他许多配置,并决定不包含它们,因为它们没有提升模型技巧。

Nevertheless, I have included this example below as reference template that you could adapt for your own problems.

尽管如此,我已经在下面将这个例子作为参考模板,以适应您自己的问题。

The changes needed to train the model on multiple previous time steps are quite minimal, as follows:

在以前的多个时间步上训练模型所需的改动非常少,如下所示:

First, you must frame the problem suitably when calling series_to_supervised(). We will use 3 hours of data as input. Also note, we no longer explictly drop the columns from all of the other fields at ob(t).

首先,您在调用series_to_supervised()时必须适当地构造问题。 我们将使用3小时的数据作为输入。 还要注意,我们不再明确地删除ob(t)中所有其他字段的列。

扫描二维码关注公众号,回复: 1630741 查看本文章

# specify the number of lag hours
n_hours = 3
n_features = 8
# frame as supervised learning
reframed = series_to_supervised(scaled, n_hours, 1)

Next, we need to be more careful in specifying the column for input and output.

接下来,我们要更仔细的指定输入输出的列

We have 3 * 8 + 8 columns in our framed dataset. We will take 3 * 8 or 24 columns as input for the obs of all features across the previous 3 hours. We will take just the pollution variable as output at the following hour, as follows:

我们在框架数据集中有3 * 8 + 8列。 我们将采用3 * 8或24列作为前3小时观测值的所有特征作为输入。 我们将在下一小时仅输出污染变量作为输出,如下所示:

# split into input and outputs
n_obs = n_hours*n_features
train_X, train_y = train[:, :n_obs], train[:, -n_features]
test_X, test_y = test[:, :n_obs], test[:, -n_features]
print(train_X.shape, len(train_X), train_y.shape)

Next, we can reshape our input data correctly to reflect the time steps and features.

接下来,我们可以正确地重塑我们的输入数据以对应时间步骤和特征。

# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], n_hours, n_features))
test_X = test_X.reshape((test_X.shape[0], n_hours, n_features))

Fitting the model is the same.

拟合模型是一样的

The only other small change is in how to evaluate the model. Specifically, in how we reconstruct the rows with 8 columns suitable for reversing the scaling operation to get the y and yhat back into the original scale so that we can calculate the RMSE.

唯一的另一个小改变是如何评估模型。 具体而言,在我们如何重构具有8列的行适合于反转缩放操作以将y和y返回到原始尺度以便我们可以计算RMSE。

The gist of the change is that we concatenate the y or yhat column with the last 7 features of the test dataset in order to inverse the scaling, as follows:

改变的要点是,我们将y或yhat列与测试数据集的最后7个特征连接起来,以反转缩放,如下所示:

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], n_hours*n_features))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, 7:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:, 0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, 7:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:, 0]

We can tie all of these modifications to the above example together. The complete example of multvariate(multivariate,这么长时间都没人指出来吗?) time series forecasting with multiple lag inputs is listed below:

我们可以将所有这些修改与上面的例子结合在一起。 具有多个滞后输入的多变量时间序列预测的完整示例如下所示:

from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM


# convert series to supervised learning
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
    # put it all together
    agg = concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg


# load dataset
dataset = read_csv('pollution.csv', header=0, index_col=0)
values = dataset.values
# integer encode direction
encoder = LabelEncoder()
values[:, 4] = encoder.fit_transform(values[:, 4])
# ensure all data is float
values = values.astype('float32')
# normalize features
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)
# specify the number of lag hours
n_hours = 3
n_features = 8
# frame as supervised learning
reframed = series_to_supervised(scaled, n_hours, 1)
print(reframed.shape)

# split into train and test sets
values = reframed.values
n_train_hours = 365 * 24
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]
# split into input and outputs
n_obs = n_hours * n_features
train_X, train_y = train[:, :n_obs], train[:, -n_features]
test_X, test_y = test[:, :n_obs], test[:, -n_features]
print(train_X.shape, len(train_X), train_y.shape)
# reshape input to be 3D [samples, timesteps, features]
train_X = train_X.reshape((train_X.shape[0], n_hours, n_features))
test_X = test_X.reshape((test_X.shape[0], n_hours, n_features))
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)

# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=50, batch_size=72, validation_data=(test_X, test_y), verbose=2,
                    shuffle=False)
# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()

# make a prediction
yhat = model.predict(test_X)
test_X = test_X.reshape((test_X.shape[0], n_hours * n_features))
# invert scaling for forecast
inv_yhat = concatenate((yhat, test_X[:, -7:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
inv_yhat = inv_yhat[:, 0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, -7:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:, 0]
# calculate RMSE
rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
print('Test RMSE: %.3f' % rmse)

The model is fit as before in a minute or two.

模型像以前一样在一两分钟内拟合

Epoch 45/50
1s - loss: 0.0143 - val_loss: 0.0154
Epoch 46/50
1s - loss: 0.0143 - val_loss: 0.0148
Epoch 47/50
1s - loss: 0.0143 - val_loss: 0.0152
Epoch 48/50
1s - loss: 0.0143 - val_loss: 0.0151
Epoch 49/50
1s - loss: 0.0143 - val_loss: 0.0152
Epoch 50/50
1s - loss: 0.0144 - val_loss: 0.0149

A plot of train and test loss over the epochs is plotted.

Plot of Loss on the Train and Test Datasets

Plot of Loss on the Train and Test Datasets

Finally, the Test RMSE is printed, not really showing any advantage in skill, at least on this problem.

最后,测试RMSE被打印出来,至少在这个问题上没有真正显示任何技巧上的优势。
Test RMSE: 27.177

I would add that the LSTM does not appear to be suitable for autoregression type problems and that you may be better off exploring an MLP with a large window.

我想补充一点,LSTM似乎不适合自动回归类型问题,并且您可能更适合用大窗口探索MLP。

I hope this example helps you with your own time series forecasting experiments.

我希望这个例子可以帮助你进行自己的时间序列预测实验。




猜你喜欢

转载自blog.csdn.net/dreamscape9999/article/details/80716740