一、LSTM是什么?
长短期记忆网络(Long-Short Term Memory,LSTM)论文首次发表于1997年。由于独特的设计结构,LSTM适合于处理和预测时间序列中间隔和延迟非常长的重要事件。时间序列数据是以一定有规律的时间间隔,从而在每个时间段产生一系列相应数据。时间序列的相应问题(这里主要将时序问题预测)是根据前n时刻的数据,从此预测后面时刻的数据。
二、使用步骤
1.读入数据
代码如下(示例):
filepath = 'E:\LSTM\data\LAI.xlsx'
data = pd.read_excel(filepath)
print(data.head())
temp = data['LAI']
temp.index = data['Date Time']
2.完整代码
代码如下(示例):
import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# 调用GPU加速
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
filepath = 'E:\LSTM\data\LAI.xlsx'
data = pd.read_excel(filepath)
print(data.head())
temp = data['LAI']
temp.index = data['Date Time']
temp.plot()
# 取前80%个数据作为训练集
train_num = int(len(data) * 0.8)
# 80%-90%用于验证
val_num = int(len(data) * 0.9)
# 最后10%用于测试
temp_mean = temp[:train_num].mean() # 均值
temp_std = temp[:train_num].std() # 标准差
# 标准化
inputs_feature = (temp - temp_mean) / temp_std
def database(dataset, start_index, end_index, history_size, target_size):
data = []
labels = []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = range(i - history_size, i)
data.append(np.reshape(dataset[indices], (history_size, 1)))
labels.append(dataset[i + target_size])
return np.array(data), np.array(labels)
history_size = 25
target_size = 0
x_train, y_train = database(inputs_feature.values, 0, train_num,
history_size, target_size)
x_val, y_val = database(inputs_feature.values, train_num, val_num,
history_size, target_size)
x_test, y_test = database(inputs_feature.values, val_num, None,
history_size, target_size)
print(y_test)
print('x_train.shape:', x_train.shape) # x_train.shape: (109125, 20, 1)
print('y_train.shape:', y_train.shape)
print('x_test.shape:', x_test.shape) # x_train.shape: (109125, 20, 1)
print('y_test.shape:', y_test.shape)
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_ds = train_ds.shuffle(25).batch(10)
# 验证集
val_ds = tf.data.Dataset.from_tensor_slices((x_val, y_val))
val_ds = val_ds.batch(10)
sample = next(iter(train_ds))
print('x_batch.shape:', sample[0].shape, 'y_batch.shape:', sample[1].shape)
print('input_shape:', sample[0].shape[-2:])
# 构造输入层
inputs = keras.Input(shape=sample[0].shape[-2:])
x = keras.layers.LSTM(8)(inputs)
x = keras.layers.Activation('relu')(x)
outputs = keras.layers.Dense(1)(x)
# 构造模型
model = keras.Model(inputs, outputs)
# 查看模型结构
model.summary()
opt = keras.optimizers.Adam(learning_rate=0.001) # 优化器
model.compile(optimizer=opt, loss='mae') # 平均误差损失
epochs = 25
history = model.fit(train_ds, epochs=epochs, validation_data=val_ds)
history_dict = history.history
train_loss = history_dict['loss'] # 训练集损失
val_loss = history_dict['val_loss']
print('训练完成')
plt.figure()
plt.plot(range(epochs), train_loss, label='train_loss') # 训练集损失
plt.plot(range(epochs), val_loss, label='val_loss') # 验证集损失
plt.legend() # 显示标签
plt.xlabel('epochs')
plt.ylabel('loss')
plt.show()
print('损失完成')
y_predict = model.predict(x_test) # 对测试集的特征值进行预测
dates = temp[val_num:-25].index # 获取时间索引
print('预测完成')
fig = plt.figure(figsize=(10, 5))
axes = fig.add_subplot(111)
axes.plot(dates, y_test, 'bo', label='actual')
print('绘制完成')
axes.plot(dates, y_predict, 'ro', label='predict')
axes.set_xticks(dates[::30])
axes.set_xticklabels(dates[::30], rotation=45)
plt.legend()
plt.grid()
plt.show()