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

多层感知机

1.案例描述构建一个多层感知机用于二分类。其中超参数用贝叶斯优化器数据生借助scikit-learn实现。2.代码实现# -*- coding:utf-8 -*- import torch import torch.nn as nn import torch.optim as optim import numpy as np from torch.utils.data import TensorDataset, DataLoader from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score from bayes_opt import BayesianOptimization best_accuracy 0.0 best_model None device torch.device(cuda if torch.cuda.is_available() else cpu) print(f设备: {device}) def get_data(): X, y make_classification(n_samples10000, n_features20, n_informative15, n_redundant5) X_train, X_test, y_train, y_test train_test_split( X, y, test_size0.2, random_state42 ) scaler StandardScaler() X_train scaler.fit_transform(X_train) X_test scaler.transform(X_test) X_train torch.tensor(X_train, dtypetorch.float32).to(device) X_test torch.tensor(X_test, dtypetorch.float32).to(device) y_train torch.tensor(y_train, dtypetorch.long).to(device) y_test torch.tensor(y_test, dtypetorch.long).to(device) return X_train, X_test, y_train, y_test class MLP(nn.Module): def __init__(self, input_dim, hidden1, hidden2, dropout_rate): super(MLP, self).__init__() self.model nn.Sequential( nn.Linear(input_dim, hidden1), nn.ReLU(), nn.Dropout(dropout_rate), nn.Linear(hidden1, hidden2), nn.ReLU(), nn.Dropout(dropout_rate), nn.Linear(hidden2, 2) ) def forward(self, x): return self.model(x) def train_mlp(hidden1, hidden2, lr, dropout_rate, weight_decay, batch_size): global best_accuracy, best_model hidden1 int(hidden1) hidden2 int(hidden2) batch_size int(batch_size) X_train, X_test, y_train, y_test get_data() input_dim X_train.shape[1] train_dataset TensorDataset(X_train, y_train) train_loader DataLoader(train_dataset, batch_sizebatch_size, shuffleTrue) model MLP(input_dim, hidden1, hidden2, dropout_rate).to(device) criterion nn.CrossEntropyLoss() optimizer optim.Adam(model.parameters(), lrlr, weight_decayweight_decay) model.train() epochs 20 for epoch in range(epochs): for data, label in train_loader: optimizer.zero_grad() output model(data) loss criterion(output, label) loss.backward() optimizer.step() model.eval() with torch.no_grad(): y_pred model(X_test).argmax(dim1).cpu().numpy() y_true y_test.cpu().numpy() acc accuracy_score(y_true, y_pred) if acc best_accuracy: best_accuracy acc best_model model print(f\n发现更优模型准确率: {acc:.4f}) return acc def bayesian_optimization(): pbounds { hidden1: (32, 256), hidden2: (16, 128), lr: (1e-4, 1e-2), dropout_rate: (0.1, 0.5), weight_decay: (1e-5, 1e-3), batch_size: (32, 256), } optimizer BayesianOptimization( ftrain_mlp, pboundspbounds, random_state42, verbose2 ) optimizer.maximize(init_points3, n_iter10) print(\n *50) print(贝叶斯优化完成) print(f最佳测试准确率: {optimizer.max[target]:.4f}) print(f最佳超参数: {optimizer.max[params]}) print(*50) return optimizer.max def save_best_model(): global best_model if best_model is not None: torch.save(best_model.state_dict(), best_mlp_model.pth) print(\n最优模型已保存best_mlp_model.pth) print(f最优模型准确率: {best_accuracy:.4f}) else: print(未找到最优模型) if __name__ __main__: best_result bayesian_optimization() save_best_model()3.运行效果设备: cuda | iter | target | hidden1 | hidden2 | lr | dropou... | weight... | batch_... | ------------------------------------------------------------------------------------------------- 发现更优模型准确率: 0.9785 | 1 | 0.9785 | 115.89698 | 122.48000 | 0.0073467 | 0.3394633 | 0.0001644 | 66.942772 | | 2 | 0.9725 | 45.010729 | 113.01172 | 0.0060510 | 0.3832290 | 3.037e-05 | 249.25980 | 发现更优模型准确率: 0.9825 | 3 | 0.9825 | 218.46715 | 39.781980 | 0.0019000 | 0.1733618 | 0.0003111 | 149.54544 | | 4 | 0.981 | 227.22424 | 45.700258 | 0.0019841 | 0.4144344 | 0.0004332 | 137.46784 | | 5 | 0.979 | 224.08165 | 19.261477 | 0.0073002 | 0.2001550 | 0.0002476 | 206.83952 | 发现更优模型准确率: 0.9870 | 6 | 0.987 | 170.35254 | 32.767894 | 0.0066508 | 0.2900235 | 0.0003652 | 143.34584 | | 7 | 0.98 | 169.70364 | 32.021540 | 0.0076808 | 0.3398859 | 0.0003341 | 143.18209 | | 8 | 0.968 | 166.19593 | 101.15967 | 0.0001455 | 0.3755693 | 7.709e-05 | 78.100896 | | 9 | 0.978 | 171.67927 | 30.865531 | 0.0056182 | 0.2547844 | 0.0009247 | 140.13546 | | 10 | 0.978 | 167.98075 | 38.545550 | 0.0076755 | 0.4065293 | 0.0007287 | 143.05541 | | 11 | 0.984 | 170.96096 | 32.689496 | 0.0049800 | 0.2569683 | 0.0002752 | 143.37508 | | 12 | 0.9845 | 217.57055 | 35.619517 | 0.0054640 | 0.2074799 | 0.0005053 | 150.93606 | 发现更优模型准确率: 0.9920 | 13 | 0.992 | 216.52372 | 38.614819 | 0.0061758 | 0.2539666 | 0.0001237 | 154.98065 | 贝叶斯优化完成 最佳测试准确率: 0.9920 最佳超参数: {hidden1: np.float64(216.52372265940144), hidden2: np.float64(38.61481936855262), lr: np.float64(0.006175809340190158), dropout_rate: np.float64(0.2539666671286065), weight_decay: np.float64(0.00012376210103301903), batch_size: np.float64(154.98065397036225)} 最优模型已保存best_mlp_model.pth 最优模型准确率: 0.9920
http://www.rkmt.cn/news/1374074.html

