目录
[1) 项目背景与目标](#1) 项目背景与目标)
前言
🍨 本文为🔗365天深度学习训练营中的学习记录博客
🍖 原作者:K同学啊
1.检查GPU
python
import tensorflow as tf
import pandas as pd
import numpy as np
gpus = tf.config.list_physical_devices("GPU")
if gpus:
tf.config.experimental.set_memory_growth(gpus[0], True) #设置GPU显存用量按需使用
tf.config.set_visible_devices([gpus[0]],"GPU")
print(gpus)
2.查看数据
python
import pandas as pd
import numpy as np
df_1 = pd.read_csv("data/woodpine2.csv")
df_1
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['savefig.dpi'] = 500 #图片像素
plt.rcParams['figure.dpi'] = 500 #分辨率
fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(14, 3))
sns.lineplot(data=df_1["Tem1"], ax=ax[0])
sns.lineplot(data=df_1["CO 1"], ax=ax[1])
sns.lineplot(data=df_1["Soot 1"], ax=ax[2])
plt.show()
dataFrame = df_1.iloc[:,1:]
dataFrame
3.划分数据集
python
width_X = 8
width_y = 1
X = []
y = []
in_start = 0
for _, _ in df_1.iterrows():
in_end = in_start + width_X
out_end = in_end + width_y
if out_end < len(dataFrame):
X_ = np.array(dataFrame.iloc[in_start:in_end , ])
X_ = X_.reshape((len(X_)*3))
y_ = np.array(dataFrame.iloc[in_end :out_end, 0])
X.append(X_)
y.append(y_)
in_start += 1
X = np.array(X)
y = np.array(y)
X.shape, y.shape
from sklearn.preprocessing import MinMaxScaler
#将数据归一化,范围是0到1
sc = MinMaxScaler(feature_range=(0, 1))
X_scaled = sc.fit_transform(X)
X_scaled.shape
X_scaled = X_scaled.reshape(len(X_scaled),width_X,3)
X_scaled.shape
X_train = np.array(X_scaled[:5000]).astype('float64')
y_train = np.array(y[:5000]).astype('float64')
X_test = np.array(X_scaled[5000:]).astype('float64')
y_test = np.array(y[5000:]).astype('float64')
X_train.shape
4.创建模型
python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM,Bidirectional
from tensorflow.keras import Input
# 多层 LSTM
model_lstm = Sequential()
model_lstm.add(LSTM(units=64, activation='relu', return_sequences=True,
input_shape=(X_train.shape[1], 3)))
model_lstm.add(LSTM(units=64, activation='relu'))
model_lstm.add(Dense(width_y))
5.编译及训练模型
python
# 只观测loss数值,不观测准确率,所以删去metrics选项
model_lstm.compile(optimizer=tf.keras.optimizers.Adam(1e-3),
loss='mean_squared_error') # 损失函数用均方误差
history_lstm = model_lstm.fit(X_train, y_train,
batch_size=64,
epochs=40,
validation_data=(X_test, y_test),
validation_freq=1)
6.结果可视化
python
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.figure(figsize=(5, 3),dpi=120)
plt.plot(history_lstm.history['loss'] , label='LSTM Training Loss')
plt.plot(history_lstm.history['val_loss'], label='LSTM Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
predicted_y_lstm = model_lstm.predict(X_test) # 测试集输入模型进行预测
y_test_one = [i[0] for i in y_test]
predicted_y_lstm_one = [i[0] for i in predicted_y_lstm]
plt.figure(figsize=(5, 3),dpi=120)
# 画出真实数据和预测数据的对比曲线
plt.plot(y_test_one[:1000], color='red', label='真实值')
plt.plot(predicted_y_lstm_one[:1000], color='blue', label='预测值')
plt.title('Title')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()
7.模型评估
python
from sklearn import metrics
"""
RMSE :均方根误差 -----> 对均方误差开方
R2 :决定系数,可以简单理解为反映模型拟合优度的重要的统计量
"""
RMSE_lstm = metrics.mean_squared_error(predicted_y_lstm, y_test)**0.5
R2_lstm = metrics.r2_score(predicted_y_lstm, y_test)
print('均方根误差: %.5f' % RMSE_lstm)
print('R2: %.5f' % R2_lstm)
8.总结
1) 项目背景与目标
本项目基于 TensorFlow 框架,构建了一个用于时间序列预测的 LSTM 模型。目标是从包含 Tem1、CO 1 和 Soot 1 三个特征的数据集中提取信息,利用连续8个时间步的历史数据来预测第9个时间步的 Tem1 值。整个代码流程涵盖了数据预处理、模型构建与训练、性能评估以及结果可视化等核心步骤。
2)代码结构与关键流程
(1)GPU资源检测与配置
通过 tf.config.list_physical_devices("GPU") 检查系统是否支持GPU运行。
同时启用按需分配显存的策略,以防止训练过程中出现内存溢出的问题。
使用GPU可显著提高训练速度,尤其适用于大数据量的时间序列任务。
(2)数据探索与归一化
数据集中包含三个字段:Tem1(预测目标)、CO 1 和 Soot 1。
利用 seaborn 和 matplotlib 进行可视化处理,分析特征随时间的变化趋势。
采用归一化方法将各特征值缩放至 [0, 1] 区间,有助于提升模型的训练稳定性与收敛效率。
(3)数据集划分与格式整理
通过滑动窗口方式构建训练样本,每条样本的输入包含前8个时间步的特征值(共3维),输出为第9个时间点的 Tem1。
划分后的数据被转换为 NumPy 数组,并进一步调整为 LSTM 所需的三维格式(样本数, 时间步长, 特征数)。
(4)模型构建
搭建了一个包含两层 LSTM 的深度网络结构:
-
第一层 LSTM 包含64个单元,使用 ReLU 激活函数,并输出完整序列供后续处理。
-
第二层 LSTM 进一步整合时序特征,最后通过一个全连接层生成最终预测值。
这种结构有助于模型学习长时依赖关系,适用于复杂时间序列建模任务。
(5)模型编译与训练过程
使用均方误差(MSE)作为损失函数,优化器选用 Adam。
模型以批大小为64进行训练,共执行40轮(epoch),每轮均验证模型在验证集上的表现。
训练过程中可实时监控验证集误差,确保模型不会过拟合。
(6)预测结果与可视化展示
绘制训练和测试损失曲线以评估模型是否收敛。
通过对比预测值与实际值的时间序列图,验证模型的预测准确性。
借助列表推导式将多维数组转为一维序列,便于图像绘制和误差分析。
(7)性能评估指标
采用均方根误差(RMSE)和决定系数(R²)来衡量模型的泛化能力:
-
RMSE 表征预测误差的平均水平;
-
R² 用于衡量模型对目标变量波动的解释能力。
评估结果显示模型在一定程度上具备预测能力,但仍有优化空间。
3)代码优点总结
-
数据处理得当:结合归一化与滑动窗口法,合理组织了模型输入格式。
-
模型结构清晰:双层 LSTM 架构提高了模型捕捉时间依赖特征的能力。
-
训练策略合理:使用自适应优化器 Adam 和经典的 MSE 损失函数,使训练过程更加稳定。
-
评估全面:结合可视化与数值评价指标,较为全面地分析了模型效果。