如何在时间序列的 LSTM 模型中使用未来预测作为输入变量?
How can I use future forecasts as input variable in a LSTM model for time series?
一般的问题,我真的找不到任何答案,只是暗示这是可能的:
假设我想预测未来的销售额。
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 模型中?如果是这样,输入矩阵在训练和使用模型时会是什么样子?
你说得很对。您可以提供当前天气和以前的销售量作为输入来预测当前销售量。
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 中前向函数可接受的输入维度进行重塑 class。
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。希望这会有所帮助。
一般的问题,我真的找不到任何答案,只是暗示这是可能的:
假设我想预测未来的销售额。
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 模型中?如果是这样,输入矩阵在训练和使用模型时会是什么样子?
你说得很对。您可以提供当前天气和以前的销售量作为输入来预测当前销售量。
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 中前向函数可接受的输入维度进行重塑 class。
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。希望这会有所帮助。