PyTorch搭建LSTM实现多变量时间序列预测(负荷预测)

608 阅读1分钟

I. 前言

在前面的一篇文章PyTorch搭建LSTM实现时间序列预测(负荷预测)中,我们利用LSTM实现了负荷预测,但我们只是简单利用负荷预测负荷,并没有利用到其他一些环境变量,比如温度、湿度等。

本篇文章主要考虑用PyTorch搭建LSTM实现多变量时间序列预测。

II. 数据处理

数据集为某个地区某段时间内的电力负荷数据,除了负荷以外,还包括温度、湿度等信息。

本文中,我们根据前24个时刻的负荷以及该时刻的环境变量来预测下一时刻的负荷。最终得到了batch_size=B的数据集Dtr、Val以及Dte,Dtr为训练集,Val为验证集,Dte为测试集。

任意输出Dte中的一条数据:

[(tensor([[0.3513, 0.0000, 0.9091, 0.0000, 0.6667, 0.3023, 0.2439],
        [0.3333, 0.0000, 0.9091, 0.0435, 0.6667, 0.3023, 0.2439],
        [0.3396, 0.0000, 0.9091, 0.0870, 0.6667, 0.3023, 0.2439],
        [0.3427, 0.0000, 0.9091, 0.1304, 0.6667, 0.3023, 0.2439],
        [0.3838, 0.0000, 0.9091, 0.1739, 0.6667, 0.3023, 0.2439],
        [0.3700, 0.0000, 0.9091, 0.2174, 0.6667, 0.3023, 0.2439],
        [0.4288, 0.0000, 0.9091, 0.2609, 0.6667, 0.3023, 0.2439],
        [0.4474, 0.0000, 0.9091, 0.3043, 0.6667, 0.3023, 0.2439],
        [0.4406, 0.0000, 0.9091, 0.3478, 0.6667, 0.3023, 0.2439],
        [0.4657, 0.0000, 0.9091, 0.3913, 0.6667, 0.3023, 0.2439],
        [0.4540, 0.0000, 0.9091, 0.4348, 0.6667, 0.3023, 0.2439],
        [0.4939, 0.0000, 0.9091, 0.4783, 0.6667, 0.3023, 0.2439],
        [0.4328, 0.0000, 0.9091, 0.5217, 0.6667, 0.3023, 0.2439],
        [0.4238, 0.0000, 0.9091, 0.5652, 0.6667, 0.3023, 0.2439],
        [0.4779, 0.0000, 0.9091, 0.6087, 0.6667, 0.3023, 0.2439],
        [0.4591, 0.0000, 0.9091, 0.6522, 0.6667, 0.3023, 0.2439],
        [0.4651, 0.0000, 0.9091, 0.6957, 0.6667, 0.3023, 0.2439],
        [0.5102, 0.0000, 0.9091, 0.7391, 0.6667, 0.3023, 0.2439],
        [0.5067, 0.0000, 0.9091, 0.7826, 0.6667, 0.3023, 0.2439],
        [0.4635, 0.0000, 0.9091, 0.8261, 0.6667, 0.3023, 0.2439],
        [0.4224, 0.0000, 0.9091, 0.8696, 0.6667, 0.3023, 0.2439],
        [0.3796, 0.0000, 0.9091, 0.9130, 0.6667, 0.3023, 0.2439],
        [0.3292, 0.0000, 0.9091, 0.9565, 0.6667, 0.3023, 0.2439],
        [0.2940, 0.0000, 0.9091, 1.0000, 0.6667, 0.3023, 0.2439]]), tensor([0.3675]))]

每一行对应一个时刻点的负荷以及环境变量,此时input_size=7。

III. LSTM模型

这里采用了深入理解PyTorch中LSTM的输入和输出(从input输入到Linear输出)中的模型:

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(batch_size, seq_len, num_directions * hidden_size)
        output, _ = self.lstm(input_seq, (h_0, c_0)) # output(5, 30, 64)
        pred = self.linear(output)  # (5, 30, 1)
        pred = pred[:, -1, :]  # (5, 1)
        return pred

IV. 训练/测试

简单训练了30轮,MAPE为6.01%: 在这里插入图片描述

V. 源码及数据

后面将陆续公开~