相关文章:

  • 2026年紫外线杀菌除藻灯优质厂家深度解析:聚焦技术、产能与服务三角 - 2026年企业推荐榜
  • ubuntu2026.04部署k8s1.36版本的傻瓜式教程(注:运行时为docker,网络插件为calico)
  • DFT笔记59
  • 高通骁龙处理器深度解码:从移动通信霸主到全场景智能计算引擎
  • 告别SSH断连焦虑:手把手教你用Screen在Linux后台挂起任务(含源码编译避坑)
  • Win7专业版电脑重启后时间服务总停止?三步设置让它稳定运行(附命令详解)
  • 鸿蒙数理体系创作说明 (鸿蒙数学一阶完结后更新说明)
  • 在CentOS7服务器上装Win10?手把手教你用Ventoy搞定双系统(附网卡驱动安装避坑指南)
  • 2026年知名的大豆定量包装机/饲料定量包装机厂家哪家好 - 行业平台推荐
  • 2026靠谱仪器推荐:Trim200离子束刻蚀机、Essent Optics分光光度计、LINZA分光光度计、LensCheck MTF传函仪选择指南 - 优质品牌商家
  • Vibing Steampunk,一座把 Claude Code、MCP 和 SAP ADT 接到一起的 ABAP 工程桥
  • 2026北京搬家公司优质推荐指南:北京公司搬家公司/北京收纳整理公司/北京日式搬家公司/北京本地搬家/北京企业搬家/选择指南 - 优质品牌商家
  • Codex入门17-上下文管理(高手秘技:如何让AI精准理解你的百万行大型项目)
  • Zynq的QSPI Flash替换为GD25Q32故障排查
  • 问题分析-并网逆变器炸机问题
  • 别再死记硬背了!用Python代码一次性搞懂曼哈顿、欧式、切比雪夫距离的底层联系
  • 2026免费在线去水印软件推荐,手把手教你5种方法,第三种0.3秒搞定!
  • 2026保姆级免费去图片水印App教程,一键无痕去除,这4款微信小程序最省心
  • 2026最好用的图片处理工具推荐:去水印 / 抠图 / 高清化实测对比
  • Claude Code 接入 DeepSeek
  • 2026专业音响设备应用白皮书文体场馆选型剖析:ZOBO音响、舞台音响、Montarbo音响、Nettuno音响选择指南 - 优质品牌商家
  • 焊接钢格板哪个好?厂家怎么选?实用推荐来啦!
  • 保姆级教程!零代码搞定学生考勤高危群体画像分析(指标卡 + 饼图 + 仪表盘,附完整操作步骤)
  • 告别.bash_profile:在macOS Ventura/Sonoma上为Maven配置环境变量的几种新方法(含Zsh教程)
  • Win10桌面右键新建菜单丢了记事本?别慌,手把手教你用注册表找回来(附权限设置详解)
  • C51开发中寄存器变量限制与优化策略
  • VMware虚拟机里装FydeOS,给旧电脑或MacBook找个轻量‘副系统’
  • AR项目想拿高分?试试用Vuforia虚拟按钮做交互:从选图到避坑全流程
  • 别再让Ubuntu卡成PPT!手把手教你用swapfile把交换空间从1G扩容到64G(附权限修复)
  • 2026年热门的无锡污水污泥脱水机源头工厂推荐 - 品牌宣传支持者