如何为具有外部功能的时间序列多步水平构建LSTM的输入数据? [英] How to construct input data to LSTM for time series multi-step horizon with external features?

查看:129
本文介绍了如何为具有外部功能的时间序列多步水平构建LSTM的输入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LSTM进行商店销售预测.这是我的原始数据的样子:

|     Date   | StoreID | Sales | Temperature |  Open   | StoreType |
|------------|---------|-------|-------------|---------|-----------|
| 01/01/2016 |   1     |   0   |      36     |    0    |     1     |
| 01/02/2016 |   1     | 10100 |      42     |    1    |     1     |
| ...
| 12/31/2016 |   1     | 14300 |      39     |    1    |     1     |
| 01/01/2016 |   2     | 25000 |      46     |    1    |     3     |
| 01/02/2016 |   2     | 23700 |      43     |    1    |     3     |
| ...
| 12/31/2016 |   2     | 20600 |      37     |    1    |     3     |
| ...
| 12/31/2016 |   10    | 19800 |      52     |    1    |     2     |

我需要预测未来10天的销售情况.在此示例中,我将需要预测从01-01-2017到01-10-2017的商店销售额.我知道如何使用其他时间序列模型或回归模型来解决此问题,但是我想知道RNN-LSTM是否是一个不错的选择.

我首先采用 storeID = 1数据 来测试LSTM.如果我的数据 仅具有日期和销售 .我将以这种方式构造trainX和trainY(如果我错了,请纠正我):

Window = 20
Horizon = 10

|         trainX                  |          trainY              |
| [Yt-10, Yt-11, Yt-12,...,Yt-29] | [Yt, Yt-1, Yt-2,...,Yt-9]    |
| [Yt-11, Yt-12, Yt-13,...,Yt-30] | [Yt-2, Yt-3, Yt-4,...,Yt-10] |
| [Yt-12, Yt-13, Yt-14,...,Yt-31] | [Yt-3, Yt-4, Yt-5,...,Yt-11] |
...

重塑两个之后

trainX.shape
(300, 1, 20)
trainY.shape
(300, 10)

问题1::在这种情况下,[样本,时间步长,特征] = [300,1,20].这是正确的吗?还是应该将样本构造为[300,20,1]?

问题2:我确实想在原始数据中使用其他信息,例如Temperature,StoreType等.如何为LSTM构造输入数据?

问题3:到目前为止,我们仅讨论了1家商店预测,如果要对所有商店进行预测,那么我应该如何构建输入数据?

目前,我正在从此处,但似乎不足以涵盖我所遇到的情况.非常感谢您的帮助!

解决方案

我最近正在解决类似的问题.就您而言:

  1. 输入的形状应为(300, 20, 1)-因为您具有具有1功能的长度为20的时间序列.

  2. 您可以这样做:

    sequential_input = Input(shape=(20, 1))
    feature_input = Input(shape=(feature_nb,))
    lstm_layer = LSTM(lstm_units_1st_layer, return_sequences=True)(sequential_input)
    lstm_layer = LSTM(lstm_units_2nd_layer, return_sequences=True)(lstm_layer)
    ...
    lstm_layer = LSTM(lstm_units_nth_layer, return_sequences=False)(lstm_layer)
    merged = merge([lstm_layer, feature_input], mode='concat')
    blend = Dense(blending_units_1st_layer, activation='relu')(merged)
    blend = Dense(blending_units_2nd_layer, activation='relu')(blend)
    ...
    output = Dense(10)(blend)
    

  3. 这是最难的部分.我建议您通过将多个商店作为一个特征向量馈入网络来预测多个商店.您可能只需要跳过这一部分,并尝试使用一种模型来预测不同的店铺,或者使用例如某种图形模型或矩阵上的PCA,其中行是日销售量.

更新:

为了处理多个顺序功能,您可以执行以下操作:

    sequential_input = Input(shape=(20, nb_of_sequental_features))
    feature_input = Input(shape=(feature_nb,))
    lstm_layer = LSTM(lstm_units_1st_layer, return_sequences=True)(sequential_input)
    lstm_layer = LSTM(lstm_units_2nd_layer, return_sequences=True)(lstm_layer)
    ...
    lstm_layer = LSTM(lstm_units_nth_layer, return_sequences=False)(lstm_layer)
    merged = merge([lstm_layer, feature_input], mode='concat')
    blend = Dense(blending_units_1st_layer, activation='relu')(merged)
    blend = Dense(blending_units_2nd_layer, activation='relu')(blend)
    ...
    output = Dense(10)(blend)
    model = Model(input=[sequential_input, feature_input], output=output])

