当前位置: 首页 > news >正文

R 2火灾温度预测

火灾温度预测

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

使用LSTM进行时间序列预测

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

1. 数据预处理

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

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import MinMaxScalerdata = 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 = 0for _, _ in data.iterrows():in_end = in_start + width_Xout_end = in_end + width_yif 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 += 1X = np.array(X)
y = np.array(y).reshape(-1, 1, 1)
2. 构建LSTM模型

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

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoaderclass 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)优化器。训练过程中,我们会记录训练损失和测试损失,并绘制其变化曲线。

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 = 0for 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_batchesreturn train_lossdef test(dataloader, model, loss_fn):num_batches = len(dataloader)test_loss = 0with 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_batchesreturn test_lossfor 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²)。

# 预测并将数据移回 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模型的使用有了初步了解。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 442一场“吃干榨净”的富贵,是怎么拼出来的?
  • 【Java基础面试题】Java的优势
  • C++:共享指针(shared_ptr)详解
  • 【RK3588】yolov5的部署
  • 小白初次Vue启动遇到问题汇总
  • 不良信息公示
  • 鸿蒙 使用 Emitter 精准刷新
  • <Rust>egui学习之小部件(六):如何在窗口中添加菜单栏部件?
  • vue ref和reactive区别
  • SoftMaker Office Pro 2024:高效办公的全方位解决方案
  • 分库分表学习笔记(二)
  • 什么是池化层
  • jarbas 靶机渗透(cms 渗透)
  • Spring Cloud全解析:网关之GateWay简介
  • Linux sentinel写法
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • Bootstrap JS插件Alert源码分析
  • ERLANG 网工修炼笔记 ---- UDP
  • export和import的用法总结
  • Javascripit类型转换比较那点事儿,双等号(==)
  • Javascript 原型链
  • js
  • 程序员最讨厌的9句话,你可有补充?
  • 对象引论
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 记录一下第一次使用npm
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 使用Gradle第一次构建Java程序
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 我看到的前端
  • 一个SAP顾问在美国的这些年
  • postgresql行列转换函数
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • #我与Java虚拟机的故事#连载03:面试过的百度,滴滴,快手都问了这些问题
  • $L^p$ 调和函数恒为零
  • %check_box% in rails :coditions={:has_many , :through}
  • (1)Android开发优化---------UI优化
  • (3) cmake编译多个cpp文件
  • (LLM) 很笨
  • (二十三)Flask之高频面试点
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • ***通过什么方式***网吧
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .net后端程序发布到nignx上,通过nginx访问
  • .net开发日常笔记(持续更新)
  • .NET实现之(自动更新)
  • .net中我喜欢的两种验证码