R 2火灾温度预测

火灾温度预测

使用LSTM进行时间序列预测

这周学习如何使用长短期记忆网络(LSTM)进行时间序列预测。使用PyTorch框架来构建和训练模型,基于一个包含温度、CO浓度以及烟灰浓度的数据集。

1. 数据预处理

首先,需要对数据进行预处理。数据集中的特征包括温度(Tem1)、CO浓度(CO 1)、以及烟灰浓度(Soot 1)。我们将这些特征标准化,并构建输入序列X和目标值y

python 复制代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler

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

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=data["Tem1"], ax=ax[0])
sns.lineplot(data=data["CO 1"], ax=ax[1])
sns.lineplot(data=data["Soot 1"], ax=ax[2])
plt.show()

# 数据归一化
dataFrame = data.iloc[:, 1:].copy()
sc  = MinMaxScaler(feature_range=(0, 1))

for i in ['CO 1', 'Soot 1', 'Tem1']:
    dataFrame[i] = sc.fit_transform(dataFrame[i].values.reshape(-1, 1))

width_X = 8
width_y = 1

# 构建输入序列X和目标值y
X = []
y = []

in_start = 0

for _, _ in data.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, ])
        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).reshape(-1, 1, 1)
2. 构建LSTM模型

接下来,使用PyTorch构建一个两层的LSTM模型。模型输入为前8个时间步的Tem1CO 1Soot 1数据,输出为第9个时间步的Tem1值。

python 复制代码
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader

class model_lstm(nn.Module):
    def __init__(self):
        super(model_lstm, self).__init__()
        self.lstm0 = nn.LSTM(input_size=3, hidden_size=320,
                             num_layers=1, batch_first=True)

        self.lstm1 = nn.LSTM(input_size=320, hidden_size=320,
                             num_layers=1, batch_first=True)
        self.fc0 = nn.Linear(320, 1)

    def forward(self, x):
        out, hidden1 = self.lstm0(x)
        out, _ = self.lstm1(out, hidden1)
        out = self.fc0(out)
        return out[:, -1:, :]
3. 模型训练

使用均方误差(MSE)作为损失函数,并采用随机梯度下降(SGD)优化器。训练过程中,我们会记录训练损失和测试损失,并绘制其变化曲线。

python 复制代码
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model_lstm().to(device)
loss_fn = nn.MSELoss()
opt = torch.optim.SGD(model.parameters(), lr=1e-1, weight_decay=1e-4)
epochs = 50
train_loss = []
test_loss = []
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(opt, epochs)

def train(train_dl, model, loss_fn, opt, lr_scheduler=None):
    size = len(train_dl.dataset)
    num_batches = len(train_dl)
    train_loss = 0

    for x, y in train_dl:
        x, y = x.to(device), y.to(device)
        pred = model(x)
        loss = loss_fn(pred, y)

        opt.zero_grad()
        loss.backward()
        opt.step()

        train_loss += loss.item()

    if lr_scheduler is not None:
        lr_scheduler.step()
    train_loss /= num_batches
    return train_loss

def test(dataloader, model, loss_fn):
    num_batches = len(dataloader)
    test_loss = 0

    with torch.no_grad():
        for x, y in dataloader:
            x, y = x.to(device), y.to(device)
            y_pred = model(x)
            loss = loss_fn(y_pred, y)
            test_loss += loss.item()

    test_loss /= num_batches
    return test_loss

for epoch in range(epochs):
    model.train()
    epoch_train_loss = train(train_dl, model, loss_fn, opt, lr_scheduler)

    model.eval()
    epoch_test_loss = test(test_dl, model, loss_fn)

    train_loss.append(epoch_train_loss)
    test_loss.append(epoch_test_loss)

    template = ('Epoch:{:2d}, Train_loss:{:.5f}, Test_loss:{:.5f}')
    print(template.format(epoch + 1, epoch_train_loss, epoch_test_loss))

plt.figure(figsize=(5, 3), dpi=120)
plt.plot(train_loss, label='LSTM Training Loss')
plt.plot(test_loss, label='LSTM Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
4. 预测与评估

最后,使用训练好的模型对测试集进行预测,并计算模型的均方根误差(RMSE)和决定系数(R²)。

python 复制代码
# 预测并将数据移回 CPU 进行逆变换
predicted_y_lstm = sc.inverse_transform(model(X_test).detach().cpu().numpy().reshape(-1, 1))
y_test_1 = sc.inverse_transform(y_test.reshape(-1, 1))

# 转换为列表形式
y_test_one = [i[0] for i in y_test_1]
predicted_y_lstm_one = [i[0] for i in predicted_y_lstm]

# 绘制真实值和预测值
plt.figure(figsize=(5, 3), dpi=120)
plt.plot(y_test_one[:2000], color='red', label='真实值')
plt.plot(predicted_y_lstm_one[:2000], color='blue', label='预测值')
plt.title('预测结果对比')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()

from sklearn import metrics
RMSE_lstm = metrics.mean_squared_error(predicted_y_lstm_one, y_test_1) ** 0.5
R2_lstm = metrics.r2_score(predicted_y_lstm_one, y_test_1)

print('均方根误差: %.5f' % RMSE_lstm)
print('R2: %.5f' % R2_lstm)
5.结果


总结

这周学习了如何使用LSTM模型进行时间序列预测,并掌握数据预处理、模型构建、训练、预测及模型评估的全流程,对lstm模型的使用有了初步了解。

相关推荐
IT_陈寒11 小时前
Vite的public文件夹放静态资源?这坑我替你踩了
前端·人工智能·后端
传说故事11 小时前
【论文阅读】Diffusion Forcing: Next-token Prediction Meets Full-Sequence Diffusion
论文阅读·人工智能·diffusion
码界奇点11 小时前
基于Python的新浪微博数据爬虫系统设计与实现
数据库·爬虫·python·毕业设计·新浪微博·源代码管理
xixixi7777711 小时前
三重筑基:5G-A超级上行提速千兆,电联低频共享扫平盲点,800V HVDC算电协同破局
人工智能·5g·ai·大模型·算力·通信·信通院
jkyy201411 小时前
AI运动数字化:以技术重塑场景,健康有益赋能全域运动健康管理
大数据·人工智能·健康医疗
金融小师妹11 小时前
4月30日多因子共振节点:鲍威尔“收官效应”与权力结构重塑的预期重构
大数据·人工智能·重构·逻辑回归
2601_9499251811 小时前
AI Agent如何重构跨境物流的决策?
大数据·人工智能·重构·ai agent·geo优化·物流科技
AI木马人11 小时前
1.人工智能实战:大模型推理接口响应慢?从模型加载到 FastAPI 部署的完整优化方案
人工智能·python·fastapi
青少儿编程课堂11 小时前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
Black蜡笔小新11 小时前
私有化本地化AI模型训推工作站DLTM训推一体工作站赋能多行业智能化落地
人工智能