I. 前言
这篇文章将多步长预测的第五种实现方法:Seq2Seq。
II. seq2seq
seq2seq由两部分组成:Encoder和Decoder。seq2seq的输入是一个序列,输出也是一个序列,经常用于时间序列预测。关于seq2seq的具体原理可以参考:DL入门(3):循环神经网络(RNN)。
III. 代码实现
3.1 数据处理
我们根据前24个时刻的负荷以及该时刻的环境变量来预测接下来12个时刻的负荷(步长pred_step_size可调)。
数据处理代码和前面的直接多输出预测一致。
3.2 模型搭建
模型搭建分为三个步骤:编码器、解码器以及seq2seq。
首先是Encoder:
class Encoder(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, batch_size):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.num_directions = 1
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True, bidirectional=False)
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, batch_size, self.hidden_size).to(device)
c_0 = torch.randn(self.num_directions * self.num_layers, batch_size, self.hidden_size).to(device)
output, (h, c) = self.lstm(input_seq, (h_0, c_0))
return h, c
一般来讲编码器采用的就是RNN网络,这里采用了LSTM将原始数据进行编码,然后将LSTM的最后的隐状态和单元状态返回。
接着是解码器Decoder:
class Decoder(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
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True, bidirectional=False)
self.linear = nn.Linear(self.hidden_size, self.output_size)
def forward(self, input_seq, h, c):
# input_seq(batch_size, input_size)
batch_size = input_seq.shape[0]
input_seq = input_seq.view(batch_size, 1, self.input_size)
output, (h, c) = self.lstm(input_seq, (h, c))
# output(batch_size, seq_len, num * hidden_size)
pred = self.linear(output) # pred(batch_size, 1, output_size)
pred = pred[:, -1, :]
return pred, h, c
解码器同样也由LSTM组成,不过解码器的初始的隐状态和单元状态是编码器的输出。此外,解码器每次只输入seq_len中的一个。
最后定义seq2seq:
class Seq2Seq(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size):
super().__init__()
self.output_size = output_size
self.Encoder = Encoder(input_size, hidden_size, num_layers, batch_size)
self.Decoder = Decoder(input_size, hidden_size, num_layers, output_size, batch_size)
def forward(self, input_seq):
batch_size, seq_len, _ = input_seq.shape[0], input_seq.shape[1], input_seq.shape[2]
h, c = self.Encoder(input_seq)
outputs = torch.zeros(batch_size, seq_len, self.output_size).to(device)
for t in range(seq_len):
_input = input_seq[:, t, :]
output, h, c = self.Decoder(_input, h, c)
outputs[:, t, :] = output
return outputs[:, -1, :]
seq2seq中,将24个时刻的的数据依次迭代输入解码器。
3.3 模型训练/测试
模型训练和测试同前文一致。
3.4 实验结果
前24个预测未来12个,每个模型训练50轮,MAPE为9.09%,还需要进一步完善。
IV. 源码及数据
后面将陆续公开~