LSTM火灾温度预测(Pytorch版本)

ops/2025/1/17 5:25:55/

本文为为🔗365天深度学习训练营内部文章

原作者:K同学啊

 一 导入数据

import torch.nn.functional as F
import numpy as np
import pandas as pd
import torch
from torch import nn
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")data = pd.read_csv('woodpine2.csv')
print(data)

 

二 可视化

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()df = data.iloc[:,1:]
print(df)

 

三 构建数据集

1.数据集预处理 

from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0,1))for i in ['Tem1','CO 1','Soot 1']:df[i] = sc.fit_transform(df[i].values.reshape(-1,1))
print(df.shape)

 2.取前八个时间段的Tem、CO 1、Soot 1的数据为X,第九个数据为y

width_x = 8
width_y = 1
x = []
y = []in_start = 0for _,_ in df.iterrows():in_end = in_start + width_xout_end = in_end + width_yif out_end < len(df):X_ = np.array(df.iloc[in_start:in_end,])# 0代表只提取第一列y_ = np.array(df.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)
print(x.shape,y.shape)print('-----检查数据集中是否有空值-------')
print(np.any(np.isnan(x)))
print(np.any(np.isnan(y)))

 3.划分数据集

# 3.划分数据集
X_train = torch.tensor(np.array(x[:5000]).astype('float32'))
y_train = torch.tensor(np.array(y[:5000]).astype('float32'))
X_test = torch.tensor(np.array(x[5000:]).astype('float32'))
y_test = torch.tensor(np.array(y[5000:]).astype('float32'))
print(X_train.shape,X_test.shape)from torch.utils.data import TensorDataset,DataLoadertrain_dl = DataLoader(TensorDataset(X_train,y_train),batch_size=64,shuffle=False)
test_dl = DataLoader(TensorDataset(X_test,y_test),batch_size=64,shuffle=False)
(5948, 3)
(5939, 8, 3) (5939, 1, 1)
-----检查数据集中是否有空值-------
False
False
torch.Size([5000, 8, 3]) torch.Size([939, 8, 3])

四 模型训练 

1.构建模型

'''
模型训练
'''
# 1.构建模型
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,:]  # 取2个预测值,否则经过lstm会得到8*2个预测model = model_lstm()
print(model)
model_lstm((lstm0): LSTM(3, 320, batch_first=True)(lstm1): LSTM(320, 320, batch_first=True)(fc0): Linear(in_features=320, out_features=1, bias=True)
)

2.编写训练函数

# 2.定义训练函数
# 训练循环
def train(train_dl, model, loss_fn, optimizer,lr_scheduler=None):size = len(train_dl.dataset)  # 训练集的大小,一共60000张图片num_batches = len(train_dl)  # 批次数目,1875(60000/32)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)  # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失# 反向传播optimizer.zero_grad()  # grad属性归零loss.backward()  # 反向传播optimizer.step()  # 每一步自动更新# 记录acc与losstrain_loss += loss.item()if lr_scheduler is not None:lr_scheduler.step()print('learning rate = {:.5f}'.format(optimizer.param_groups[0]['lr']),end=' ')train_loss /= num_batchesreturn train_loss

3.编写测试函数 

def t(dataloader, model, loss_fn):size = len(dataloader.dataset)  # 测试集的大小,一共10000张图片num_batches = len(dataloader)  # 批次数目,313(10000/32=312.5,向上取整)test_loss = 0# 当不进行训练时,停止梯度更新,节省计算内存消耗with torch.no_grad():for imgs, target in dataloader:imgs, target = imgs.to(device), target.to(device)# 计算losstarget_pred = model(imgs)loss = loss_fn(target_pred, target)test_loss += loss.item()test_loss /= num_batchesreturn test_loss

4.正式训练 

