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

机器学习 - 神经网络分类

什么叫做分类问题?
A classification problem involves predicting whether something is one thing or another.

Problem typeWhat is it?Example
Binary classificationTarget can be one of two options, e.g. yes or noPredict whether or not someone has heart disease based on their health parameters.
Multi-class classificationTarget can be one of more than two optionsDecide whether a photo is of food, a person or a dog
Multi-label classificationTarget can be assigned more than one optionPredict what categories should be assigned to a Wikipedia article (e.g. mathematics, science & philosophy).

做 classification problem 步骤:

  1. Architecture of a classification neural network
  2. Getting binary classification data ready
  3. Building a PyTorch classification model
  4. Fitting the model to data (training)
  5. Making predictions and evaluating a model (inference)
  6. Improving a model (from a model perspective)
  7. Non-linearity
  8. Replicating non-linear functions
  9. Putting it all together with multi-class classification

Architecture of a classification neural network

HyperparameterBinary ClassificationMulticlass classification
Input layer shape (in_features)Same as number of features (e.g. 5 for age, sex, height, weight, smoking status in heart disease prediction)Same as binary classification
Hidden layer(s)Problem specific, minimum = 1, maximum = unlimitedSame as binary classification
Neurons per hidden layerProblem specific, generally 10 to 512Same as binary classification
Output layer shape (out_features)1 (one class or the other)1 per class (e.g. 3 for food, person or dog photo)
Hidden layer activationUsually ReLU (rectified linear unit) but can be many othersSame as binary classification
Output activationSigmoid (torch.sigmoid in PyTorch)Softmax (torch.softmax in PyTorch)
Loss functionBinary crossentropy (torch.nn.BCELoss in PyTorch)Cross entropy (torch.nn.CrossEntropy Loss in PyTorch)
OptimizerSGD (stochastic gradient descent), Adam (see torch.optim for more options)Same as binary classification

ReLU 函数定义:f(x) = max(0, x),x为输入
该函数特点:

  1. 非线性:尽管ReLU函数在 x <= 0 时输出固定值为零,但在 x > 0 时输出与输入成正比,因此具有非线性特性,有助于神经网络学习复杂的非线性关系。
  2. 稀疏性:在神经网络的训练过程中,由于ReLU函数在 x <= 0 时输出为零,因此某些神经元会被“关闭”,这意味着它们不会对网络的输出产生影响,使得网络的稀疏性增加,有助于减少过拟合。
  3. 解决梯度消失问题:ReLU 函数在正区间的梯度始终为1,不会出现梯度消失的问题,有助于缓解梯度消失问题,提高训练的稳定性和速度。

Make classification data and get it ready

创建一些数据

from sklearn.datasets import make_circles # Make 1000 samples
n_samples = 1000# Create circles
X, y = make_circles(n_samples,noise = 0.03,random_state = 42)
print(f"X里的前5个数:\n{X[:5]}")
print(f"y里的前5个数:\n{y[:5]}")# 结果如下
X里的前5个数:
[[ 0.75424625  0.23148074][-0.75615888  0.15325888][-0.81539193  0.17328203][-0.39373073  0.69288277][ 0.44220765 -0.89672343]]
y里的前5个数:
[1 1 1 1 0]

make_circles 函数用于生成一个包含两个圆形类别的二分类数据集,其中一个类别位于另一个类别的内部。
在 make_circles 函数中,noise参数用于控制生成的数据集中噪声的程度。noise参数的值在[0,1]范围内,表示生成的数据中随机噪声的标准差,即数据点在生成圆形分类时的偏移程度。noise 参数控制了数据点在生成时的偏移程度,从而影响了生成的数据集的分布情况。当 noise 参数较小 (接近0)时,生成的数据点更加紧密地分布在圆形区域内,而当 noise 参数较大 (接近1)时,生成地数据点可能会有较大地偏移,甚至出现在不同类别地区域内。

这里稍微介绍什么时“噪声”? 在数据领域中,“噪声”通常指的是在数据中存在的不希望或不相关的信息。这些信息可能是由于数据收集过程中的各种因素引入的随机性,错误或干扰造成的。噪声可能会包括几种类型:随机噪声,错误噪声,干扰噪声,不相关噪声。

随机噪声:由于测量或采样过程中的不确定性引起的随机波动。例如:图像中的图像噪声等。
错误噪声:由于设备故障,数据输入错误或其他技术问题引起的错误数据。例如:传输过程中的数据丢失等。
干扰噪声:来自外部环境的干扰信号或干扰源引入的数据干扰。例如:电磁干扰。
不相关噪声:数据中存在的与目标任务无关的信息。


将上面的代码放到 pandas 里的 DataFrame 更清楚的查看关系

import pandas as pd
circles = pd.DataFrame({"X1": X[:, 0],"X2": X[:, 1],"label": y
})
print(circles.head(10))# 结果如下X1        X2  label
0  0.754246  0.231481      1
1 -0.756159  0.153259      1
2 -0.815392  0.173282      1
3 -0.393731  0.692883      1
4  0.442208 -0.896723      0
5 -0.479646  0.676435      1
6 -0.013648  0.803349      1
7  0.771513  0.147760      1
8 -0.169322 -0.793456      1
9 -0.121486  1.021509      0

通过这结果,每一个X有两个features (X1 和 X2),并且对应一个label值。label值不是1就是0,说明这是 binary classification.

为了确定X1和X2的数量是否相同

print(circles.label.value_counts())# 结果如下:
1    500
0    500
Name: label, dtype: int64# 两个features的数量一致

将数据显示

import matplotlib.pyplot as plt plt.scatter(x = X[:, 0],y = X[:, 1],c = y,cmap = plt.cm.RdYlBu)

图形如下:
图形

都看到这了,给个赞支持一下呗~

相关文章:

  • 【牛客】SQL146 0级用户高难度试卷的平均用时和平均得分
  • HashMap---数据结构
  • 开发npm上传发布
  • 华为OD技术面算法题整理
  • 家庭网络防御系统搭建-生产要素准备
  • 前端基础 Vue -组件化基础
  • 开始喜欢上了runnergo,JMeter out了?
  • 【物联网】Qinghub Kafka 数据采集
  • mysql 存储过程示例
  • 谈一谈BEV和Transformer在自动驾驶中的应用
  • FPGA电平标准
  • Mac电脑虚拟显示器:BetterDisplay Pro for Mac v2.0.11激活版
  • java Web餐馆订单管理系统用eclipse定制开发mysql数据库BS模式java编程jdbc
  • Vscode与Cmake搭配配置opencv使用
  • 使用Spring Boot Admin监控和管理Spring Boot应用程序
  • 收藏网友的 源程序下载网
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • CODING 缺陷管理功能正式开始公测
  • CSS 三角实现
  • Docker容器管理
  • echarts花样作死的坑
  • EventListener原理
  • JavaWeb(学习笔记二)
  • js ES6 求数组的交集,并集,还有差集
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • SegmentFault 2015 Top Rank
  • vue-router的history模式发布配置
  • vue脚手架vue-cli
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 回顾2016
  • 力扣(LeetCode)22
  • 深度学习中的信息论知识详解
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 译有关态射的一切
  • 最近的计划
  • C# - 为值类型重定义相等性
  • Spring Batch JSON 支持
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #Linux(帮助手册)
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (1) caustics\
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (3)llvm ir转换过程
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (pytorch进阶之路)扩散概率模型
  • (第二周)效能测试
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (转)JAVA中的堆栈