news 2026/4/18 3:57:14

DAY37 早停策略和模型权重的保存

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
DAY37 早停策略和模型权重的保存

@浙大疏锦行

importtorchimporttorch.nnasnnimporttorch.optimasoptimfromsklearn.datasetsimportload_irisfromsklearn.model_selectionimporttrain_test_splitimportnumpyasnp iris=load_iris()X=iris.data y=iris.target X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)print(X_train.shape)print(y_train.shape)print(X_test.shape)print(y_test.shape)fromsklearn.preprocessingimportMinMaxScaler scaler=MinMaxScaler()X_train=scaler.fit_transform(X_train)X_test=scaler.transform(X_test)X_train=torch.FloatTensor(X_train)y_train=torch.LongTensor(y_train)X_test=torch.FloatTensor(X_test)y_test=torch.LongTensor(y_test)importtorchimporttorch.nnasnnimporttorch.optimclassMLP(nn.Module):def__init__(self,*args,**kwargs):super().__init__(*args,**kwargs)self.fc1=nn.Linear(4,10)self.relu=nn.ReLU()self.fc2=nn.Linear(10,3)defforward(self,x):out=self.fc1(x)out=self.relu(out)out=self.fc2(out)returnout model=MLP()criterion=nn.CrossEntropyLoss()optimizer=torch.optim.SGD(model.parameters(),lr=0.01)num_epochs=20000losses=[]forepochinrange(num_epochs):outputs=model.forward(X_train)loss=criterion(outputs,y_train)# 预测损失# 反向传播和优化optimizer.zero_grad()loss.backward()# 反向传播计算梯度optimizer.step()losses.append(loss.item())if(epoch+1)%100==0:print(f'Epoch[{epoch+1}/{num_epochs}],Loss:{loss.item():.4f}')importmatplotlib.pyplotasplt plt.plot(range(num_epochs),losses)plt.xlabel('Epoch')plt.ylabel('Loss')plt.title('Training Loss over Epochs')plt.show()


fromtqdmimporttqdm# 注意:这里导入的是 tqdm 函数,不是模块train_losses=[]test_losses=[]epochs=[]# ===== 新增早停相关参数 =====best_test_loss=float('inf')# 记录最佳测试集损失best_epoch=0# 记录最佳epochpatience=50# 早停耐心值(连续多少轮测试集损失未改善时停止训练)counter=0# 早停计数器early_stopped=False# 是否早停标志# ==========================withtqdm(total=num_epochs,desc="训练进度",unit="epoch")aspbar:forepochinrange(num_epochs):outputs=model(X_train)train_loss=criterion(outputs,y_train)optimizer.zero_grad()train_loss.backward()optimizer.step()if(epoch+1)%200==0:model.eval()withtorch.no_grad():test_outputs=model(X_test)test_loss=criterion(test_outputs,y_test)model.train()train_losses.append(train_loss.item())test_losses.append(test_loss.item())epochs.append(epoch+1)pbar.set_postfix({'Train Loss':f'{train_loss.item():.4f}','Test Loss':f'{test_loss.item():.4f}'})# ===== 新增早停逻辑 =====iftest_loss.item()<best_test_loss:# 如果当前测试集损失小于最佳损失best_test_loss=test_loss.item()# 更新最佳损失best_epoch=epoch+1# 更新最佳epochcounter=0# 重置计数器# 保存最佳模型torch.save(model.state_dict(),'best_model.pth')else:counter+=1ifcounter>=patience:print(f"早停触发!在第{epoch+1}轮,测试集损失已有{patience}轮未改善。")print(f"最佳测试集损失出现在第{best_epoch}轮,损失值为{best_test_loss:.4f}")early_stopped=Truebreak# 终止训练循环# ======================# 每1000个epoch更新一次进度条if(epoch+1)%1000==0:pbar.update(1000)# 更新进度条ifpbar.n<num_epochs:pbar.update(num_epochs-pbar.n)# 可视化损失曲线plt.figure(figsize=(10,6))plt.plot(epochs,train_losses,label='Train Loss')# 原始代码已有plt.plot(epochs,test_losses,label='Test Loss')# 新增:测试集损失曲线plt.xlabel('Epoch')plt.ylabel('Loss')plt.title('Training and Test Loss over Epochs')plt.legend()# 新增:显示图例plt.grid(True)plt.show()# 在测试集上评估模型,此时model内部已经是训练好的参数了# 评估模型model.eval()# 设置模型为评估模式withtorch.no_grad():# torch.no_grad()的作用是禁用梯度计算,可以提高模型推理速度outputs=model(X_test)# 对测试数据进行前向传播,获得预测结果_,predicted=torch.max(outputs,1)# torch.max(outputs, 1)返回每行的最大值和对应的索引correct=(predicted==y_test).sum().item()# 计算预测正确的样本数accuracy=correct/y_test.size(0)print(f'测试集准确率:{accuracy*100:.2f}%')

torch.save(model.state_dict(),"model_weights.pth")model=MLP()model.load_state_dict(torch.load("model_weights.pth"))
torch.save(model,"full_model.pth")model=torch.load("full_model.pth",weights_only=False)model.eval()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:47:21

QSharedMemory 变量在对象析构的时候要怎么处理

在Qt中使用QSharedMemory时&#xff0c;对象析构时的正确处理至关重要&#xff0c;主要涉及共享内存的释放和跨进程同步问题。以下是关键处理原则和步骤&#xff1a; 1. 自动析构行为&#xff08;Qt内置机制&#xff09; 自动detach()&#xff1a;当QSharedMemory对象销毁时&…

作者头像 李华
网站建设 2026/4/14 16:51:51

【免费领源码】Python/Mysql数据库+53824中国传统服装微信小程序的设计与实现+ 计算机毕业设计项目推荐上万套实战教程JAVA、PHP,node.js,C++、python、大屏数据可视化

摘要 本文旨在探讨基于Spring Boot框架的中国传统服装微信小程序的设计与实现过程。该微信小程序旨在为用户提供一个便捷、高效的线上购物平台&#xff0c;专注于中国传统服装的展示与销售。通过深入分析用户需求和市场趋势&#xff0c;我们设计了包含用户管理、服装分类、热卖…

作者头像 李华
网站建设 2026/4/16 23:07:12

django用Python设计自主学习系统

目录 摘要 演示视频 系统功能实现 代码实现 推荐项目 项目案例 项目开发总结 为什么选择我 源码获取 博主介绍&#xff1a;✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于…

作者头像 李华
网站建设 2026/4/16 15:26:35

Linux的shell命令

1.基础的shell命令在Linux系统中不同于window中的图形化操作&#xff0c;linux更多的是用的命令行的操作&#xff0c;下面我们来看看其中的一些基础shell命令。首先我们看下面这段命令解释一下其中的提示符&#xff1a;linuxubuntu:~$ sudo su [sudo] linux 的密码&#xff1a;…

作者头像 李华