机器学习 - 提高模型 (代码)
如果模型出现了 underfitting 问题,就得提高模型了。
| Model improvement technique | What does it do? |
|---|---|
| Add more layers | Each layer potentially increases the learning capabilities of the model with each layer being able to learn some kind of new pattern in the data, more layers is often referred to as making your neural network deeper. |
| Add more hidden units | More hidden units per layer means a potential increase in learning capabilities of the model, more hidden units is often referred to as making your neural network wider. |
| Fitting for longer (more epochs) | Your model might learn more if it had more opportunities to look at the data. |
| Changing the activation functions | Some data just can’t be fit with only straight lines, using non-linear activation functions can help with this. |
| Change the learning rate | Less model specific, but still related, the learning rate of the optimizer decides how much a model should change its parameter each step, too much and the model overcorrects, too little and it doesn’t learn enough. |
| Change the loss function | Less model specific but still important, different problems require different loss functions. For example, a binary cross entropy loss function won’t work with a multi-class classification problem. |
| Use transfer learning | Take a pretrained model from a problem domain similar to yours and adjust it to your own problem. |
举个例子,代码如下:
class CircleModelV1(nn.Module):def __init__(self):super().__init__()self.layer_1 = nn.Linear(in_features = 2, out_features = 10)self.layer_2 = nn.Linear(in_features = 10, out_features = 10)self.layer_3 = nn.Linear(in_features = 10, out_features = 1)def forward(self, x):return self.layer_3(self.layer_2(self.layer_1(x)))model_1 = CircleModelV1().to("cpu")
print(model_1)loss_fn = nn.BCEWithLogitsLoss()
optimizer = torch.optim.SGD(model_1.parameters(), lr=0.1)torch.manual_seed(42)epochs = 1000X_train, y_train = X_train.to("cpu"), y_train.to("cpu")
X_test, y_test = X_test.to("cpu"), y_test.to("cpu")for epoch in range(epochs):### Training# 1. Forward pass y_logits = model_1(X_train).squeeze()y_pred = torch.round(torch.sigmoid(y_logits)) # logits -> probabilities -> prediction labels # 2. Calculate loss/accuracy loss = loss_fn(y_logits, y_train)acc = accuracy_fn(y_true = y_train, y_pred = y_pred)# 3. Optimizer zero grad optimizer.zero_grad()# 4. Loss backwards loss.backward()# 5. Optimizer step optimizer.step() ### Testing model_1.eval()with torch.inference_mode():# 1. Forward pass test_logits = model_1(X_test).squeeze()test_pred = torch.round(torch.sigmoid(test_logits))# 2. Calculate loss/accuracy test_loss = loss_fn(test_logits, y_test)test_acc = accuracy_fn(y_true = y_test, y_pred = test_pred)if epoch % 100 == 0:print(f"Epoch: {epoch} | Loss: {loss:.5f}, Accuracy: {acc:.2f}%")# 结果如下
CircleModelV1((layer_1): Linear(in_features=2, out_features=10, bias=True)(layer_2): Linear(in_features=10, out_features=10, bias=True)(layer_3): Linear(in_features=10, out_features=1, bias=True)
)
Epoch: 0 | Loss: 0.69528, Accuracy: 51.38%
Epoch: 100 | Loss: 0.69325, Accuracy: 47.88%
Epoch: 200 | Loss: 0.69309, Accuracy: 49.88%
Epoch: 300 | Loss: 0.69303, Accuracy: 50.50%
Epoch: 400 | Loss: 0.69300, Accuracy: 51.38%
Epoch: 500 | Loss: 0.69299, Accuracy: 51.12%
Epoch: 600 | Loss: 0.69298, Accuracy: 51.50%
Epoch: 700 | Loss: 0.69298, Accuracy: 51.38%
Epoch: 800 | Loss: 0.69298, Accuracy: 51.50%
Epoch: 900 | Loss: 0.69298, Accuracy: 51.38%
都看到这了,点个赞呗~
相关文章:
机器学习 - 提高模型 (代码)
如果模型出现了 underfitting 问题,就得提高模型了。 Model improvement techniqueWhat does it do?Add more layersEach layer potentially increases the learning capabilities of the model with each layer being able to learn some kind of new pattern in…...
数值代数及方程数值解:预备知识——二进制及浮点数
文章目录 二进制IEEE浮点数 本篇文章的前置知识:数学分析 二进制 命题:二进制转化为十进制 二进制的数字表示为 ⋯ b 2 b 1 b 0 . b − 1 b − 2 ⋯ \cdots b_2b_1b_0.b_{-1}b_{-2}\cdots ⋯b2b1b0.b−1b−2⋯这等价于十进制下的 ⋯ b 2 2 …...
新数字时代的启示:揭开Web3的秘密之路
在当今数字时代,随着区块链技术的不断发展,Web3作为下一代互联网的概念正逐渐引起人们的关注和探索。本文将深入探讨新数字时代的启示,揭开Web3的神秘之路,并探讨其在未来的发展前景。 1. Web3的定义与特点 Web3是对互联网未来发…...
算法——动态规划:01背包
原始01背包见下面这篇文章:http://t.csdnimg.cn/a1kCL 01背包的变种:. - 力扣(LeetCode) 给你一个 只包含正整数 的 非空 数组 nums 。请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。 简化一…...
写作类AI推荐(二)
本章要介绍的写作AI如下: 火山写作 主要功能: AI智能创作:告诉 AI 你想写什么,立即生成你理想中的文章AI智能改写:选中段落句子,可提升表达、修改语气、扩写、总结、缩写等文章内容优化:根据全文…...
分寝室(20分)(JAVA)
目录 题目描述 输入格式: 输出格式: 输入样例 1: 输出样例 1: 输入样例 2: 输出样例 2: 题解: 题目描述 学校新建了宿舍楼,共有 n 间寝室。等待分配的学生中,有女…...
Spring 源码调试问题 ( List.of(“bin“, “build“, “out“); )
Spring 源码调试问题 文章目录 Spring 源码调试问题一、问题描述二、解决方案 一、问题描述 错误:springframework\buildSrc\src\main\java\org\springframework\build\CheckstyleConventions.java:68: 错误: 找不到符号 List<String> buildFolders List.of…...
Centos7安装RTL8111网卡驱动
方法一: // 安装pciutils # yum install -y pciutils // 查看pci设备信息 # lspci | grep -i Ethernet 09:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 03) // 上面看到是Re…...
吉时利KEITHLEY2460数字源表
181/2461/8938产品概述: Keithley 2460 高电流源表源测量单元 (SMU) 将先进的触摸、测试和发明技术带到您的指尖。Keithley 2460 将创新的图形用户界面 (GUI) 与电容式触摸屏技术相结合,使测试变得直观并最大限度地缩短学习曲线,从而帮助工程…...
数据库原理(含思维导图)
数据库原理笔记,html与md笔记已上传 1.绪论 发展历程 记住数据怎么保存,谁保存数据,共享性如何,独立性如何 人工管理阶段 数据不保存应用程序管理数据数据不共享数据不具有独立性 文件系统阶段 数据可以长期保存文件系统管…...
数据结构(六)——图
六、图 6.1 图的基本概念 图的定义 图:图G由顶点集V和边集E组成,记为G (V, E),其中V(G)表示图G中顶点的有限非空集;E(G) 表示图G中顶点之间的关系(边)集合。若V {v1, v2, … , vn},则用|V|…...
Android-AR眼镜屏幕显示
Android-AR眼镜 前提:Android手持设备 需要具备DP高清口 1、创建Presentation(双屏异显) public class MyPresentation extends Presentation {private PreviewSingleBinding binding;private ScanActivity activity;public MyPresentatio…...
蓝桥集训之货币系统
蓝桥集训之货币系统 核心思想:背包 #include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N 30,M 10010;typedef long long LL;LL f[M];int w[N];int n,m;int main(){cin>>n>>m;for(int i1;i&…...
基于微信小程序的校园服务平台设计与实现(程序+论文)
本文以校园服务平台为研究对象,首先分析了当前校园服务平台的研究现状,阐述了本系统设计的意义和背景,运用微信小程序开发工具和云开发技术,研究和设计了一个校园服务平台,以满足学生在校园生活中的多样化需求。通过引…...
QT+Opencv+yolov5实现监测
功能说明:使用QTOpencvyolov5实现监测 仓库链接:https://gitee.com/wangyoujie11/qt_yolov5.git git本仓库到本地 一、环境配置 1.opencv配置 将OpenCV-MinGW-Build-OpenCV-4.5.2-x64文件夹放在自己的一个目录下,如我的路径: …...
【Python-Docx库】Word与Python的完美结合
【Python-Docx库】Word与Python的完美结合 今天给大家分享Python处理Word的第三方库:Python-Docx。 什么是Python-Docx? Python-Docx是用于创建和更新Microsoft Word(.docx)文件的Python库。 日常需要经常处理Word文档…...
吴恩达深度学习笔记:浅层神经网络(Shallow neural networks)3.6-3.8
目录 第一门课:神经网络和深度学习 (Neural Networks and Deep Learning)第三周:浅层神经网络(Shallow neural networks)3.6 激活函数(Activation functions)3.7 为什么需要非线性激活函数?(why need a non…...
盘点最适合做剧场版的国漫,最后一部有望成为巅峰
最近《完美世界》动画官宣首部剧场版,主要讲述石昊和火灵儿的故事。这个消息一出,引发了很多漫迷的讨论,其实现在已经有好几部国漫做过剧场版,还有是观众一致希望未来会出剧场版的。那么究竟是哪些国漫呢,下面就一起来…...
Altium Designer许可需求分析
在电子设计的世界中,Altium Designer已成为设计师们的得力助手。然而,如何进行有效的许可需求分析,以确保软件的高效使用和企业的可持续发展?本文将带您了解如何进行Altium Designer的许可需求分析,让您在设计的道路上…...
[c++]类和对象常见题目详解
本专栏内容为:C学习专栏,分为初阶和进阶两部分。 通过本专栏的深入学习,你可以了解并掌握C。 💓博主csdn个人主页:小小unicorn ⏩专栏分类:C 🚚代码仓库:小小unicorn的代码仓库&…...
【SpringBoot】100、SpringBoot中使用自定义注解+AOP实现参数自动解密
在实际项目中,用户注册、登录、修改密码等操作,都涉及到参数传输安全问题。所以我们需要在前端对账户、密码等敏感信息加密传输,在后端接收到数据后能自动解密。 1、引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId...
使用分级同态加密防御梯度泄漏
抽象 联邦学习 (FL) 支持跨分布式客户端进行协作模型训练,而无需共享原始数据,这使其成为在互联和自动驾驶汽车 (CAV) 等领域保护隐私的机器学习的一种很有前途的方法。然而,最近的研究表明&…...
【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验
系列回顾: 在上一篇中,我们成功地为应用集成了数据库,并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了!但是,如果你仔细审视那些 API,会发现它们还很“粗糙”:有…...
GitHub 趋势日报 (2025年06月08日)
📊 由 TrendForge 系统生成 | 🌐 https://trendforge.devlive.org/ 🌐 本日报中的项目描述已自动翻译为中文 📈 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...
项目部署到Linux上时遇到的错误(Redis,MySQL,无法正确连接,地址占用问题)
Redis无法正确连接 在运行jar包时出现了这样的错误 查询得知问题核心在于Redis连接失败,具体原因是客户端发送了密码认证请求,但Redis服务器未设置密码 1.为Redis设置密码(匹配客户端配置) 步骤: 1).修…...
python报错No module named ‘tensorflow.keras‘
是由于不同版本的tensorflow下的keras所在的路径不同,结合所安装的tensorflow的目录结构修改from语句即可。 原语句: from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后: from tensorflow.python.keras.lay…...
在QWebEngineView上实现鼠标、触摸等事件捕获的解决方案
这个问题我看其他博主也写了,要么要会员、要么写的乱七八糟。这里我整理一下,把问题说清楚并且给出代码,拿去用就行,照着葫芦画瓢。 问题 在继承QWebEngineView后,重写mousePressEvent或event函数无法捕获鼠标按下事…...
【网络安全】开源系统getshell漏洞挖掘
审计过程: 在入口文件admin/index.php中: 用户可以通过m,c,a等参数控制加载的文件和方法,在app/system/entrance.php中存在重点代码: 当M_TYPE system并且M_MODULE include时,会设置常量PATH_OWN_FILE为PATH_APP.M_T…...
为什么要创建 Vue 实例
核心原因:Vue 需要一个「控制中心」来驱动整个应用 你可以把 Vue 实例想象成你应用的**「大脑」或「引擎」。它负责协调模板、数据、逻辑和行为,将它们变成一个活的、可交互的应用**。没有这个实例,你的代码只是一堆静态的 HTML、JavaScript 变量和函数,无法「活」起来。 …...
【学习笔记】erase 删除顺序迭代器后迭代器失效的解决方案
目录 使用 erase 返回值继续迭代使用索引进行遍历 我们知道类似 vector 的顺序迭代器被删除后,迭代器会失效,因为顺序迭代器在内存中是连续存储的,元素删除后,后续元素会前移。 但一些场景中,我们又需要在执行删除操作…...