loss_fn = nn.MSELoss() # 创建损失函数
learn_rate = 1e-4 # 学习率
opt = torch.optim.SGD(model.parameters(),lr=learn_rate)
epochs = 50train_loss = []
test_loss = []
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(opt,epochs,last_epoch=-1)for epoch in range(epochs):model.train()epoch_train_loss = train(train_dl, model, loss_fn, opt,lr_scheduler)model.eval()epoch_test_loss = t(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))
print('Done')
learning rate = 0.00010 Epoch: 1, Train_loss:0.21849, Test_loss:0.75929
learning rate = 0.00010 Epoch: 2, Train_loss:0.21115, Test_loss:0.74356
learning rate = 0.00010 Epoch: 3, Train_loss:0.20415, Test_loss:0.72837
learning rate = 0.00010 Epoch: 4, Train_loss:0.19749, Test_loss:0.71375
learning rate = 0.00010 Epoch: 5, Train_loss:0.19115, Test_loss:0.69969
learning rate = 0.00010 Epoch: 6, Train_loss:0.18514, Test_loss:0.68619
learning rate = 0.00010 Epoch: 7, Train_loss:0.17945, Test_loss:0.67325
learning rate = 0.00009 Epoch: 8, Train_loss:0.17406, Test_loss:0.66087
learning rate = 0.00009 Epoch: 9, Train_loss:0.16897, Test_loss:0.64903
learning rate = 0.00009 Epoch:10, Train_loss:0.16417, Test_loss:0.63775
learning rate = 0.00009 Epoch:11, Train_loss:0.15965, Test_loss:0.62699
learning rate = 0.00009 Epoch:12, Train_loss:0.15540, Test_loss:0.61676
learning rate = 0.00008 Epoch:13, Train_loss:0.15141, Test_loss:0.60704
learning rate = 0.00008 Epoch:14, Train_loss:0.14766, Test_loss:0.59782
learning rate = 0.00008 Epoch:15, Train_loss:0.14415, Test_loss:0.58910
learning rate = 0.00008 Epoch:16, Train_loss:0.14087, Test_loss:0.58084
learning rate = 0.00007 Epoch:17, Train_loss:0.13780, Test_loss:0.57305
learning rate = 0.00007 Epoch:18, Train_loss:0.13493, Test_loss:0.56571
learning rate = 0.00007 Epoch:19, Train_loss:0.13227, Test_loss:0.55881
learning rate = 0.00007 Epoch:20, Train_loss:0.12978, Test_loss:0.55233
learning rate = 0.00006 Epoch:21, Train_loss:0.12748, Test_loss:0.54625
learning rate = 0.00006 Epoch:22, Train_loss:0.12534, Test_loss:0.54057
learning rate = 0.00006 Epoch:23, Train_loss:0.12336, Test_loss:0.53526
learning rate = 0.00005 Epoch:24, Train_loss:0.12153, Test_loss:0.53033
learning rate = 0.00005 Epoch:25, Train_loss:0.11984, Test_loss:0.52574
learning rate = 0.00005 Epoch:26, Train_loss:0.11829, Test_loss:0.52150
learning rate = 0.00004 Epoch:27, Train_loss:0.11686, Test_loss:0.51757
learning rate = 0.00004 Epoch:28, Train_loss:0.11556, Test_loss:0.51397
learning rate = 0.00004 Epoch:29, Train_loss:0.11436, Test_loss:0.51066
learning rate = 0.00003 Epoch:30, Train_loss:0.11328, Test_loss:0.50763
learning rate = 0.00003 Epoch:31, Train_loss:0.11230, Test_loss:0.50488
learning rate = 0.00003 Epoch:32, Train_loss:0.11141, Test_loss:0.50239
learning rate = 0.00003 Epoch:33, Train_loss:0.11061, Test_loss:0.50015
learning rate = 0.00002 Epoch:34, Train_loss:0.10990, Test_loss:0.49815
learning rate = 0.00002 Epoch:35, Train_loss:0.10926, Test_loss:0.49636
learning rate = 0.00002 Epoch:36, Train_loss:0.10870, Test_loss:0.49479
learning rate = 0.00002 Epoch:37, Train_loss:0.10821, Test_loss:0.49342
learning rate = 0.00001 Epoch:38, Train_loss:0.10779, Test_loss:0.49223
learning rate = 0.00001 Epoch:39, Train_loss:0.10742, Test_loss:0.49121
learning rate = 0.00001 Epoch:40, Train_loss:0.10711, Test_loss:0.49035
learning rate = 0.00001 Epoch:41, Train_loss:0.10685, Test_loss:0.48964
learning rate = 0.00001 Epoch:42, Train_loss:0.10664, Test_loss:0.48906
learning rate = 0.00000 Epoch:43, Train_loss:0.10647, Test_loss:0.48860
learning rate = 0.00000 Epoch:44, Train_loss:0.10634, Test_loss:0.48825
learning rate = 0.00000 Epoch:45, Train_loss:0.10624, Test_loss:0.48799
learning rate = 0.00000 Epoch:46, Train_loss:0.10616, Test_loss:0.48781
learning rate = 0.00000 Epoch:47, Train_loss:0.10612, Test_loss:0.48769
learning rate = 0.00000 Epoch:48, Train_loss:0.10609, Test_loss:0.48763
learning rate = 0.00000 Epoch:49, Train_loss:0.10607, Test_loss:0.48760
learning rate = 0.00000 Epoch:50, Train_loss:0.10606, Test_loss:0.48760
Done

 五 Loss损失评估和模型预测评估

