深度学习-第R2周——LSTM火灾温度预测

深度学习-第R2周——LSTM火灾温度预测

  • 深度学习-第R2周——LSTM火灾温度预测
    • 一、前言
    • 二、我的环境
    • 三、前期工作
      • 1、导入数据集
      • 2、数据可视化
    • 四、构建数据集
      • 1、设置x,y
      • 2、归一化
      • 3、划分数据集
    • 五、构建模型
    • 六、模型训练
      • 1、编译
      • 2、训练
    • 七、评估
      • 1、loss图
      • 2、预测

深度学习-第R2周——LSTM火灾温度预测

一、前言

  • 本文为365天深度学习训练营 中的学习记录博客
  • 原作者:K同学啊

二、我的环境

  • 电脑系统:Windows 10
  • 语言环境:Python 3.8.5
  • 编译器:colab在线编译
  • 深度学习环境:Tensorflow

三、前期工作

1、导入数据集

下载数据集,并划分训练集和测试集

import tensorflow as tf
import pandas as pd
import numpy as np

df_1 = pd.read_csv("woodpine2.csv")

2、数据可视化

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()

深度学习-第R2周——LSTM火灾温度预测_第1张图片

四、构建数据集

dataFrame = df_1.iloc[:, 1:]
dataFrame

深度学习-第R2周——LSTM火灾温度预测_第2张图片

1、设置x,y

取前8个时间段的Tem1,CO1,Soot1为X,第九个时间段的tem1为y

width_X = 8
width_y = 1
len(dataFrame)

X = []
y = []

in_start = 0
for _, _ in df_1.iterrows(): #df.iterrows( )函数:可以返回所有的行索引,以及该行的所有内容
  in_end = in_start + width_X #0+8
  out_end = in_end + width_y #8+1

  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

2、归一化

from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler(feature_range = (0, 1))
X_scaled = sc.fit_transform(X) 
X_scaled.shape

3、划分数据集

取5000之前的数据为训练集,5000之后的为验证集

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

五、构建模型

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM, Bidirectional
from tensorflow.keras import Input

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))

六、模型训练

1、编译

model_lstm.compile(optimizer=tf.keras.optimizers.Adam(1e-3), loss = 'mean_squared_error')

2、训练

history_lstm = model_lstm.fit(X_train, y_train, batch_size = 64, epochs = 40, validation_data = (X_test, y_test), validation_freq = 1)

深度学习-第R2周——LSTM火灾温度预测_第3张图片

七、评估

1、loss图

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #运行配置参数中的字体(font)为黑体(SimHei)

plt.rcParams['axes.unicode_minus'] = False #运行配置参数总的轴(axes)正常显示正负号(minus)


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()

深度学习-第R2周——LSTM火灾温度预测_第4张图片

2、预测

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()

深度学习-第R2周——LSTM火灾温度预测_第5张图片

你可能感兴趣的:(深度学习,lstm,python)