I. 前言
这篇文章介绍多步长预测的第四种实现方式:多模型滚动预测。
II. 多模型滚动预测
所谓多模型滚动预测:还是前10个预测后3个为例:首先需要按照多模型单步预测的方式训练3个模型,然后模型1利用[1…10]预测[11’],然后模型2利用[2…10 11’]预测[12’],最后由模型3利用[3…10 11’ 12’]预测[13’]。
III. 代码实现
3.1 数据处理
我们根据前24个时刻的负荷以及该时刻的环境变量来预测接下来12个时刻的负荷(步长pred_step_size可调)。
数据处理代码和前面的多模型单步预测一致。简单来说,如果需要利用前24个值预测接下来12个值,那么我们需要生成12个数据集。
3.2 模型搭建
模型和之前的文章一致:
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 1 # 单向LSTM
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.linear = nn.Linear(self.hidden_size, self.output_size)
def forward(self, input_seq):
batch_size, seq_len = input_seq.shape[0], input_seq.shape[1]
h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(device)
output, _ = self.lstm(input_seq, (h_0, c_0))
pred = self.linear(output)
pred = pred[:, -1, :]
return pred
3.3 模型训练/测试
模型训练与多模型单步预测一致。
模型测试与单步滚动预测有些类似,但每一步都由不同的模型来进行预测。
3.4 实验结果
前24个预测未来12个,每个模型训练50轮,效果很差,MAPE为13.26%,还需要进一步完善。
IV. 源码及数据
后面将陆续公开~