from datetime import datetime
current_time = datetime.now()plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      # 用来正常显示负号
plt.rcParams['figure.dpi'] = 100        #分辨率epochs_range = range(epochs)plt.figure(figsize=(12, 3))
plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, test_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.xlabel(current_time)
plt.title('Training and Validation Loss')
plt.show()

调用模型进行预测

'''
调用模型进行预测
'''
pred_y_lstm = sc.inverse_transform(model(X_test).detach().numpy().reshape(-1,1))
y_test_1 = sc.inverse_transform(y_test.reshape(-1,1))
y_test_one = [o[0] for o in y_test_1]
pred_y_lstm_one = [i[0] for i in pred_y_lstm]plt.figure(figsize=(8,6))
plt.plot(y_test_one[:2000],color='red',label='real_temp')
plt.plot(pred_y_lstm_one[:2000],color='blue',label='prediction')plt.title('Title')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()

 预测评估

'''
R2评估
'''
from sklearn import metricsRMSE_lstm = metrics.mean_squared_error(pred_y_lstm_one,y_test_one)**0.5
R2_lstm = metrics.r2_score(pred_y_lstm_one,y_test_one)print('均方差误差:%.5f'%RMSE_lstm)
print('R2:%.5f'%R2_lstm)
均方差误差:0.00001
R2:0.83518

http://www.ppmy.cn/ops/150720.html

相关文章

【PHP】双方接口通信校验服务

请求方 使用 ApiAuthService::buildUrl($domain, [terminal > 1, ts > time()]); //http://域名/adminapi/login/platformLogin?signF7FE8A150DEC18BE8A71C5059742C81A&terminal1&ts1736904841接收方 $getParams $this->request->get();$validate ApiA…

项目练习:若依管理系统字典功能-Vue前端部分

文章目录 一、情景说明二、若依Vue相关代码及配置1、utils代码2、components组件3、api接口代码4、Vuex配置5、main.js配置 三、使用方法1、html部分2、js部分 一、情景说明 我们在做web系统的时候&#xff0c;肯定会遇到一些常量选择场景。 比如&#xff0c;性别&#xff1a;…

Unity3D手机游戏发热发烫优化指南与技巧详解

前言 在移动游戏开发领域&#xff0c;游戏发热和发烫问题一直是影响用户体验的重要因素。对于Unity3D开发者来说&#xff0c;优化游戏以减少设备发热和提升性能至关重要。本文将详细介绍Unity3D手机游戏发热发烫的优化指南与技巧&#xff0c;并提供相应的技术详解和代码实现。…

GPT Notes 3.2.1.2 | 最强GPT解锁会员版无需登录无限制使用

GPT Notes是一款功能强大的AI智能软件&#xff0c;提供无需登录的永久会员体验&#xff0c;满足用户在工作和学习中的各种需求。它支持语音识别&#xff0c;帮助用户轻松撰写文章、解答问题&#xff0c;并具备语音转文字、图片识别等几十种AI功能。打开即享会员特权&#xff0c…

校园跑腿小程序--我的,登录和注册页面开发

hello hello~ &#xff0c;这里是 code袁~&#x1f496;&#x1f496; &#xff0c;欢迎大家点赞&#x1f973;&#x1f973;关注&#x1f4a5;&#x1f4a5;收藏&#x1f339;&#x1f339;&#x1f339; &#x1f981;作者简介&#xff1a;一名喜欢分享和记录学习的在校大学生…

华为OD上机考试真题(Java)——字符串分割

题目&#xff1a; 给定一个字符串&#xff0c;只包含小写字母&#xff0c;字符串长度是 5-30。 求&#xff1a;是否存在两个节点&#xff0c;使得字符串被这两个节点分成三个部分&#xff0c;每个部分的 ASCII 码的值之和都相等。如果存在输出两个节点下标&#xff0c;以逗号隔…

快速、可靠且高性价比的定制IP模式提升芯片设计公司竞争力

作者&#xff1a;Karthik Gopal&#xff0c;SmartDV Technologies亚洲区总经理 智权半导体科技&#xff08;厦门&#xff09;有限公司总经理 无论是在出货量巨大的消费电子市场&#xff0c;还是针对特定应用的细分芯片市场&#xff0c;差异化芯片设计带来的定制化需求也在芯片…

淘宝 URL 采集商品详情数据及开发

一、通过淘宝开放平台&#xff08;如果有资质&#xff09; 注册成为淘宝开发者 访问淘宝开放平台官方网站&#xff0c;按照要求填写开发者信息&#xff0c;包括企业或个人身份验证等步骤。这一步是为了获取合法的 API 使用权限。 了解商品详情 API 淘宝开放平台提供了一系列…