Neural network 如何构造具有外部特征的时间序列多步视界的LSTM输入数据?

Neural network 如何构造具有外部特征的时间序列多步视界的LSTM输入数据?,neural-network,time-series,keras,lstm,recurrent-neural-network,Neural Network,Time Series,Keras,Lstm,Recurrent Neural Network,我正在尝试使用LSTM做商店销售预测。以下是我的原始数据的外观: | Date | StoreID | Sales | Temperature | Open | StoreType | |------------|---------|-------|-------------|---------|-----------| | 01/01/2016 | 1 | 0 | 36 | 0 | 1 | | 01/02/20

我正在尝试使用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天的销售额。在本例中,我需要预测2017年1月1日至2017年10月1日的门店销售额。我知道如何使用其他时间序列模型或回归模型来解决这个问题,但我想知道RNN-LSTM是否是一个很好的候选者

我首先只获取storeID=1数据来测试LSTM。如果我的数据只包含日期和销售额。我将以这种方式构造trainX和trainY(如果我错了,请纠正我):

在重塑两个

trainX.shape
(300, 1, 20)
trainY.shape
(300, 10)
问题1:在这种情况下,[样本、时间步长、特征]=[300,1,20]。是这样吗?或者我应该将样本构造为[300,20,1]

问题2:我确实想使用原始数据中的其他信息,如温度、存储类型等。我应该如何构建LSTM的输入数据

问题3:到目前为止,我们只讨论了1个门店预测,如果我想预测所有门店,那么我应该如何构建输入数据


目前我正在从中举出例子,但这似乎不足以涵盖我的场景。我真的很感谢你的帮助

我最近在解决类似的问题。就你而言:

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

  • 你可以这样做:

    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)
    
  • 这是最难的部分。我不建议您通过将多个店铺作为一个特征向量反馈到网络来预测多个店铺。您可以跳过这一部分,尝试使用一个模型预测不同的店铺,或使用某种图形模型或矩阵上的
    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\u data,features]
    其中
    sequential\u data.shape=(nb\u of \u examples,timesteps,sequential\u features)
    features.shape=(nb\u of \u examples,feature\u nb)
    。所以
    sales
    temperature
    应该存储在
    sequential\u features
    中,而
    store\u type
    应该存储在
    features
    中,非常感谢您的回答!对于#1,我尝试了(300,20,1)选项,精度比以前好多了!(虽然速度很慢)对于#2,我仍然对答案感到非常困惑。我们是否将一个LSTM图层与一个要素图层合并?要素层是另一个lstm层吗?或者你是在建议我们将每个X视为一个单独的序列,并为每个X序列创建一个LSTM?n=3?为什么我们最终需要多个致密层?如果你能解释一下逻辑,那就太好了!真的再次感谢!!!好啊我终于明白了——天气信息也是连续的,是的(你有一系列的天气,不是吗?)是的。“温度”列是基于时间的序列。”“打开”和“存储类型”列更像是存储状态的分类功能。我想我仍然不确定如何在LSTM模型中添加这些额外的特性。我猜我们可以在3d张量中添加特征[样本、时间步长、特征],或者为不同的序列创建多个层并将它们合并在一起?非常感谢更新答案!!只是想确保我能理解。您正在使用LSTM处理所有连续的_数据(如销售、临时、假日)。完成后,我们将从序列中学习到的内容与不基于时间的功能合并。最后,我们使用多个密集层来处理所有信息,就像一个完全连接的前馈网络。这是否与为序列数据构建一个RNN模型相同,然后使用带有额外信息的输出构建另一个CNN模型?输入数据应如何构建?它是否也必须作为timestep系列进行准备?我现在正在为同一个问题而挣扎,如果您有任何进一步的信息,请告诉我,谢谢陈-)
        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])