目录

  • I. 前言
  • II. 数据处理
  • III. LSTM模型
  • IV. 训练/测试
  • V. 源码及数据


I. 前言

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

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

II. 数据处理

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

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

任意输出一条数据:

(<tf.Tensor: shape=(24, 7), dtype=float32, numpy=
array([[0.36147627, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.3429366 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.34939995, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.35257494, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.39485145, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.38066387, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.44114256, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.4603167 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.45330796, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.47912365, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.46706894, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.5081953 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.4452976 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.4360156 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.4917237 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.4723147 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.47849187, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.524864  , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.52128404, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.47682068, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.4345901 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.39052632, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.33869517, 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244],
       [0.3025095 , 0.        , 0.90909094, 0.        , 0.8333333 ,
        0.3255814 , 0.24390244]], dtype=float32)>, <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.37805316], dtype=float32)>)

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

III. LSTM模型

这里采用了TensorFlow搭建LSTM实现时间序列预测(负荷预测)中的模型:

class LSTM(keras.Model):
    def __init__(self, args):
        super(LSTM, self).__init__()
        self.lstm = Sequential()
        for i in range(args.num_layers):
            self.lstm.add(layers.LSTM(units=args.hidden_size, input_shape=(args.seq_len, args.input_size),
                                      activation='tanh', return_sequences=True))
        self.fc1 = layers.Dense(64, activation='relu')
        self.fc2 = layers.Dense(args.output_size)

    def call(self, data, training=None, mask=None):
        x = self.lstm(data)
        x = self.fc1(x)
        x = self.fc2(x)

        return x[:, -1:, :]

IV. 训练/测试

简单训练了30轮,MAPE为4.14%:

LSTM多输入单输出python lstm输出多个预测值_lstm

V. 源码及数据

后面将陆续公开~