【问题标题】:How can I use future forecasts as input variable in a LSTM model for time series?如何在 LSTM 模型中将未来预测用作时间序列的输入变量?
【发布时间】:2020-03-17 05:54:14
【问题描述】:

我无法找到任何答案的一般问题,仅暗示有可能:

假设我想预测未来的销售额。

y(t+1) = sales at day t+1 (t+1 = next day)

我有两个输入变量;历史销售和历史天气预报。

x1(t) = historical sales day t
x2(t) = historical weather forecast for day t

训练模型后,我可以预测 y(t+1)。

但是,我如何使用未来的天气数据作为输入?我已经有了 t+1 天的天气预报,它会影响我的销售,我想将其用作输入 - 在本例中为 x2(t+1)。像这样:

Output:
y(t+1)

Input:
x1(t)
x2(t)
x2(t+1) <------

是否可以将此功能合并到 LSTM 模型中?如果是这样,输入矩阵在训练和使用模型时会是什么样子?

【问题讨论】:

    标签: machine-learning time-series lstm data-science recurrent-neural-network


    【解决方案1】:

    你说的很对。您可以提供当前天气和以前的销售额作为预测当前销售额的输入。

    sales[t+1] = RNN(weather[t+1], sales[t]) <-- [Correct]
    

    但是,没有必要提供以前的天气,因为相关信息将通过隐藏的功能传播。

    sales[t+1] = RNN(weather[t+1], weather[t], sales[t]) <-- [Wrong]
    

    示例

    让这成为我们的样本数据。

    df = pd.DataFrame([{'weather':1, 'sales':500}, {'weather':3, 'sales':200}, {'weather':2, 'sales':400}, {'weather':0, 'sales':600}])
    print(df)
    
       weather  sales
    0        1    500
    1        3    200
    2        2    400
    3        0    600
    

    我们必须生成具有特定维度的训练输入。

    #Training input dimensions = (No. of training samples, seq_length, No. of features)
    
    seq_len = 3 #Number of times the LSTM loops
    n_features = 2 # weather and sales are considered as input
    
    training_input = torch.zeros((df.shape[0], seq_len, n_features))
    row = torch.zeros(seq_len, n_features)
    for i in range(df.shape[0]):
        row[:-1] = row[1:]
        prev_sales = df.sales[i-1] if i > 0 else 0 #i.e., sales[-1] = 0
        row[-1, :] = torch.tensor([df.weather[i], prev_sales])
        training_input[i] = row
    
    print(training_input)
    
    tensor([[[  0.,   0.],
             [  0.,   0.],
             [  1.,   0.]],
    
            [[  0.,   0.],
             [  1.,   0.],
             [  3., 500.]],
    
            [[  1.,   0.],
             [  3., 500.],
             [  2., 200.]],
    
            [[  3., 500.],
             [  2., 200.],
             [  0., 400.]]])
    

    以下部分是有关向 LSTM 层提供训练输入的示例。

    初始化 LSTM 参数

    input_size = 2 #weather and previous sales are considered as input
    hidden_size = 2 #any number can be used
    n_layers = 1 #number of LSTMs stacked. In this case, only 1 LSTM is used
    batch_size = training_input.size()[0] #passing entire training input in one go
    

    初始化 hidden_​​input

    hidden_input = torch.zeros(n_layers,batch_size,hidden_size), torch.zeros(n_layers,batch_size, hidden_size)
    

    创建 LSTM 层

    lstm = nn.LSTM(input_size,hidden_size)
    

    必须根据 LSTM 类中前向函数接受的输入维度对训练输入进行整形。

    lstm_input = training_input.view(seq_len,batch_size,input_size)
    out, hidden = lstm(lstm_input, hidden_input)
    
    print(out[-1])
    
    tensor([[2.0370e-10, 9.6134e-07],
        [2.2299e-25, 7.1835e-28],
        [2.0600e-10, 1.1409e-06],
        [8.0952e-21, 1.2101e-24]], grad_fn=<SelectBackward>)
    

    有关详细信息,请参阅Pytorch documentation for LSTM layer。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2019-12-01
      • 2018-07-03
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      相关资源
      最近更新 更多