在这种情况下,您的输入应包含两个表的列表:[sequential_data, features]其中sequential_data.shape = (nb_of_examples, timesteps, sequential_features)features.shape = (nb_of_examples, feature_nb).因此,salestemperature应该存储在sequential_features中的sequential_featuresstore_type中.

I'm trying to use LSTM to do store sales forecast. Here is how my raw data look like:

|     Date   | StoreID | Sales | Temperature |  Open   | StoreType |
|------------|---------|-------|-------------|---------|-----------|
| 01/01/2016 |   1     |   0   |      36     |    0    |     1     |
| 01/02/2016 |   1     | 10100 |      42     |    1    |     1     |
| ...
| 12/31/2016 |   1     | 14300 |      39     |    1    |     1     |
| 01/01/2016 |   2     | 25000 |      46     |    1    |     3     |
| 01/02/2016 |   2     | 23700 |      43     |    1    |     3     |
| ...
| 12/31/2016 |   2     | 20600 |      37     |    1    |     3     |
| ...
| 12/31/2016 |   10    | 19800 |      52     |    1    |     2     |

I need to forecast for the next 10 days' sales. In this example, I will need to forecast the store sales from 01-01-2017 to 01-10-2017. I know how to use other time series model or regression model to solve this problem, but I want to know if RNN-LSTM is a good candidate for it.

I started by taking only storeID=1 data to test the LSTM. If my data only have Date and Sales. I will construct my trainX and trainY in this way (please correct me if I'm wrong):

Window = 20
Horizon = 10

|         trainX                  |          trainY              |
| [Yt-10, Yt-11, Yt-12,...,Yt-29] | [Yt, Yt-1, Yt-2,...,Yt-9]    |
| [Yt-11, Yt-12, Yt-13,...,Yt-30] | [Yt-2, Yt-3, Yt-4,...,Yt-10] |
| [Yt-12, Yt-13, Yt-14,...,Yt-31] | [Yt-3, Yt-4, Yt-5,...,Yt-11] |
...

After reshaping the two

trainX.shape
(300, 1, 20)
trainY.shape
(300, 10)

Question1: In this case, [samples, time steps, features] = [300, 1, 20]. Is this right? Or should I construct the sample as [300, 20, 1] ?

Question2: I do want to use other information in the raw data like Temperature, StoreType, etc. How should I construct my input data for LSTM?

Question3: So far we only discussed 1 store forecast, if I want to forecast for all the stores, how should I construct my input data then?

Currently I'm flowing examples from here, but it seems not sufficient to cover the scenario that I have. I really appreciate for your help!

解决方案

I was recently solving similiar problem. In your case:

  1. Input should have shape (300, 20, 1) - because you have time sequences of length 20 with 1 feature.

  2. You may do it like this:

    sequential_input = Input(shape=(20, 1))
    feature_input = Input(shape=(feature_nb,))
    lstm_layer = LSTM(lstm_units_1st_layer, return_sequences=True)(sequential_input)
    lstm_layer = LSTM(lstm_units_2nd_layer, return_sequences=True)(lstm_layer)
    ...
    lstm_layer = LSTM(lstm_units_nth_layer, return_sequences=False)(lstm_layer)
    merged = merge([lstm_layer, feature_input], mode='concat')
    blend = Dense(blending_units_1st_layer, activation='relu')(merged)
    blend = Dense(blending_units_2nd_layer, activation='relu')(blend)
    ...
    output = Dense(10)(blend)
    

  3. This is the hardest part. I do not advise you to predict multiple shops by feeding them to a network as one feature vector. You may under simply skip this part and try to predict different shops using one model or postprocess output using e.g. some kind of graphical models or PCA on matrix where rows are day sales.

UPDATE:

In order to deal with multiple sequential features you could do the following thing:

    sequential_input = Input(shape=(20, nb_of_sequental_features))
    feature_input = Input(shape=(feature_nb,))
    lstm_layer = LSTM(lstm_units_1st_layer, return_sequences=True)(sequential_input)
    lstm_layer = LSTM(lstm_units_2nd_layer, return_sequences=True)(lstm_layer)
    ...
    lstm_layer = LSTM(lstm_units_nth_layer, return_sequences=False)(lstm_layer)
    merged = merge([lstm_layer, feature_input], mode='concat')
    blend = Dense(blending_units_1st_layer, activation='relu')(merged)
    blend = Dense(blending_units_2nd_layer, activation='relu')(blend)
    ...
    output = Dense(10)(blend)
    model = Model(input=[sequential_input, feature_input], output=output])

In this case your input should consist of list of two tables: [sequential_data, features] where sequential_data.shape = (nb_of_examples, timesteps, sequential_features) and features.shape = (nb_of_examples, feature_nb). So sales or temperature should be stored in sequential_features and store_type in features.

这篇关于如何为具有外部功能的时间序列多步水平构建LSTM的输入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