基于TensorFlow的双层BiDirection_Attention_LSTM的北京PM2.5数据集预测
预测北京PM2.5数据集你会学到:
传统的线性模型难以解决多变量或多输入问题,而神经网络如LSTM则擅长于处理多个变量的问题,该特性使其有助于解决时间序列预测问题。
在接下来的这篇博客中,你将学会如何利用深度学习框架TF搭建LSTM模型来处理多个变量的时间序列预测问题。
经过这个博客你会掌握:
1. 如何将原始数据转化为适合处理时序预测问题的数据格式;
2. 如何准备数据并搭建双层双向+Attention的LSTM来处理时序预测问题;
3. 如何利用模型预测。
1.数据集内容
2.数据处理
利用Pandas处理数据集,PM2.5数据集在这里 ==》
[link](Beijing PM2.5 Data Set)
def PreData():
df = pd.read_csv(config.preprocess_file_path)
# 丢弃掉不需要的行
df.drop('No', axis=1, inplace=True)
# 丢弃含有NA的行
df.dropna(axis=0, how='any', inplace=True`
# 填充csv中的NAN为0.0
df.fillna(axis=0, value=0.0, inplace=True)
# index为False表示不要序列
df.to_csv(config.process_file_path, index=False)
new_pd = pd.read_csv(config.process_file_path)
print(new_pd.info())
print(new_pd.head(5))
#准备训练和测试的批次为100的数据
def getTrain_Test():
df = pd.read_csv(config.process_file_path)
weather_data = df.iloc[0:40000, 5:12]
test_data = df.iloc[40000:41700, 5:12]
weather_pm = df.iloc[0:40000, 4:5]
test_pm = df.iloc[40000:41700, 4:5]
print(weather_data)
print(type(weather_pm))
print(len(weather_pm))
i = 0
# 当数据为测试集时
if FLAGS.isPreData_isTrain_isPredict == 2:
while i < len(test_data):
# 当数据为训练集时
x_train = test_data[i:i + config.batch_size].values
y_label = test_pm[i:i + config.batch_size].values
# print(type(x_train)) <class 'numpy.ndarray'>
# print(x_train.shape) (72, 7)
string_encode = sp.LabelEncoder()
to_1 = sp.MinMaxScaler(feature_range=(0, 1))
x_train[:, 3] = string_encode.fit_transform(x_train[:, 3])
x_train = to_1.fit_transform(x_train)
y_label = to_1.fit_transform(y_label)
x_train = np.reshape(x_train, [100, 7, 1])
train_x_list.append(x_train.tolist())
label_y_list.append(y_label.tolist())
i += config.batch_size
if FLAGS.isPreData_isTrain_isPredict == 1:
while i < len(weather_pm):
# 当数据为训练集时
x_train = weather_data[i:i + config.batch_size].values
y_label = weather_pm[i:i + config.batch_size].values
# print(type(x_train)) <class 'numpy.ndarray'>
# print(x_train.shape) (72, 7)
string_encode = sp.LabelEncoder()
to_1 = sp.MinMaxScaler(feature_range=(0, 1))
x_train[:, 3] = string_encode.fit_transform(x_train[:, 3])
x_train = to_1.fit_transform(x_train)
y_label = to_1.fit_transform(y_label)
x_train = np.reshape(x_train, [100, 7, 1])
train_x_list.append(x_train.tolist())
label_y_list.append(y_label.tolist())
i += config.batch_size
3.需要导入的包以及参数配置
import tensorflow as tf
import pandas as pd
import sklearn.preprocessing as sp
import numpy as np
FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_integer('isPreData_isTrain_isPredict', 1, '0为PreData,1为Train,2为Predict')
tf.flags.DEFINE_integer('isAttention', 1, '0为关闭Attention机制,1为启用Attention机制')
tf.flags.DEFINE_integer('is_featureMap', 0, '0为每个特征都Attention权重,1为每个featureMap一个权重')
class Config():
preprocess_file_path = './PRSA_data_2010.1.1-2014.12.31.csv'
process_file_path = './PMcsv/bjpm.csv'
batch_size = 100
loop_time = [128, 128]
input_train_dim = 7
input_label_dim = 1
lstm_units = 128
config = Config()
train_x_list = []
label_y_list = []
input_data = tf.placeholder(tf.float32, [1, config.batch_size, config.input_train_dim, 1])
input_label = tf.placeholder(tf.float32, [1, config.batch_size, config.input_label_dim])
4.下面上LSTM模型了
def Bi_lstm():
global input_data
# 双层LSTM,第二次LSTM将前向,后向的输出值拼接起来
concat_tensor = input_data
concat_tensor = tf.reshape(concat_tensor, [100, 7, 1])
with tf.variable_scope('lstm1'):
lstm_cell = tf.nn.rnn_cell.LSTMCell(num_units=config.lstm_units)
lstm_fw_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, input_keep_prob=0.95, output_keep_prob=0.95)
lstm_bw_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, input_keep_prob=0.95, output_keep_prob=0.95)
# [100,7,1]===>[100,7,128]
value, _ = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, concat_tensor, dtype=tf.float32)
# [[100,7,128],[100,7,128]]===>[100,7,256]
concat_tensor = tf.concat(value, 2)
# concat_tensor = tf.layers.batch_normalization(concat_tensor)
with tf.variable_scope('lstm2'):
lstm_cell = tf.nn.rnn_cell.LSTMCell(num_units=config.lstm_units)
lstm_fw_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, input_keep_prob=0.90, output_keep_prob=0.80)
lstm_bw_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, input_keep_prob