﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>机器学习暑假班 &#8211; 学术创新中心</title>
	<atom:link href="https://www.leexinghai.com/aic/category/zafu/workspace/mlsh/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.leexinghai.com/aic</link>
	<description>Academic Innovation Center</description>
	<lastBuildDate>Sat, 13 Sep 2025 05:49:59 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.leexinghai.com/aic/wp-content/uploads/2025/08/cropped-徽标名称-32x32.jpg</url>
	<title>机器学习暑假班 &#8211; 学术创新中心</title>
	<link>https://www.leexinghai.com/aic</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>4.3卷积神经网络ALEXNET</title>
		<link>https://www.leexinghai.com/aic/4-3%e5%8d%b7%e7%a7%af%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9calexnet/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Fri, 29 Aug 2025 01:05:54 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3339</guid>

					<description><![CDATA[ALEXNET相对于LeNet，做出了五个改进： 首先，我们来看看什么是ALEXNET： 1. AlexNet [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>ALEXNET相对于LeNet，做出了五个改进：</p>



<ol class="wp-block-list">
<li>ReLu函数</li>



<li>最大池化</li>



<li>随机丢弃</li>



<li>数据扩增</li>



<li>GPU并行训练</li>
</ol>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="899" height="621" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-73.png" alt="" class="wp-image-3340" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-73.png 899w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-73-300x207.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-73-768x531.png 768w" sizes="(max-width: 899px) 100vw, 899px" /></figure>



<p>首先，我们来看看什么是ALEXNET：</p>



<h2 class="wp-block-heading">1. AlexNet 的基本情况</h2>



<ul class="wp-block-list">
<li><strong>作者</strong>：Alex Krizhevsky、Ilya Sutskever（Hinton 的学生们）</li>



<li><strong>提出时间</strong>：2012 年</li>



<li><strong>数据集</strong>：ImageNet（120 万张图像，1000 个类别）</li>



<li><strong>规模</strong>：约 650,000 个神经元、6000 万参数、6500 万连接</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">2. 网络结构（典型版）</h2>



<p>输入是 227×227×3彩色图像，网络包含 <strong>8 层可训练的层</strong>（5 个卷积层 + 3 个全连接层）：</p>



<ol class="wp-block-list">
<li><strong>Conv1</strong>：96 个 11×11 卷积核，stride=4 → 输出 55×55×96</li>



<li><strong>Max Pooling</strong>：3×3, stride=2 → 27×27×96</li>



<li><strong>Conv2</strong>：256 个 5×5 卷积核 → 27×27×256</li>



<li><strong>Max Pooling</strong>：3×3, stride=2 → 13×13×256</li>



<li><strong>Conv3</strong>：384 个 3×3 卷积核 → 13×13×384</li>



<li><strong>Conv4</strong>：384 个 3×3卷积核 → 13×13×384</li>



<li><strong>Conv5</strong>：256 个 3×3卷积核 → 13×13×256</li>



<li><strong>Max Pooling</strong>：3×3, stride=2 → 6×6×256</li>



<li><strong>FC1</strong>：全连接层，4096 个神经元</li>



<li><strong>FC2</strong>：全连接层，4096 个神经元</li>



<li><strong>FC3</strong>：全连接层，1000 个神经元（对应 1000 个类别，Softmax 输出）</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">3. AlexNet 的关键创新</h2>



<ul class="wp-block-list">
<li><strong>使用 ReLU 激活函数</strong>（代替传统的 Sigmoid/Tanh，加快收敛速度）。</li>



<li><strong>使用 Dropout</strong>（随机丢弃神经元，防止过拟合）。</li>



<li><strong>数据增强</strong>（图像平移、翻转、颜色扰动，扩大训练集有效规模）。</li>



<li><strong>GPU 并行训练</strong>（首次把 GPU 引入大规模卷积神经网络训练，大大提高训练速度）。</li>



<li><strong>最大池化</strong>（突出最显著的特征，对背景噪声更鲁棒）。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">4. 影响</h2>



<ul class="wp-block-list">
<li>在 2012 ILSVRC 比赛中，Top-5 错误率仅 15.3%，而第二名是 26.2%，差距非常大。</li>



<li>AlexNet 的成功让业界认识到：<strong>深度卷积神经网络在大规模数据和 GPU 加速下，能远超传统方法</strong>。</li>



<li>后续的 VGG、GoogLeNet、ResNet 都是在 AlexNet 的基础上发展起来的。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<p>接下来让我们分别看看每一个创新点</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<h2 class="wp-block-heading has-text-align-center">1.ReLu函数</h2>



<p>在 <strong>AlexNet</strong> 里用的 <strong>ReLU（Rectified Linear Unit，修正线性单元）</strong>，是它最重要的创新之一。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">1. 传统激活函数的问题</h3>



<p>在 AlexNet 之前，常见的非线性激活函数是 <strong>Sigmoid</strong> 和 <strong>tanh</strong>：</p>



<p><strong>Sigmoid</strong>：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>σ</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mfrac><mn>1</mn><mrow><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac></math>



<p>，输出在(0,1) 之间</p>



<p><strong>tanh</strong>：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>tanh</mi><mo data-mjx-texclass="NONE">⁡</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>−</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac></math>



<p>，输出在 (−1,1)之间</p>



<p>这两种函数的主要缺点：</p>



<ol class="wp-block-list">
<li><strong>计算复杂</strong>：涉及指数运算，早期计算资源有限，效率不高。</li>



<li><strong>梯度消失</strong>：在输入很大或很小时，函数会饱和（趋近于常数），导数接近 0，导致梯度几乎传不下去 → 深层网络难以训练。</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2. ReLU 的定义</h3>



<p>f(x)=max⁡(0,x)</p>



<p>非常简单：小于 0 的直接变成 0，大于 0 的保持原值。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3. ReLU 的优点</h3>



<ul class="wp-block-list">
<li><strong>计算高效</strong>：只需比较和截断，比 sigmoid/tanh 快得多。</li>



<li><strong>缓解梯度消失</strong>：在 x&gt;0 区域，梯度恒为 1，可以把误差有效传递到深层。</li>



<li><strong>稀疏激活</strong>：当 x&lt;0 时输出 0，很多神经元不激活，使网络稀疏化，减少参数依赖，提高泛化能力。</li>



<li><strong>收敛速度快</strong>：在 AlexNet 的实验里，ReLU 网络的收敛速度比 tanh 快 6 倍以上。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4. ReLU 的小问题与变种</h3>



<ul class="wp-block-list">
<li><strong>“神经元死亡”</strong>：如果某个神经元长期输入为负，就一直输出 0，不再更新。</li>



<li>解决办法：出现了 <strong>Leaky ReLU、Parametric ReLU、ELU</strong> 等变体，在负半轴给一个小斜率，而不是完全为 0。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<h2 class="wp-block-heading has-text-align-center">2.最大池化MaxPooling</h2>



<h3 class="wp-block-heading">1. 什么是池化？</h3>



<p>池化就是在 <strong>特征图的局部区域</strong> 内，按照某种规则把多个像素“聚合”为一个值，从而：</p>



<ul class="wp-block-list">
<li><strong>缩小特征图尺寸</strong>（降采样）</li>



<li><strong>减少计算量</strong></li>



<li><strong>增强模型对平移和局部变化的鲁棒性</strong></li>
</ul>



<p>简单理解就是：</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>把邻近像素看作一个“小池子”，然后用池化函数（如取最大值/平均值）来代表这一片区域。</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2. 常见池化方法</h3>



<h4 class="wp-block-heading">(1) <strong>平均池化 (Average Pooling)</strong></h4>



<ul class="wp-block-list">
<li>输出 = 窗口内所有像素的平均值</li>



<li>LeNet-5 使用的就是这种方式</li>



<li>特点：保留整体趋势，但细节容易被“冲淡”</li>
</ul>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>y</mi><mo>=</mo><mfrac><mn>1</mn><msup><mi>k</mi><mn>2</mn></msup></mfrac><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>k</mi></mrow></munderover><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>j</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>k</mi></mrow></munderover><msub><mi>x</mi><mrow><mi>i</mi><mi>j</mi></mrow></msub></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">(2) <strong>最大池化 (Max Pooling)</strong></h4>



<ul class="wp-block-list">
<li>输出 = 窗口内像素的最大值</li>



<li>AlexNet 采用的方式</li>



<li>特点：突出最显著的特征（如边缘、角点），对背景噪声更鲁棒</li>
</ul>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>y</mi><mo>=</mo><munder><mo data-mjx-texclass="OP" movablelimits="true">max</mo><mrow><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>∈</mo><mtext>window</mtext></mrow></munder><msub><mi>x</mi><mrow><mi>i</mi><mi>j</mi></mrow></msub></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading">(3) <strong>全局平均池化 (Global Average Pooling, GAP)</strong></h4>



<ul class="wp-block-list">
<li>对整个特征图取平均，得到单个数值</li>



<li>GoogLeNet 开始流行，用于代替全连接层，减少参数</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3. 池化的作用</h3>



<ol class="wp-block-list">
<li><strong>降采样</strong>：减少特征图大小和参数量</li>



<li><strong>平移不变性</strong>：即使目标在图像里稍微移动，池化后的特征变化不大</li>



<li><strong>抑制噪声</strong>：尤其是最大池化，能忽略弱特征，强化显著特征</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4. LeNet vs AlexNet 的对比</h3>



<ul class="wp-block-list">
<li><strong>LeNet (1998)</strong>：用 <strong>平均池化 (average pooling)</strong>，更偏“平滑”</li>



<li><strong>AlexNet (2012)</strong>：改用 <strong>最大池化 (max pooling)</strong>，保留了更强的边缘和纹理特征，识别效果显著提升。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<h2 class="wp-block-heading has-text-align-center">3.随机丢弃Dropout</h2>



<p><strong>Dropout（随机失活/随机丢弃）</strong> 是 AlexNet 中首次大规模应用的一种正则化方法，提出的动机是为了缓解深度神经网络容易出现的 <strong>过拟合</strong> 问题。它的做法很直观：在训练过程中，按照一定概率 p（通常取 0.5）随机“丢弃”部分神经元及其连接，不让它们参与本次前向传播和反向传播，相当于临时把这些神经元屏蔽掉。这样每一次训练迭代时，网络结构都不完全一样，更像是训练了很多不同的子网络。由于这些子网络共享权重，最后整体效果相当于在做一种“模型集成”，能显著提高网络的泛化能力。</p>



<p>在实际实现时，Dropout 只在训练阶段起作用，测试阶段则恢复所有神经元，但会把权重按丢弃概率缩放，使得输出的期望保持一致。AlexNet 正是因为采用了 Dropout，有效缓解了当时训练大规模网络时严重的过拟合问题，才能在 ImageNet 上取得远超传统方法的表现。</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<h2 class="wp-block-heading has-text-align-center">4.数据扩增Data Augumentation</h2>



<p>AlexNet 用的 ImageNet 数据集已经有 120 万张标注图片，在当时算是非常庞大的训练集了。但即便如此，这样一个 <strong>拥有 6000 万参数</strong> 的深度卷积神经网络，依然容易 <strong>过拟合</strong>。原因有两个：</p>



<p><strong>第一，网络容量太大。</strong><br>AlexNet 的参数规模比以往的模型大得多，它有能力“记住”训练数据里的很多细节甚至噪声。如果没有更多样化的数据，它在测试集上就会表现下降。</p>



<p><strong>第二，样本分布不够“丰富”。</strong><br>虽然 ImageNet 总量大，但对每个类别来说，图片的采集场景有限：</p>



<ul class="wp-block-list">
<li>大部分照片拍摄角度类似</li>



<li>光照条件相对单一</li>



<li>物体位置、比例变化不够多</li>
</ul>



<p>这会导致网络学习到的特征偏向于这些“训练集特定的分布”，泛化能力不足。</p>



<p>因此，数据增强（Data Augmentation）就显得非常重要。AlexNet 里采用的增强方法主要有：</p>



<ul class="wp-block-list">
<li><strong>随机裁剪</strong>：从原始 256×256 图像中随机裁出 224×224 的区域，增加平移与缩放的变化。</li>



<li><strong>水平翻转</strong>：随机把图片左右翻转，让模型学会对称性。</li>



<li><strong>RGB 通道扰动</strong>：对颜色做轻微变化，模拟不同光照环境。</li>
</ul>



<p>这些操作能在 <strong>不增加人工标注成本</strong> 的前提下，成倍地产生新的训练样本。这样一来，网络不仅见过“猫的正面照片”，也见过“猫的翻转照片”；不仅见过“光线好的苹果”，也见过“偏暗的苹果”。最终效果就是 <strong>提升泛化性能，降低过拟合</strong>。</p>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<h2 class="wp-block-heading has-text-align-center">5.GPU并行训练</h2>



<p>在 2012 年的时候，深度神经网络的规模已经远远超出了普通 CPU 的承载能力。AlexNet 大约有 <strong>6000 万参数、6.5 亿连接</strong>，如果完全用 CPU 训练，可能要几个月甚至更久，几乎不可行。</p>



<p>Alex Krizhevsky 当时采用了 <strong>两块 NVIDIA GTX 580 GPU（每块显存 3GB）</strong> 来加速训练，并设计了一种 <strong>跨 GPU 的分工机制</strong>：</p>



<ul class="wp-block-list">
<li><strong>通道切分（channel split）</strong>：每一层的特征图通道数被 <strong>平均分成两份</strong>，上面一半分配给 GPU1，下面一半分配给 GPU2。</li>



<li><strong>卷积局部计算</strong>：大多数卷积操作只在“本 GPU 的通道子集”上完成，减少跨卡通信量。</li>



<li><strong>有限的跨 GPU 连接</strong>：在某些层（如 Conv3、Conv4、Conv5）引入少量跨 GPU 的连接，用来保持特征的一致性。</li>
</ul>



<p>这样做的好处是：</p>



<ol class="wp-block-list">
<li><strong>分担显存压力</strong>：当时的显存有限，把通道拆分能保证网络能放下。</li>



<li><strong>并行计算</strong>：两个 GPU 同时运算，大幅缩短训练时间。AlexNet 最终用了 <strong>5~6 天</strong> 就在 ImageNet 上完成训练，而不是几个月。</li>



<li><strong>工程上的突破</strong>：这是深度学习第一次真正利用 GPU 进行大规模卷积神经网络的训练，奠定了后面“GPU 是深度学习核心算力”的基础。</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<p>思考题：根据LeNet参数个数为61706个，计算ALEXNET每层参数的个数。</p>



<h3 class="wp-block-heading">卷积层</h3>



<ul class="wp-block-list">
<li><strong>Conv1</strong>：11×11×3×96+96=34944</li>



<li><strong>Conv2（groups=2）</strong>：每个核只连到上一层的一半通道 48<br>5×5×48×256+256=307456</li>



<li><strong>Conv3（不分组）</strong>：3×3×256×384+384=885120</li>



<li><strong>Conv4（groups=2）</strong>：3×3×192×384+384=663936</li>



<li><strong>Conv5（groups=2）</strong>：3×3×192×256+256=442624</li>
</ul>



<h3 class="wp-block-heading">全连接层</h3>



<ul class="wp-block-list">
<li><strong>FC6</strong>（6×6×256=9216→4096）：9216×4096+4096=37752832</li>



<li><strong>FC7</strong>（4096→4096）：4096×4096+4096=16781312</li>



<li><strong>FC8</strong>（4096→1000）：4096×1000+1000=4097000</li>
</ul>



<h3 class="wp-block-heading">总计</h3>



<p>60965224</p>



<p>（≈ 6,096 万参数）</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>4.2卷积神经网络</title>
		<link>https://www.leexinghai.com/aic/4-2%e5%8d%b7%e7%a7%af%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Thu, 28 Aug 2025 03:04:03 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3330</guid>

					<description><![CDATA[LeNet-5，是深度学习史上非常经典的卷积神经网络结构，由 Yann LeCun 在 1998 年提出，最初 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><strong>LeNet-5</strong>，是深度学习史上非常经典的卷积神经网络结构，由 <strong>Yann LeCun</strong> 在 1998 年提出，最初是为了解决手写数字识别问题（比如 <strong>MNIST 数据集</strong>）。它可以说是后来 CNN（包括 AlexNet、ResNet 等）的“鼻祖”。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">1. LeNet-5 的基本结构</h2>



<p>LeNet-5 由 7 层（不包括输入层）组成，包括卷积层、池化层、全连接层：</p>



<ol class="wp-block-list">
<li><strong>输入层</strong>
<ul class="wp-block-list">
<li>输入图像大小：32 × 32 像素灰度图。</li>



<li>（MNIST 是 28 × 28，LeNet 会在周围补白到 32 × 32）。</li>
</ul>
</li>



<li><strong>C1：第1个卷积层</strong>
<ul class="wp-block-list">
<li>卷积核大小：5 × 5</li>



<li>卷积核数量：6</li>



<li>输出特征图大小：28 × 28 × 6</li>



<li><strong>作用</strong>：提取局部边缘、线条等低级特征。</li>
</ul>
</li>



<li><strong>S2：第1个下采样层（平均池化）</strong>
<ul class="wp-block-list">
<li>池化核：2 × 2，步长 2</li>



<li>输出特征图大小：14 × 14 × 6</li>



<li><strong>作用</strong>：减少参数，增强特征平移不变性。</li>
</ul>
</li>



<li><strong>C3：第2个卷积层</strong>
<ul class="wp-block-list">
<li>卷积核大小：5 × 5</li>



<li>卷积核数量：16</li>



<li>输出特征图大小：10 × 10 × 16</li>



<li><strong>作用</strong>：提取更复杂的形状（如角点、组合结构）。</li>
</ul>
</li>



<li><strong>S4：第2个下采样层（平均池化）</strong>
<ul class="wp-block-list">
<li>池化核：2 × 2，步长 2</li>



<li>输出特征图大小：5 × 5 × 16</li>



<li><strong>作用</strong>：进一步压缩特征图。</li>
</ul>
</li>



<li><strong>C5：第3个卷积层（特殊，全连接卷积）</strong>
<ul class="wp-block-list">
<li>卷积核大小：5 × 5</li>



<li>卷积核数量：120</li>



<li>因为输入是 5 × 5，卷积后得到 1 × 1 × 120</li>



<li><strong>作用</strong>：相当于“全连接层”，整合所有特征。</li>
</ul>
</li>



<li><strong>F6：全连接层</strong>
<ul class="wp-block-list">
<li>节点数：84</li>



<li>使用 sigmoid 激活函数（当时没有 ReLU）。</li>



<li><strong>作用</strong>：作为分类前的特征整合层。</li>
</ul>
</li>



<li><strong>输出层</strong>
<ul class="wp-block-list">
<li>节点数：10（对应 0~9 的手写数字类别）。</li>



<li>使用 Softmax 作为输出。</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">2. LeNet-5 的特点</h2>



<ul class="wp-block-list">
<li><strong>局部连接</strong>：不同于全连接层，卷积层只和输入的一小块区域相连。</li>



<li><strong>权值共享</strong>：同一个卷积核在不同位置共享权重，大幅减少参数数量。</li>



<li><strong>分层特征提取</strong>：从边缘 → 局部组合 → 整体形状 → 分类结果。</li>



<li><strong>池化机制</strong>：增强模型对位置变化的鲁棒性。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">3. LeNet-5 的影响</h2>



<ul class="wp-block-list">
<li>是 <strong>第一个被广泛应用的卷积神经网络</strong>，在银行支票识别中取得成功。</li>



<li>奠定了现代 CNN 的基本框架（Conv → Pooling → Conv → Pooling → FC → Softmax）。</li>



<li>后续的 <strong>AlexNet (2012)</strong> 在此基础上引入了 ReLU、Dropout、GPU 训练，才真正引爆了深度学习浪潮。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p class="has-large-font-size">那么我们如何求解卷积核中参数的梯度呢？</p>



<p><strong>P1</strong>（左上）： P1=ω<sub>1</sub>X<sub>1</sub>+ω<sub>2</sub>X<sub>2</sub>+ω<sub>3</sub>X<sub>4</sub>+ω<sub>4</sub>X<sub>5</sub></p>



<p><strong>P2</strong>（右上）：  P2=ω<sub>1</sub>X<sub>2</sub>+ω<sub>2</sub>X<sub>3</sub>+ω<sub>3</sub>X<sub>5</sub>+ω<sub>4</sub>X<sub>6</sub></p>



<p><strong>P3</strong>（左下）：  P3=ω<sub>1</sub>X<sub>4</sub>+ω<sub>2</sub>X<sub>5</sub>+ω<sub>3</sub>X<sub>7</sub>+ω<sub>4</sub>X<sub>8</sub></p>



<p><strong>P4</strong>（右下）：  P4=ω<sub>1</sub>X<sub>5</sub>+ω<sub>2</sub>X<sub>6</sub>+ω<sub>3</sub>X<sub>8</sub>+ω<sub>4</sub>X<sub>9</sub></p>



<h3 class="wp-block-heading">积核参数的梯度（反向传播）</h3>



<p>若损失为 LLL，上游梯度记为 </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msub><mi>δ</mi><mi>k</mi></msub><mo>=</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>L</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>P</mi><mi>k</mi></msub></mrow></mfrac></math>



<p>​（k=1,2,3,4），则每个权重的梯度是“<strong>对应位置输入的加权和</strong>”： </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtable displaystyle="true" columnalign="right left right left right left right left right left right left" columnspacing="0em 2em 0em 2em 0em 2em 0em 2em 0em 2em 0em" rowspacing="3pt"><mtr><mtd><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>L</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>ω</mi><mn>1</mn></msub></mrow></mfrac></mtd><mtd><mi></mi><mo>=</mo><msub><mi>δ</mi><mn>1</mn></msub><msub><mi>X</mi><mn>1</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>2</mn></msub><msub><mi>X</mi><mn>2</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>3</mn></msub><msub><mi>X</mi><mn>4</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>4</mn></msub><msub><mi>X</mi><mn>5</mn></msub><mo>,</mo></mtd></mtr><mtr><mtd><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>L</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>ω</mi><mn>2</mn></msub></mrow></mfrac></mtd><mtd><mi></mi><mo>=</mo><msub><mi>δ</mi><mn>1</mn></msub><msub><mi>X</mi><mn>2</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>2</mn></msub><msub><mi>X</mi><mn>3</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>3</mn></msub><msub><mi>X</mi><mn>5</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>4</mn></msub><msub><mi>X</mi><mn>6</mn></msub><mo>,</mo></mtd></mtr><mtr><mtd><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>L</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>ω</mi><mn>3</mn></msub></mrow></mfrac></mtd><mtd><mi></mi><mo>=</mo><msub><mi>δ</mi><mn>1</mn></msub><msub><mi>X</mi><mn>4</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>2</mn></msub><msub><mi>X</mi><mn>5</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>3</mn></msub><msub><mi>X</mi><mn>7</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>4</mn></msub><msub><mi>X</mi><mn>8</mn></msub><mo>,</mo></mtd></mtr><mtr><mtd><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>L</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>ω</mi><mn>4</mn></msub></mrow></mfrac></mtd><mtd><mi></mi><mo>=</mo><msub><mi>δ</mi><mn>1</mn></msub><msub><mi>X</mi><mn>5</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>2</mn></msub><msub><mi>X</mi><mn>6</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>3</mn></msub><msub><mi>X</mi><mn>8</mn></msub><mo>+</mo><msub><mi>δ</mi><mn>4</mn></msub><msub><mi>X</mi><mn>9</mn></msub><mo>.</mo></mtd></mtr></mtable></math>



<p>（若有偏置 b，则 ∂L/∂b=δ<sub>1</sub>+δ<sub>2</sub>+δ<sub>3</sub>+δ<sub>4</sub>）</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>记忆方法：<strong>哪个输出位置用了某个权重乘了哪一个输入像素，反传时就把该输出的上游梯度乘回那像素并累加到该权重的梯度上。</strong><br>推广到更大核/更大特征图/多通道完全一样：对所有位置求和即可。</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity has-midnight-gradient-background has-background is-style-wide"/>



<h2 class="wp-block-heading has-text-align-center">降采样层（引入）</h2>



<h3 class="wp-block-heading">1. 为什么需要降采样层？</h3>



<p>在卷积网络的早期设计（LeNet-5、AlexNet）里，降采样的动机主要有：</p>



<ul class="wp-block-list">
<li><strong>减少数据量</strong>：降低特征图空间尺寸，减少计算量和参数量。</li>



<li><strong>防止过拟合</strong>：通过压缩信息，减少模型过度拟合细节噪声的风险。</li>



<li><strong>增强平移不变性</strong>：在小范围平移、旋转下，池化能保持特征稳定（例如猫耳朵往左移一点，特征依然能被识别）。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2. 常见的降采样方式</h3>



<h4 class="wp-block-heading">(1) 最大池化（Max Pooling）</h4>



<ul class="wp-block-list">
<li><strong>公式</strong>：从窗口内取最大值作为输出。</li>



<li><strong>特点</strong>：保留最显著的特征，丢弃背景或弱特征。</li>



<li><strong>常用参数</strong>：2×2 窗口，stride=2。</li>



<li><strong>优点</strong>：效果直观，表现好。</li>
</ul>



<h4 class="wp-block-heading">(2) 平均池化（Average Pooling）</h4>



<ul class="wp-block-list">
<li><strong>公式</strong>：对窗口内所有值取平均值。</li>



<li><strong>特点</strong>：保留整体趋势，平滑特征。</li>



<li><strong>LeNet-5</strong> 就用了平均池化（叫“subsampling”）。</li>
</ul>



<h4 class="wp-block-heading">(3) 全局平均池化（Global Average Pooling, GAP）</h4>



<ul class="wp-block-list">
<li><strong>公式</strong>：直接对整个特征图取平均，得到 1 个值。</li>



<li><strong>应用</strong>：现代 CNN（如 GoogLeNet）用 GAP 来代替全连接层，减少参数并提升泛化。</li>
</ul>



<h4 class="wp-block-heading">(4) 随机池化 / Lp 池化</h4>



<ul class="wp-block-list">
<li>一些研究提出过随机选择、或者 Lp 范数池化，但应用没有前两种普遍。</li>
</ul>



<figure class="wp-block-pullquote"><blockquote><p>通过Python来理解降采样层</p><cite><strong>最大池化 2×2 stride=2 的图示</strong>（输入 4×4 → 输出 2×2），直观看到“降采样层”是怎么工作的</cite></blockquote></figure>



<pre class="wp-block-code"><code>import platform

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
system = platform.system()
if system == "Windows":
    matplotlib.rcParams&#91;'font.family'] = 'Microsoft YaHei'
elif system == "Darwin":
    matplotlib.rcParams&#91;'font.family'] = 'Arial Unicode MS'
else:
    matplotlib.rcParams&#91;'font.family'] = 'SimHei'
matplotlib.rcParams&#91;'axes.unicode_minus'] = False
# 构造一个4x4的输入特征图
input_matrix = np.array(&#91;
    &#91;1, 3, 2, 4],
    &#91;5, 6, 7, 8],
    &#91;9, 2, 0, 1],
    &#91;3, 4, 5, 6]
])

# 最大池化 2x2 stride=2
output_matrix = np.array(&#91;
    &#91;np.max(input_matrix&#91;0:2, 0:2]), np.max(input_matrix&#91;0:2, 2:4])],
    &#91;np.max(input_matrix&#91;2:4, 0:2]), np.max(input_matrix&#91;2:4, 2:4])]
])

fig, axes = plt.subplots(1, 2, figsize=(8,4))

# 输入矩阵可视化
axes&#91;0].imshow(input_matrix, cmap="Blues", vmin=0, vmax=9)
for i in range(4):
    for j in range(4):
        axes&#91;0].text(j, i, input_matrix&#91;i,j], ha="center", va="center", color="black")
axes&#91;0].set_title("输入特征图 (4x4)")

# 输出矩阵可视化
axes&#91;1].imshow(output_matrix, cmap="Oranges", vmin=0, vmax=9)
for i in range(2):
    for j in range(2):
        axes&#91;1].text(j, i, output_matrix&#91;i,j], ha="center", va="center", color="black")
axes&#91;1].set_title("池化输出 (2x2)")

plt.tight_layout()
plt.show()
</code></pre>



<p>效果图：</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="593" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-72-1024x593.png" alt="" class="wp-image-3332" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-72-1024x593.png 1024w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-72-300x174.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-72-768x445.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-72.png 1204w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide"/>



<p>计算LeNet待估计参数个数：61684个</p>



<p>这个 <strong>61684</strong> 的总数，是把 <strong>C1 和 C3 不计偏置</strong>，其余层计了偏置 的“混合口径”算出来的。<br>逐项对应你给的公式：</p>



<ul class="wp-block-list">
<li><strong>C1</strong>：5×5×6=150（只算权重，<strong>没加 6 个 bias</strong>）</li>



<li><strong>S2</strong>：0（现代实现里池化无可学习参数）</li>



<li><strong>C3</strong>：5×5×6×16=2400（只算权重，<strong>没加 16 个 bias</strong>）</li>



<li><strong>S4</strong>：0</li>



<li><strong>C5</strong>：120×(16×5×5+1)=48,120（含 bias）</li>



<li><strong>F6</strong>：120×84+84=10,164（含 bias）</li>



<li><strong>输出</strong>：84×10+10=850（含 bias）</li>
</ul>



<p>把这些相加：150+0+2400+0+48120+10164+850=61684</p>



<p>三种常见口径对齐：</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>层</th><th>只算权重</th><th>只算权重的数</th><th>加上偏置后的数</th></tr></thead><tbody><tr><td>C1 (5×5, in=1, out=6)</td><td>5⋅5⋅1⋅6</td><td>150</td><td><strong>156</strong>（+6 bias）</td></tr><tr><td>S2 (pool)</td><td>–</td><td>0</td><td><strong>0</strong></td></tr><tr><td>C3 (5×5, in=6, out=16，全连接)</td><td>5⋅5⋅6⋅16</td><td>2400</td><td><strong>2416</strong>（+16 bias）</td></tr><tr><td>S4 (pool)</td><td>–</td><td>0</td><td><strong>0</strong></td></tr><tr><td>C5 (5×5, in=16, out=120)</td><td>16⋅5⋅5⋅120</td><td>48,000</td><td><strong>48,120</strong>（+120 bias）</td></tr><tr><td>F6 (120→84)</td><td>120⋅84</td><td>10,080</td><td><strong>10,164</strong>（+84 bias）</td></tr><tr><td>输出 (84→10)</td><td>84⋅10</td><td>840</td><td><strong>850</strong>（+10 bias）</td></tr><tr><td><strong>合计</strong></td><td><strong>61,470</strong></td><td></td><td><strong>61,706</strong></td></tr></tbody></table></figure>



<p><br><strong>61,684</strong> = 上表“加上偏置”的总数 <strong>61,706</strong> −（C1 的 6 + C3 的 16）= <strong>61,706 − 22</strong>。</p>



<p class="has-text-align-center">思考题请见下一页<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>


]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>4.1自编码器-引入</title>
		<link>https://www.leexinghai.com/aic/4-1%e8%87%aa%e7%bc%96%e7%a0%81%e5%99%a8-%e5%bc%95%e5%85%a5/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Thu, 28 Aug 2025 02:13:21 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3328</guid>

					<description><![CDATA[自编码器的分层初始化（Layer-wise Pretraining）思想，其实就是 2006 年 Hinton [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><strong>自编码器的分层初始化（Layer-wise Pretraining）思想</strong>，其实就是 2006 年 Hinton 等人提出的 <strong>深度网络逐层无监督预训练</strong> 方法。</p>



<h3 class="wp-block-heading">1. 背景</h3>



<ul class="wp-block-list">
<li>在深度学习早期（2000 年前后），深层神经网络训练很难：
<ul class="wp-block-list">
<li>随机初始化 → 反向传播时梯度消失 / 爆炸；</li>



<li>没有足够标注数据，深层网络容易陷入局部最优。</li>
</ul>
</li>



<li>Hinton 提出利用 <strong>自编码器</strong> 逐层无监督训练网络，把深度模型拆解成若干层来初始化参数。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">2. 分层初始化的核心思想</h3>



<p><strong>逐层训练 + 自底向上堆叠</strong>：</p>



<ol class="wp-block-list">
<li>先训练一个浅层自编码器（输入层 → 隐层 → 重构输出层），学到第一层特征。</li>



<li>固定第一层权重，只保留隐层表示，作为“输入”去训练第二个自编码器，得到第二层特征。</li>



<li>重复以上过程，层层堆叠，直到得到多层网络的参数。</li>



<li>最后把这些预训练好的权重作为深度神经网络的 <strong>初始化参数</strong>，再用有监督的目标（分类/回归）进行 <strong>整体微调（fine-tuning）</strong>。</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">3. 为什么有效？</h3>



<ul class="wp-block-list">
<li><strong>缓解梯度消失问题</strong>：每一层都先单独学会“有用的特征”，不依赖深层反向传播。</li>



<li><strong>更好的初始化</strong>：比随机初始化更接近“合适区域”，微调时更容易收敛。</li>



<li><strong>利用无标签数据</strong>：自编码器训练不需要标签，可以充分利用海量未标注农作物图像。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Matlab代码</p>



<pre class="wp-block-code"><code>3.function sae= sae create(SIZE)
    sae =nn create(&#91;SIZE(1),SIZE(2),SIZE(1)]) %输入和输出都是SIZE1,中间层是SIZE2 这是各层训练初始化的过程
4.function sae =sae train(sae,option,train_x)
    sae.encoder = 1;
    sae=nn train(sae,option,train_x,train_x)
end</code></pre>


]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.10训练参数设置</title>
		<link>https://www.leexinghai.com/aic/3-10%e8%ae%ad%e7%bb%83%e5%8f%82%e6%95%b0%e8%ae%be%e7%bd%ae/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Wed, 27 Aug 2025 08:54:21 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3299</guid>

					<description><![CDATA[三个训练神经网络的建议 (1)一般情况下，在训练集上的目标函数的平均值(cost)会随着训练的深入而不断减小， [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>三个训练神经网络的建议</p>



<p>(1)一般情况下，在训练集上的目标函数的平均值(cost)会随着训练的深入而不断减小，如果这个指标有增大情况，停下来。<br>有两种情况:</p>



<ul class="wp-block-list">
<li>采用的模型不够复杂，以致于不能在训练集上完全拟合;</li>



<li>已经训练很好了。</li>
</ul>



<p>(2)<strong>分出一些验证集Validation Set，训练本质目标是在验证集上获取最大识别率。</strong>因此训练一段时间后，必须在验证集上测试识别率，保存使验证集上识别率最大的模型参数作为最后的结果。</p>



<p>(3)<strong>注意调整学习率Learning Rate</strong>，如果刚训练几步损失函数就增加，一般来说是学习率太高；反之如果每次cost变化很小，说明学习率太低。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">一点人生的经验：</h2>



<p>（1）目标函数可以加入正则项</p>



<p class="has-text-align-center">Minimize E(ω，b)=L(ω，b)+λ/2 ||ω||<sup>2</sup>  </p>



<p>L(ω，b)为原来的目标函数，λ/2 ||ω||<sup>2</sup>为正则项。λ为权值衰减系数</p>



<p>参考前向传播<code>nn_forward.m</code></p>



<pre class="wp-block-code"><code>if strcmp(nn.objective_function,'MSE')
            nn.cost(s) = 0.5 / m * sum(sum((nn.a{k} - batch_y).^2)) + 0.5 * nn.weight_decay * cost2;
        elseif strcmp(nn.objective_function,'Cross Entropy')
            nn.cost(s) = -0.5*sum(sum(batch_y.*log(nn.a{k})))/m + 0.5 * nn.weight_decay * cost2;</code></pre>



<p>后向传播<code>nn_backpropagation.m</code></p>



<pre class="wp-block-code"><code>nn.W_grad{nn.depth-1} = nn.theta{nn.depth}*nn.a{nn.depth-1}'/m + nn.weight_decay*nn.W{nn.depth-1};
nn.b_grad{nn.depth-1} = sum(nn.theta{nn.depth},2)/m;</code></pre>



<p>（2）训练数据归一化</p>



<p>newX=[X-mean(X)]/ std(X)</p>



<p>（3）参数ω和b的初始化</p>



<p>一种比较简单有效的方法：</p>



<p>（ω，b）初始化从区间（-1/sqrt(d),1/sqrt(d)）均匀随机取值，其中d为（ω，b）所在层的神经元个数。</p>



<p>可以证明如果X服从均值0方差1的正态分布，且各个维度无关，而（ω，b）是区间（-1/sqrt(d),1/sqrt(d)）的均匀分布，则ω<sup>T</sup>X+b是均值0，方差为1/3的正态分布</p>



<p><code>nn_create.m</code></p>



<pre class="wp-block-code"><code>nn.W{k} = 2*rand(height, width)/sqrt(width)-1/sqrt(width);%rand产生伪随机数矩阵，即W权重矩阵初始化
nn.b{k} = 2*rand(height, 1)/sqrt(width)-1/sqrt(width);%b阈值的初始化</code></pre>



<p>避免一开始梯度趋近于0的现象。</p>



<p>（4）BATCH NORMALIZATION</p>



<p>论文:Batch normalization accelerating deep network training by reducing internal covariate shift(2015)</p>



<p>在这可以看：</p>



<div data-wp-interactive="core/file" class="wp-block-file"><object data-wp-bind--hidden="!state.hasPdfPreview" hidden class="wp-block-file__embed" data="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/43442.pdf" type="application/pdf" style="width:100%;height:600px" aria-label="嵌入 也可以点击直接下载论文："></object><a id="wp-block-file--media-0073f6c1-f969-40f8-885f-d0335b359207" href="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/43442.pdf">也可以点击直接下载论文：</a><a href="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/43442.pdf" class="wp-block-file__button wp-element-button" download aria-describedby="wp-block-file--media-0073f6c1-f969-40f8-885f-d0335b359207">下载</a></div>



<p><br>基本思想:既然我们希望每一层获得的值都在0附近，从而避免梯度消失现象，那么我们为什么不直接把每一层的值做基于均值和方差的归一化呢?</p>



<p>（5）参数的更新策略</p>



<p>ADAGRAD的方法</p>



<pre class="wp-block-code"><code>if strcmp(nn.optimization_method,'AdaGrad')
nn.rW{k}= nn.rW{k}+nn.W_grad{k}.^2;nn.rb{k}= nn.rb{k}+nn.b_grad{k}.^2;
nn.W{k}=nn.W{k}-nn.learning_rate*nn.W_grad{k}./(sqrt(nn.rW{k})+0.001);
nn.b{k}=nn.b{k}-nn.learning_rate*nn.b_qrad{k}./(sqrt(nn.rb{k})+0.001);</code></pre>



<p>解决梯度随机性的问题：引入Momentum</p>



<p>同时结合：Adam-解决梯度绝对值分量不平衡和梯度方向随机性的问题，也引入了逐渐降低梯度搜索步长的机制。</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="770" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-70-1024x770.png" alt="" class="wp-image-3302" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-70-1024x770.png 1024w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-70-300x226.png 300w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-70-768x578.png 768w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-70.png 1058w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4dd.png" alt="📝" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 算法步骤解释</h2>



<ol class="wp-block-list">
<li><strong>Require</strong>
<ul class="wp-block-list">
<li><strong>Step size</strong> (ϵ，学习率)，推荐默认值 0.001。</li>



<li><strong>Exponential decay rates</strong> (ρ1,ρ2​)：分别控制一阶、二阶动量的衰减速率。推荐默认值 ρ1=0.9，ρ2=0.999。</li>



<li><strong>Small constant δ</strong>：数值稳定常数，防止分母为零，默认 10<sup>−8</sup>。</li>



<li><strong>Initial parameters</strong> θ：模型初始参数。</li>
</ul>
</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<ol start="2" class="wp-block-list">
<li><strong>Initialize</strong>
<ul class="wp-block-list">
<li>一阶动量变量 s=0（存放梯度的指数加权平均，类似 Momentum）。</li>



<li>二阶动量变量 r=0（存放梯度平方的指数加权平均，类似 RMSProp）。</li>



<li>时间步 t=0。</li>
</ul>
</li>



<li><strong>循环过程</strong> (直到满足停止条件，例如迭代次数用完或收敛) 
<ul class="wp-block-list">
<li><strong>Step A. 采样一个 minibatch</strong>
<ul class="wp-block-list">
<li>从训练集取出一个小批量样本 {x<sup>(1)</sup>,...,x<sup>(m)</sup>} 和对应标签。</li>
</ul>
</li>



<li><strong>Step B. 计算梯度</strong></li>
</ul>
</li>
</ol>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>g</mi><mo>=</mo><mfrac><mn>1</mn><mi>m</mi></mfrac><msub><mi mathvariant="normal">∇</mi><mi>θ</mi></msub><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>m</mi></munderover><mi>L</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><msup><mi>x</mi><mrow><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo></mrow></msup><mo>;</mo><mi>θ</mi><mo stretchy="false">)</mo><mo>,</mo><msup><mi>y</mi><mrow><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo></mrow></msup><mo stretchy="false">)</mo></math>



<p>即小批量平均梯度。 </p>



<p><strong>Step C. 时间步递增</strong> </p>



<p class="has-text-align-center">t←t+1</p>



<p> <strong>Step D. 更新一阶动量（偏置的）</strong> </p>



<p class="has-text-align-center">s←ρ1s+(1−ρ1)g</p>



<p> ——这是梯度的指数滑动平均（类似 Momentum）。</p>



<p><strong>Step E. 更新二阶动量（偏置的）</strong> </p>



<p class="has-text-align-center">r←ρ2r+(1−ρ2)(g⊙g)</p>



<p>——这里 ⊙表示逐元素乘法。即对梯度平方取指数滑动平均（类似 RMSProp）。</p>



<p> <strong>Step F. 偏差修正</strong><br>由于初始化 s=0,r=0，前期会有向零偏移，需要修正： </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mover><mi>s</mi><mo stretchy="false">^</mo></mover></mrow><mo>=</mo><mfrac><mi>s</mi><mrow><mn>1</mn><mo>−</mo><msubsup><mi>ρ</mi><mn>1</mn><mi>t</mi></msubsup></mrow></mfrac><mo>,</mo><mstyle scriptlevel="0"><mspace width="1em"></mspace></mstyle><mrow><mover><mi>r</mi><mo stretchy="false">^</mo></mover></mrow><mo>=</mo><mfrac><mi>r</mi><mrow><mn>1</mn><mo>−</mo><msubsup><mi>ρ</mi><mn>2</mn><mi>t</mi></msubsup></mrow></mfrac></math>



<p>​<strong>Step G. 计算更新量</strong> </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi mathvariant="normal">Δ</mi><mi>θ</mi><mo>=</mo><mo>−</mo><mi>ϵ</mi><mo>⋅</mo><mfrac><mrow><mover><mi>s</mi><mo stretchy="false">^</mo></mover></mrow><mrow><msqrt><mrow><mover><mi>r</mi><mo stretchy="false">^</mo></mover></mrow></msqrt><mo>+</mo><mi>δ</mi></mrow></mfrac></math>



<p><strong>Step H. 更新参数</strong> </p>



<p>θ←θ+Δθ</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f511.png" alt="🔑" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 总结</h2>



<ul class="wp-block-list">
<li><strong>sss</strong>：梯度的一阶动量（方向 + 平滑）。</li>



<li><strong>rrr</strong>：梯度的二阶动量（幅度 + 自适应缩放）。</li>



<li><strong>偏差修正</strong>：解决初期 (s,r≈0)的估计偏差问题。</li>



<li><strong>更新公式</strong>：学习率会根据梯度历史动态调整，每个参数有自己独立的学习率。</li>
</ul>



<p>Adam 的更新可以理解为：<br><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>用 Momentum 决定方向</strong>，再 <strong>用 RMSProp 决定步长大小</strong>。</p>



<p>Python代码示例（一阶动量用 <code>s</code>，二阶动量用 <code>r</code>，含偏差修正；并给了一个最小化二次函数的小示例）：</p>



<pre class="wp-block-code"><code>import numpy as np

class Adam:
    """
    Adam 优化器（Algorithm 8.7）
    s: 一阶动量（biased）
    r: 二阶动量（biased）
    """
    def __init__(self, shape, lr=1e-3, rho1=0.9, rho2=0.999, eps=1e-8):
        self.lr   = lr        # ε (step size)
        self.rho1 = rho1      # ρ1
        self.rho2 = rho2      # ρ2
        self.eps  = eps       # δ
        self.s    = np.zeros(shape)  # 初始化一阶动量 s=0
        self.r    = np.zeros(shape)  # 初始化二阶动量 r=0
        self.t    = 0                  # 初始化时间步 t=0

    def step(self, theta, g):
        """
        单次更新：
        theta: 参数
        g:     当前梯度（对 minibatch 的平均梯度）
        return: 更新后的参数
        """
        # t ← t + 1
        self.t += 1

        # Update biased first moment estimate: s ← ρ1 s + (1-ρ1) g
        self.s = self.rho1 * self.s + (1.0 - self.rho1) * g

        # Update biased second moment estimate: r ← ρ2 r + (1-ρ2) (g ⊙ g)
        self.r = self.rho2 * self.r + (1.0 - self.rho2) * (g * g)

        # Correct bias:
        # ŝ = s / (1 - ρ1^t),   r̂ = r / (1 - ρ2^t)
        s_hat = self.s / (1.0 - self.rho1 ** self.t)
        r_hat = self.r / (1.0 - self.rho2 ** self.t)

        # Compute update: Δθ = -ε * ŝ / (sqrt(r̂) + δ)
        delta_theta = - self.lr * s_hat / (np.sqrt(r_hat) + self.eps)

        # Apply update: θ ← θ + Δθ
        theta = theta + delta_theta
        return theta

# ================= 示例：最小化 f(θ)=∑ θ_i^2 =================
# 真梯度：∇f(θ)=2θ
np.random.seed(0)
theta = np.random.randn(3) * 5.0        # 初始参数
opt   = Adam(shape=theta.shape, lr=1e-2) # 用默认 ρ1=0.9, ρ2=0.999, δ=1e-8

for k in range(1, 501):
    g = 2.0 * theta                      # 计算梯度 (小批量平均梯度在真实任务里替换这里)
    theta = opt.step(theta, g)           # 按图中流程更新
    if k % 100 == 0:
        fval = (theta**2).sum()
        print(f"iter {k:3d}  f(theta)={fval:.6f}  theta={theta}")

# 输出会看到 f(θ) 单调下降，θ 收敛到 0 附近
</code></pre>



<p>输出：</p>



<pre class="wp-block-code"><code>iter 100  f(theta)=78.124320  theta=&#91;7.84011204 1.09919008 3.93048901]
iter 200  f(theta)=57.552498  theta=&#91;6.91704488 0.49699096 3.07570938]
iter 300  f(theta)=42.152403  theta=&#91;6.0541767  0.17936161 2.33819949]
iter 400  f(theta)=30.556370  theta=&#91;5.25288565 0.05091135 1.72074696]
iter 500  f(theta)=21.872385  theta=&#91;4.51437396 0.01130478 1.22175496]</code></pre>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4ca.png" alt="📊" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 结果逐行解释</h2>



<p>输出是每 100 次迭代打印一次：</p>



<h3 class="wp-block-heading">Iter 100</h3>



<pre class="wp-block-code"><code>f(theta)=78.124320  
theta=&#91;7.84011204 1.09919008 3.93048901]</code></pre>



<ul class="wp-block-list">
<li>初始 θ 很大（一开始是 <code>np.random.randn(3)*5</code> 随机出来的）。</li>



<li>经过 100 步更新后，参数值比初始小了一些，但还比较大。目标函数 f(θ) 还在 78 左右。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Iter 200</h3>



<pre class="wp-block-code"><code>f(theta)=57.552498  
theta=&#91;6.91704488 0.49699096 3.07570938]</code></pre>



<ul class="wp-block-list">
<li>θ 的数值进一步下降了（尤其是第二个分量从 ~1.1 → 0.49）。</li>



<li>函数值 f(θ) 从 78 降到了 57，说明 Adam 在往 0 的方向走。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Iter 300</h3>



<pre class="wp-block-code"><code>f(theta)=42.152403  
theta=&#91;6.0541767  0.17936161 2.33819949]</code></pre>



<ul class="wp-block-list">
<li>继续下降，f 值变成 ~42。</li>



<li>第二个分量（0.179）几乎快收敛到 0 了。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Iter 400</h3>



<pre class="wp-block-code"><code>f(theta)=30.556370  
theta=&#91;5.25288565 0.05091135 1.72074696]</code></pre>



<ul class="wp-block-list">
<li>三个分量继续减小，函数值也继续下降。</li>



<li>可以看出来参数在逐步往 0 收缩。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">Iter 500</h3>



<pre class="wp-block-code"><code>f(theta)=21.872385  
theta=&#91;4.51437396 0.01130478 1.22175496]</code></pre>



<ul class="wp-block-list">
<li>此时 f 值还在下降（21），但下降速度变慢了。</li>



<li>第二个参数已经基本到 0（0.01），其他两个参数也明显比最开始小了很多。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f511.png" alt="🔑" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 总结</h2>



<ol class="wp-block-list">
<li><strong>趋势</strong>：函数值从 78 → 57 → 42 → 30 → 21，说明优化器 Adam 确实在不断让目标函数下降。</li>



<li><strong>参数收敛</strong>：θ 在逐步往 0 收敛（最终最优解）。</li>



<li><strong>下降速度</strong>：一开始下降快，后面越来越慢，这是正常的，因为越靠近最优点，梯度越小。</li>



<li><strong>未到 0</strong>：500 步还没完全到 0，是因为学习率比较小（lr=0.01），如果继续迭代或适当调大学习率，θ 会更快逼近 0。</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p><strong>画图验证Adam 能不断减小损失函数，并逐渐收敛到最优解的python代码请到第2页查看<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong></p>


]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.9用MATLAB编程构建多层神经网络分类器</title>
		<link>https://www.leexinghai.com/aic/3-9%e7%94%a8matlab%e7%bc%96%e7%a8%8b%e6%9e%84%e5%bb%ba%e5%a4%9a%e5%b1%82%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e5%88%86%e7%b1%bb%e5%99%a8/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Wed, 27 Aug 2025 07:37:48 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3293</guid>

					<description><![CDATA[nn_testChess.m 读入数据（one-hot两类情形）： 划分数据集分配： 分别对训练、验证、测试数 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><code>nn_testChess.m</code></p>



<p>读入数据（one-hot两类情形）：</p>



<pre class="wp-block-code"><code>if string(13) == 100
            yapp = &#91;yapp,&#91;1,0]'];
        else
            yapp = &#91;yapp,&#91;0,1]'];
        end;</code></pre>



<p>划分数据集分配：</p>



<pre class="wp-block-code"><code>ratioTraining = 0.15;    % 15%训练
ratioValidation = 0.05;  % 5%验证
ratioTesting = 0.8;      % 80%测试</code></pre>



<p>分别对训练、验证、测试数据集进行归一化：</p>



<pre class="wp-block-code"><code>xTraining = (xTraining - repmat(avgX,U,1))./repmat(sigma,U,1);
xValidation = (xValidation - repmat(avgX,U,1))./repmat(sigma,U,1);
xTesting = (xTesting - repmat(avgX,U,1))./repmat(sigma,U,1);</code></pre>



<p>整个神经网络的构建、训练和测试：</p>



<pre class="wp-block-code"><code>nn = nn_create(&#91;6,10,10,10,10,10,10,10,10,10,10,2],'active function','relu','learning rate',0.005, 'batch normalization',1,'optimization method','Adam', 'objective function', 'Cross Entropy');
%数组代表了神经网络每一层中的神经元个数。学习率α：learning rate、目标函数：objective function、神经网络的激活函数：active function；输入是6个维度，输出是2个维度，10层，每层有10个神经元。</code></pre>



<p>其他解读：</p>



<pre class="wp-block-code"><code>option.batch_size = 100;%每个mini-batch中有100个训练样本
maxIteration = 10000; %最大训练轮次10000轮
nn = nn_train(nn,option,xTraining,yTraining);%训练
totalCost(iteration) = sum(nn.cost)/length(nn.cost);%测试平均的损失函数
&#91;wrongs,accuracy] = nn_test(nn,xValidation,yValidation); %在验证集测试识别率</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>深入程序内部：</p>



<p><code>nn_forward.m</code></p>



<p>前向计算：</p>



<p>从第K-1层到第K层的输出过程：K-1层输出*权重矩阵ω+偏置</p>



<pre class="wp-block-code"><code>y = nn.W{k-1} * nn.a{k-1} + repmat(nn.b{k-1},1,m);
%repmat(A,m,n)将A复制m×n块
        %由于进行批处理，将m组数据存在矩阵同时处理，而对每组数据来说阈值设定是相同的，故将b复制m次
        %此处y即为所给推导方法中的z.</code></pre>



<p>经过非线性函数获得第K层输出：</p>



<pre class="wp-block-code"><code> switch nn.active_function%隐层激活函数选择
                case 'sigmoid'
                    nn.a{k} = sigmoid(y);
                case 'tanh'
                    nn.a{k} = tanh(y);
                case 'relu'
                    nn.a{k} = max(y,0);
            end</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>后向传播（根据前面学过的SIGMOID,TANH,SOFTMAX+CROSSENTROPY）：</p>



<p><code>nn_backpropagation.m</code></p>



<pre class="wp-block-code"><code>switch nn.output_function 
        case 'sigmoid'
            nn.theta{nn.depth} = -(batch_y-nn.a{nn.depth}) .* nn.a{nn.depth} .* (1 - nn.a{nn.depth});
        case 'tanh'
            nn.theta{nn.depth} = -(batch_y-nn.a{nn.depth}) .* (1 - nn.a{nn.depth}.^2);
        case 'softmax'
            nn.theta{nn.depth} = nn.a{nn.depth} - batch_y;
    end</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>多层神经网络参数的更新过程：</p>



<p><code>nn_applygradient.m</code></p>



<pre class="wp-block-code"><code>if strcmp(nn.optimization_method, 'normal')
            nn.W{k} = nn.W{k} - nn.learning_rate*nn.W_grad{k};
            nn.b{k} = nn.b{k} - nn.learning_rate*nn.b_grad{k};</code></pre>



<p>与3.7第2页中总结（4）迭代公式一样。</p>



<p>忘记了？看看<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<figure class="wp-block-embed is-type-wp-embed"><div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="LuSk7xZmkP"><a href="https://www.leexinghai.com/aic/3-7%e5%90%8e%e5%90%91%e4%bc%a0%e6%92%ad%e7%ae%97%e6%b3%95/">3.7后向传播算法</a></blockquote><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  title="《 3.7后向传播算法 》—学术创新中心" src="https://www.leexinghai.com/aic/3-7%e5%90%8e%e5%90%91%e4%bc%a0%e6%92%ad%e7%ae%97%e6%b3%95/embed/#?secret=VLAsDql9RX#?secret=LuSk7xZmkP" data-secret="LuSk7xZmkP" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div></figure>



<p></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.8后向传播算法的应用</title>
		<link>https://www.leexinghai.com/aic/3-8%e5%90%8e%e5%90%91%e4%bc%a0%e6%92%ad%e7%ae%97%e6%b3%95%e7%9a%84%e5%ba%94%e7%94%a8/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Wed, 27 Aug 2025 02:19:59 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3272</guid>

					<description><![CDATA[阶跃函数sigmoid的导数有： φ′(x)=(11+e−x)′=e−x(1+e−x)2=[1−11+e−x] [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>阶跃函数sigmoid的导数有：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msup><mi>φ</mi><mo>′</mo></msup><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><msup><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">(</mo><mfrac><mn>1</mn><mrow><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo data-mjx-texclass="CLOSE">)</mo></mrow><mo>′</mo></msup><mo>=</mo><mfrac><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup><mrow><mo stretchy="false">(</mo><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></mfrac><mo>=</mo><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">[</mo><mn>1</mn><mo>−</mo><mfrac><mn>1</mn><mrow><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo data-mjx-texclass="CLOSE">]</mo></mrow><mfrac><mn>1</mn><mrow><mn>1</mn><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo>=</mo><mi>φ</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo stretchy="false">[</mo><mn>1</mn><mo>−</mo><mi>φ</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo stretchy="false">]</mo></math>



<p>这个可以用来代替阶跃函数。</p>



<p>常用的非线性函数双曲正切tah函数表达式如下：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>φ</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mfrac><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>−</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac></math>



<p>这个tanh的导数有：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msup><mi>φ</mi><mo>′</mo></msup><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><msup><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">(</mo><mfrac><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>−</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo data-mjx-texclass="CLOSE">)</mo></mrow><mo>′</mo></msup><mo>=</mo><mfrac><mrow><mo stretchy="false">(</mo><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup><msup><mo stretchy="false">)</mo><mn>2</mn></msup><mo>−</mo><mo stretchy="false">(</mo><msup><mi>e</mi><mi>x</mi></msup><mo>−</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow><mrow><mo stretchy="false">(</mo><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup><msup><mo stretchy="false">)</mo><mn>2</mn></msup></mrow></mfrac><mo>=</mo><mn>1</mn><mo>−</mo><msup><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">[</mo><mfrac><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>−</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow><mrow><msup><mi>e</mi><mi>x</mi></msup><mo>+</mo><msup><mi>e</mi><mrow><mo>−</mo><mi>x</mi></mrow></msup></mrow></mfrac><mo data-mjx-texclass="CLOSE">]</mo></mrow><mn>2</mn></msup><mo>=</mo><mn>1</mn><mo>−</mo><mo stretchy="false">[</mo><mi>φ</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><msup><mo stretchy="false">]</mo><mn>2</mn></msup></math>



<p>对于目标函数，可以用预测值y和真实值Y差的模的平方：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>E</mi><mo>=</mo><mfrac><mn>1</mn><mn>2</mn></mfrac><mrow><mo stretchy="false">|</mo></mrow><mi>y</mi><mo>−</mo><mi>Y</mi><msup><mo stretchy="false">|</mo><mn>2</mn></msup></math>



<h2 class="wp-block-heading">但通常用基于softmax和交叉熵（cross-entropy）的目标函数。</h2>



<h2 class="wp-block-heading"><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-pale-pink-color">这些是什么东西？</mark></h2>



<h2 class="wp-block-heading">一、Softmax 函数是什么？</h2>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 定义：</h3>



<p>Softmax 是一个<strong>将实数向量变换为概率分布</strong>的函数。设输入为向量 z = [z<sub>1</sub>, z<sub>2</sub>, ......, z<sub>n</sub>]，Softmax 输出为：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mi>S</mi><mi>o</mi><mi>f</mi><mi>t</mi><mi>m</mi><mi>a</mi><mi>x</mi></mrow><mo stretchy="false">(</mo><msub><mi>z</mi><mi>i</mi></msub><mo stretchy="false">)</mo><mo>=</mo><mfrac><msup><mi>e</mi><mrow><msub><mi>z</mi><mi>i</mi></msub></mrow></msup><mrow><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>j</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></munderover><msup><mi>e</mi><mrow><msub><mi>z</mi><mi>j</mi></msub></mrow></msup></mrow></mfrac></math>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 特点：</h3>



<ul class="wp-block-list">
<li>输出值范围在 (0, 1) 之间；</li>



<li>所有输出值加起来是 1，所以可以看作是一个概率分布；</li>



<li>最大值对应的类的概率最大，适用于分类问题。</li>
</ul>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 应用场景：</h3>



<ul class="wp-block-list">
<li>多分类神经网络的输出层；</li>



<li>强化学习中的策略分布；</li>



<li>注意力机制中的归一化权重（softmax attention）。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">二、交叉熵函数是什么？</h2>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 定义：</h3>



<p>交叉熵衡量的是两个概率分布之间的差异，常用于度量模型输出的概率分布与真实标签之间的距离。</p>



<p>设真实标签为 one-hot 编码的向量 Y，预测输出为 softmax 概率 y，交叉熵损失函数定义为： </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mtext>CrossEntropy</mtext><mo stretchy="false">(</mo><mrow><mi mathvariant="bold">Y</mi></mrow><mo>,</mo><mrow><mrow><mi mathvariant="bold">y</mi></mrow></mrow><mo stretchy="false">)</mo><mo>=</mo><mo>−</mo><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></munderover><msub><mi>Y</mi><mi>i</mi></msub><mi>log</mi><mo data-mjx-texclass="NONE">⁡</mo><mo stretchy="false">(</mo><msub><mi>y</mi><mi>i</mi></msub><mo stretchy="false">)</mo></math>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 实际效果：</h3>



<ul class="wp-block-list">
<li>如果预测 y<sub>i</sub> 趋近于真实标签对应的 1，则损失趋近于 0；</li>



<li>如果预测错误（即真实标签对应的 y<sub>i</sub> 趋近于 0），则损失趋近于 +∞；</li>



<li>因此它鼓励模型“自信且正确”地做出分类。</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading has-text-align-center">↓那么就可以用softmax+交叉熵来证明一个非常简洁的求偏导公式↓</h2>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">三、Softmax + CrossEntropy 的组合：为什么要一起用？</h2>



<p>在 PyTorch、TensorFlow 等框架中，这两个通常<strong>合并为一个函数</strong>（如 <code>F.cross_entropy()</code>），原因如下：</p>



<ol class="wp-block-list">
<li><strong>数值稳定性</strong>：单独计算 softmax 后再计算 log，容易造成梯度爆炸或 underflow；</li>



<li><strong>优化效率</strong>：合并后的函数可以直接用更快更稳定的算法实现；</li>



<li><strong>梯度简洁</strong>：反向传播时，Softmax + CrossEntropy 的梯度简洁优雅，便于学习和实现。</li>
</ol>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 反向传播的梯度（多分类）：</h3>



<p>设 L 为交叉熵损失，z<sub>i</sub> 为 softmax 输入，Y<sub>i</sub> 为真实标签（one-hot），则： </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>L</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>i</mi></msub></mrow></mfrac><mo>=</mo><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><mo>−</mo><msub><mi>Y</mi><mi>i</mi></msub></math>



<p>这意味着反向传播时，只需<strong>预测概率 - 实际标签</strong>，非常高效！</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">四、举个例子</h2>



<p>假设我们有三个类别，模型输出 logits 是： z=[2.0,1.0,0.1]</p>



<p>用 softmax 得到： y=Softmax(z)=[0.659,0.242,0.099]</p>



<p>假设真实标签是第一类（即 Y = [1, 0, 0]），那么交叉熵损失是： −log⁡(0.659)≈0.417</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>而对于此处的【三】，推理证明过程如下：</p>



<p>这个简洁优雅的偏导公式：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>i</mi></msub></mrow></mfrac><mo>=</mo><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><mo>−</mo><msub><mi>Y</mi><mi>i</mi></msub></math>



<p>或者向量形式：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mrow><mi mathvariant="bold">z</mi></mrow></mrow></mfrac><mo>=</mo><mrow><mrow><mi mathvariant="bold">y</mi></mrow></mrow><mo>−</mo><mrow><mi mathvariant="bold">Y</mi></mrow></math>



<p>是 Softmax + CrossEntropy 的组合在反向传播中的<strong>黄金公式</strong>，我们下面用<strong>逐步推导法</strong>来详细证明它，逻辑清晰，结果精确。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9e0.png" alt="🧠" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 前提定义</h2>



<p>设：</p>



<p>模型输出 logits 向量为 z = [z<sub>1</sub>, z<sub>2</sub>, ......, z<sub>n</sub>]</p>



<p>softmax 输出为 </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><mo>=</mo><mfrac><msup><mi>e</mi><mrow><msub><mi>z</mi><mi>i</mi></msub></mrow></msup><mrow><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>j</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></munderover><msup><mi>e</mi><mrow><msub><mi>z</mi><mi>j</mi></msub></mrow></msup></mrow></mfrac></math>



<p>真实标签为 one-hot 向量 Y = [Y<sub>1</sub>, Y<sub>2</sub>, ......., Y<sub>n</sub>]</p>



<p>损失函数为交叉熵：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>E</mi><mo>=</mo><mo>−</mo><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></munderover><msub><mi>Y</mi><mi>i</mi></msub><mi>log</mi><mo data-mjx-texclass="NONE">⁡</mo><mo stretchy="false">(</mo><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><mo stretchy="false">)</mo></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 目标：求 E对z<sub>k</sub>的偏导</h2>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f536.png" alt="🔶" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 第一步：链式法则分步计算</h3>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac><mo>=</mo><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></munderover><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mrow></mfrac><mo>⋅</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f536.png" alt="🔶" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 第二步：分别求导</h3>



<h4 class="wp-block-heading">① 交叉熵对 softmax 输出的导数</h4>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mrow></mfrac><mo>=</mo><mo>−</mo><mfrac><msub><mi>Y</mi><mi>i</mi></msub><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mfrac></math>



<h4 class="wp-block-heading">② softmax 输出对 z 的偏导</h4>



<p>分情况讨论（这是关键）： </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac><mo>=</mo><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">{</mo><mtable columnalign="left left" columnspacing="1em" rowspacing=".2em"><mtr><mtd><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><mo stretchy="false">)</mo></mtd><mtd><mtext>if&nbsp;</mtext><mi>i</mi><mo>=</mo><mi>k</mi></mtd></mtr><mtr><mtd><mo>−</mo><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub></mtd><mtd><mtext>if&nbsp;</mtext><mi>i</mi><mo>≠</mo><mi>k</mi></mtd></mtr></mtable><mo data-mjx-texclass="CLOSE" fence="true" stretchy="true" symmetric="true"></mo></mrow></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f536.png" alt="🔶" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 第三步：合并两部分</h3>



<p>将上面两项相乘并相加： </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac><mo>=</mo><munderover><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow><mi>n</mi></mrow></munderover><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">(</mo><mo>−</mo><mfrac><msub><mi>Y</mi><mi>i</mi></msub><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mfrac><mo data-mjx-texclass="CLOSE">)</mo></mrow><mo>⋅</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac></math>



<p>我们将它拆分为两部分：</p>



<h4 class="wp-block-heading">当 i = k：</h4>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">(</mo><mo>−</mo><mfrac><msub><mi>Y</mi><mi>k</mi></msub><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub></mfrac><mo data-mjx-texclass="CLOSE">)</mo></mrow><mo>⋅</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo stretchy="false">)</mo><mo>=</mo><mo>−</mo><msub><mi>Y</mi><mi>k</mi></msub><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo stretchy="false">)</mo></math>



<h4 class="wp-block-heading">当 i ≠ k：</h4>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><munder><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>≠</mo><mi>k</mi></mrow></munder><mrow data-mjx-texclass="INNER"><mo data-mjx-texclass="OPEN">(</mo><mo>−</mo><mfrac><msub><mi>Y</mi><mi>i</mi></msub><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub></mfrac><mo data-mjx-texclass="CLOSE">)</mo></mrow><mo>⋅</mo><mo stretchy="false">(</mo><mo>−</mo><msub><mrow><mi>y</mi></mrow><mi>i</mi></msub><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo stretchy="false">)</mo><mo>=</mo><munder><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>≠</mo><mi>k</mi></mrow></munder><msub><mi>Y</mi><mi>i</mi></msub><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo>=</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><munder><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>≠</mo><mi>k</mi></mrow></munder><msub><mi>Y</mi><mi>i</mi></msub></math>



<p>而 one-hot 编码下 </p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><munder><mo data-mjx-texclass="OP">∑</mo><mrow><mi>i</mi><mo>≠</mo><mi>k</mi></mrow></munder><msub><mi>Y</mi><mi>i</mi></msub><mo>=</mo><mn>1</mn><mo>−</mo><msub><mi>Y</mi><mi>k</mi></msub></math>



<p>，因此：</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac><mo>=</mo><mo>−</mo><msub><mi>Y</mi><mi>k</mi></msub><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo stretchy="false">)</mo><mo>+</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><msub><mi>Y</mi><mi>k</mi></msub><mo stretchy="false">)</mo><mo>=</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo>−</mo><msub><mi>Y</mi><mi>k</mi></msub></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 总结结果</h2>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><msub><mi>z</mi><mi>k</mi></msub></mrow></mfrac><mo>=</mo><msub><mrow><mi>y</mi></mrow><mi>k</mi></msub><mo>−</mo><msub><mi>Y</mi><mi>k</mi></msub><mstyle scriptlevel="0"><mspace width="1em"></mspace></mstyle><mo stretchy="false">⇒</mo><mstyle scriptlevel="0"><mspace width="1em"></mspace></mstyle><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mrow><mi mathvariant="bold">z</mi></mrow></mrow></mfrac><mo>=</mo><mrow><mrow><mi mathvariant="bold">y</mi></mrow></mrow><mo>−</mo><mrow><mi mathvariant="bold">Y</mi></mrow></math>



<p>这个推导成立的前提是<strong>使用 softmax + cross entropy 的组合，并且标签是 one-hot 向量</strong>。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f393.png" alt="🎓" class="wp-smiley" style="height: 1em; max-height: 1em;" /> 延伸说明：为什么这很重要？</h2>



<ul class="wp-block-list">
<li>这个公式使得反向传播极其高效，避免了显式求导和链式展开；</li>



<li>框架如 PyTorch、TensorFlow 在 <code>CrossEntropyLoss</code> 中自动融合了 softmax 和 log 操作，实现了这个精确求导；</li>



<li>也说明了 softmax 输出已经是概率分布，损失的梯度本质上就是“预测误差”。</li>
</ul>



<p>上面就是多层神经网络的第二个改进，这个是比原来的E要好的（↓）</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>E</mi><mo>=</mo><mfrac><mn>1</mn><mn>2</mn></mfrac><mrow><mo stretchy="false">|</mo></mrow><mi>y</mi><mo>−</mo><mi>Y</mi><msup><mo stretchy="false">|</mo><mn>2</mn></msup></math>



<p>多层神经网络的第三个改进是随机梯度下降法SGD，要点如下：</p>



<ol class="wp-block-list">
<li>不用输入每个样本就去更新参数；而是输入一批样本（BATCH/MINI-MATCH)，求出这些样本的梯度平均值后，根据平均值来改变参数。</li>



<li><strong><mark style="background-color:#f78da7" class="has-inline-color has-black-color">在神经网络训练中，样本数（BATCH-SIZE）大概设置在50-200之间</mark></strong>
<ul class="wp-block-list">
<li><mark style="background-color:#f78da7" class="has-inline-color">按照BATCH遍历所有训练样本一次，这一次称为一个<strong>EPOCH</strong>。</mark></li>
</ul>
</li>



<li>对于所有训练数据，根据BATCH-SIZE分割为不同的BATCH。</li>
</ol>



<p><strong>实际训练中，根据BATCH多次遍历所有训练样本，即训练不止一个EPOCH，增加BATCH中训练样本的随机性。</strong></p>



<p><em>本章节的思考部分，请至第二页继续阅读<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></em></p>


]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.7后向传播算法</title>
		<link>https://www.leexinghai.com/aic/3-7%e5%90%8e%e5%90%91%e4%bc%a0%e6%92%ad%e7%ae%97%e6%b3%95/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Wed, 20 Aug 2025 00:57:24 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3214</guid>

					<description><![CDATA[本讲分为两个部分。第一部分讲解梯度下降算法的具体求解过程（链式求导法）；第二部分讲解更一般的神经网络（神经网络 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h3 class="wp-block-heading">本讲分为两个部分。第一部分讲解梯度下降算法的具体求解过程（链式求导法）；第二部分讲解更一般的神经网络（神经网络的矩阵方式）。您可根据文末分页按钮选择需要的部分阅读。</h3>



<p>梯度下降算法的具体求解过程，或称 链式求导法</p>



<p>∵</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>E</mi><mo>=</mo><mfrac><mn>1</mn><mn>2</mn></mfrac><mo stretchy="false">(</mo><mi>y</mi><mo>−</mo><mi>Y</mi><msup><mo stretchy="false">)</mo><mn>2</mn></msup></math>



<p>∴</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>y</mi></mrow></mfrac><mo>=</mo><mi>y</mi><mo>−</mo><mi>Y</mi></math>



<p>接着根据偏导数的链式求导法则：</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="367" height="130" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-58.png" alt="" class="wp-image-3232" style="width:128px" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-58.png 367w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-58-300x106.png 300w" sizes="auto, (max-width: 367px) 100vw, 367px" /></figure>



<h2 class="wp-block-heading">继续用链式求导法则：</h2>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="361" height="117" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-60.png" alt="" class="wp-image-3234" style="width:128px" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-60.png 361w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-60-300x97.png 300w" sizes="auto, (max-width: 361px) 100vw, 361px" /></figure>



<p>根据上式，有：</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="466" height="121" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-59.png" alt="" class="wp-image-3233" style="width:192px" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-59.png 466w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-59-300x78.png 300w" sizes="auto, (max-width: 466px) 100vw, 466px" /></figure>



<p>因为已经有：</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="270" height="130" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-56.png" alt="" class="wp-image-3229" style="width:128px"/></figure>



<p>又因为y=ω<sub>1</sub>z<sub>1</sub>+ω<sub>2</sub>z<sub>2</sub>+b<sub>3</sub></p>



<p>所以：</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="195" height="120" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-61.png" alt="" class="wp-image-3235" style="width:128px"/></figure>



<p>同理因为z<sub>1</sub>=φ(α<sub>1</sub>)</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="296" height="117" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-62.png" alt="" class="wp-image-3236" style="width:138px"/></figure>



<h2 class="wp-block-heading">综合可得</h2>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="462" height="111" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-63.png" alt="" class="wp-image-3237" style="width:192px" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-63.png 462w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-63-300x72.png 300w" sizes="auto, (max-width: 462px) 100vw, 462px" /></figure>



<p>经过相似推导，有：</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="715" height="128" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-64.png" alt="" class="wp-image-3238" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-64.png 715w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-64-300x54.png 300w" sizes="auto, (max-width: 715px) 100vw, 715px" /></figure>



<p>通过三个枢纽位置的偏导数求出九个偏导数：</p>



<p>由于：y=ω<sub>1</sub>z<sub>1</sub>+ω<sub>2</sub>z<sub>2</sub>+b<sub>3</sub></p>



<p>有：</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="457" height="346" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-65.png" alt="" class="wp-image-3239" style="width:224px" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-65.png 457w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-65-300x227.png 300w" sizes="auto, (max-width: 457px) 100vw, 457px" /></figure>



<p>由于a<sub>1</sub>=ω<sub>11</sub>x<sub>1</sub>+ω<sub>21</sub>x<sub>2</sub>+b<sub>1</sub></p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="733" height="378" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-66.png" alt="" class="wp-image-3240" style="width:512px" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-66.png 733w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/image-66-300x155.png 300w" sizes="auto, (max-width: 733px) 100vw, 733px" /></figure>



<h2 class="wp-block-heading">总结（第1部分）</h2>



<p>1.对神经网络每一层各个神经元，随机选取相应的ω，b的值</p>



<p>2.设置目标函数E，例如E=1/2 (y-Y)<sup>2</sup> 用后向传播算法对每一个ω，b计算</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>ω</mi></mrow></mfrac><mo>和</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>b</mi></mrow></mfrac></math>



<p>3.然后用</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msup><mi>ω</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></msup><mo>=</mo><msup><mi>ω</mi><mi>n</mi></msup><mo>−</mo><mi>α</mi><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>ω</mi></mrow></mfrac><mrow><mo stretchy="false">|</mo></mrow><msup><mi>ω</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup><mo>,</mo><msup><mi>b</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup><mspace linebreak="newline"></mspace><mspace linebreak="newline"></mspace></math>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msup><mi>b</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></msup><mo>=</mo><msup><mi>b</mi><mi>n</mi></msup><mo>−</mo><mi>α</mi><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>b</mi></mrow></mfrac><mrow><mo stretchy="false">|</mo></mrow><msup><mi>ω</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup><mo>,</mo><msup><mi>b</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup></math>



<p>更新ω和b的值。</p>



<p>4.回到2.不断循环，直到所有</p>



<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>ω</mi></mrow></mfrac><mrow><mo stretchy="false">|</mo></mrow><msup><mi>ω</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup><mo>,</mo><msup><mi>b</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup><mspace linebreak="newline"></mspace><mspace linebreak="newline"></mspace><mo>和</mo><mfrac><mrow><mi mathvariant="normal">∂</mi><mi>E</mi></mrow><mrow><mi mathvariant="normal">∂</mi><mi>b</mi></mrow></mfrac><mrow><mo stretchy="false">|</mo></mrow><msup><mi>ω</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup><mo>,</mo><msup><mi>b</mi><mrow><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></mrow></msup></math>



<p>很小为止，退出循环。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">本部分的后向传播算法结束。请点击【第2页】来看一种更一般的神经网络<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h2>


]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.6梯度下降算法</title>
		<link>https://www.leexinghai.com/aic/3-6%e6%a2%af%e5%ba%a6%e4%b8%8b%e9%99%8d%e7%ae%97%e6%b3%95/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Tue, 19 Aug 2025 02:12:00 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3209</guid>

					<description><![CDATA[这么个流程： 👇假定神经网络是某一种结构 👇将一堆训练数据输入到这个网络中 ⚠估计这个网络的待求参数 ⚠算法模 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>这么个流程：</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" />假定神经网络是某一种结构</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" />将一堆训练数据输入到这个网络中</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/26a0.png" alt="⚠" class="wp-smiley" style="height: 1em; max-height: 1em;" />估计这个网络的待求参数</p>



<h2 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/26a0.png" alt="⚠" class="wp-smiley" style="height: 1em; max-height: 1em;" />算法模型的复杂度要和训练样本的复杂度匹配=>对于训练样本很多的情况->增加神经网络的层数和神经元的个数</h2>



<p>统一式子表达：y=(ω<sub>1</sub>ω<sub>11</sub>+ω<sub>2</sub>ω<sub>21</sub>)x<sub>1</sub>+(ω<sub>1</sub>ω<sub>12</sub>+ω<sub>2</sub>ω<sub>22</sub>)x<sub>2</sub>+(ω<sub>1</sub>b<sub>1</sub>+ω<sub>2</sub>b<sub>2</sub>+b<sub>3</sub>)【上一章的东西】</p>



<p>使小y和标签Y尽可能接近：</p>



<p>Min：E(ω，b)=<mark style="background-color:#f78da7" class="has-inline-color has-black-color">E（X,Y）</mark>[(Y-y)<sup>2</sup>]</p>



<p><mark style="background-color:#f78da7" class="has-inline-color">遍历训练样本及标签的数学期望</mark></p>



<p>由于y=>ω，b的非凸函数</p>



<p>用<strong><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color">梯度下降法（Gradient Descent Method）</mark></strong>求解局部极小值</p>



<p>（1）随机选取ω和b的初始值（ω<sup>(0)</sup>,b<sup>(0)</sup>）</p>



<p>（2）应用迭代算法发求目标函数的局部极值</p>



<p>在第n步迭代中，ω和b的更新公式如下：</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1920" height="1080" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/3fc192e10ae9a5e3613668bd38aa625c.jpg" alt="" class="wp-image-3210" style="object-fit:cover" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/3fc192e10ae9a5e3613668bd38aa625c.jpg 1920w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/3fc192e10ae9a5e3613668bd38aa625c-300x169.jpg 300w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/3fc192e10ae9a5e3613668bd38aa625c-1024x576.jpg 1024w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/3fc192e10ae9a5e3613668bd38aa625c-768x432.jpg 768w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/3fc192e10ae9a5e3613668bd38aa625c-1536x864.jpg 1536w" sizes="auto, (max-width: 1920px) 100vw, 1920px" /></figure>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="906" src="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/b4aede9c46739e4c1508e834ba5b6c43-1024x906.jpg" alt="" class="wp-image-3211" srcset="https://www.leexinghai.com/aic/wp-content/uploads/2025/08/b4aede9c46739e4c1508e834ba5b6c43-1024x906.jpg 1024w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/b4aede9c46739e4c1508e834ba5b6c43-300x265.jpg 300w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/b4aede9c46739e4c1508e834ba5b6c43-768x680.jpg 768w, https://www.leexinghai.com/aic/wp-content/uploads/2025/08/b4aede9c46739e4c1508e834ba5b6c43.jpg 1077w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.5多层神经网络</title>
		<link>https://www.leexinghai.com/aic/3-5%e5%a4%9a%e5%b1%82%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Tue, 19 Aug 2025 01:31:32 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3205</guid>

					<description><![CDATA[结构： α1=ω11x1+ω12x2+b1（第一个神经元） α2=ω21x1+ω22x2+b2（第二个神经元） [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>结构：</p>



<p>α<sub>1</sub>=ω<sub>11</sub>x<sub>1</sub>+ω<sub>12</sub>x<sub>2</sub>+b<sub>1</sub>（第一个神经元）</p>



<p>α<sub>2</sub>=ω<sub>21</sub>x<sub>1</sub>+ω<sub>22</sub>x<sub>2</sub>+b<sub>2</sub>（第二个神经元）</p>



<p>z<sub>1</sub>=φ(α<sub>1</sub>)（非线性函数）</p>



<p>z<sub>2</sub>=φ(α<sub>2</sub>)（非线性函数）</p>



<p>y=ω<sub>1</sub>z<sub>1</sub>+ω<sub>2</sub>z<sub>2</sub>+b<sub>3</sub>（第三个神经元）</p>



<p>展开y=(ω<sub>1</sub>ω<sub>11</sub>+ω<sub>2</sub>ω<sub>21</sub>)x<sub>1</sub>+(ω<sub>1</sub>ω<sub>12</sub>+ω<sub>2</sub>ω<sub>22</sub>)x<sub>2</sub>+(ω<sub>1</sub>b<sub>1</sub>+ω<sub>2</sub>b<sub>2</sub>+b<sub>3</sub>)</p>



<p>层与层之间不加非线性函数=&gt;多层神经网络将会退化到一个神经元的<mark style="background-color:#ff6900" class="has-inline-color">感知器模型状态</mark>。</p>



<p>加入非线性函数：<mark style="background-color:#ff6900" class="has-inline-color">阶跃函数</mark></p>



<p>φ(x)={1(x&gt;0); 0(x&lt;0)</p>



<p>为什么？</p>



<p>如果非线性函数采用阶跃函数，那么三层神经网络可以模拟任何的非线性函数。</p>



<p></p>



<h2 class="wp-block-heading">定理：三层神经网络可以模拟所有的决策面。</h2>



<p></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>3.4人工神经网络的第一次寒冬</title>
		<link>https://www.leexinghai.com/aic/3-4%e4%ba%ba%e5%b7%a5%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e7%9a%84%e7%ac%ac%e4%b8%80%e6%ac%a1%e5%af%92%e5%86%ac/</link>
		
		<dc:creator><![CDATA[李星海]]></dc:creator>
		<pubDate>Tue, 19 Aug 2025 01:04:53 +0000</pubDate>
				<category><![CDATA[机器学习暑假班]]></category>
		<guid isPermaLink="false">https://www.leexinghai.com/aic/?p=3201</guid>

					<description><![CDATA[设y=+1表示X是连通图 设y=-1表示X是非连通图。 用 反证法 证明 识别连通图问题是非线性可分的： 假设 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>设y=+1表示X是连通图</p>



<p>设y=-1表示X是非连通图。</p>



<p>用 反证法 证明 识别连通图问题是非线性可分的：</p>



<p>假设：</p>



<p>这个问题是线性可分的，那么一定存在（ω，b）：</p>



<p>当y=+1时，ω<sup>T</sup>x+b≥0；</p>



<p>当y=-1是，ω<sup>T</sup>x+b＜0</p>



<p>图1连通：y=+1</p>



<p>2（1）1+2+3+5+6+b≥0</p>



<p>5（2）1+2+3+4+7+b≥0</p>



<p>口-（3）1+2+3+4+5+b＜0</p>



<p>-口（4）1+2+3+6+7+b＜0</p>



<p>公式1+2，有2[1+2+3+b]+4+5+6+7≥0</p>



<p>公式3+4，有2[1+2+3+b]+4+5+6+7&lt;0</p>



<p>那么两个新公式相同，不可能同时＞0，＜0.</p>



<p>所以识别连通图的问题是非线性可分的。</p>



<h2 class="wp-block-heading">思考：请证明【一笔画问题】是【非线性可分】问题。即区分两类二值图像，一种可以用一笔画出，另一种不能一笔画出。要证明这个问题的非线性可分问题。</h2>



<h2 class="wp-block-heading">结论</h2>



<p>“能否一笔画完”的判定 <strong>不是线性可分的</strong>。<br>下面给出一种 <strong>构造-反证</strong>：把经典的 <strong>XOR（奇偶）问题</strong> 嵌入到“一笔画”判定里；因为 XOR 本身在任何原始特征空间都不可线性分离，一旦它是“一笔画”问题的子问题，就说明整题也必然非线性可分。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">1 预备：一笔画⇔顶点奇偶性</h2>



<p>对无向图 G=(V,E) 来说 </p>



<p>G 可一笔画  ⟺  ∣{v∈V∣deg⁡(v) 为奇}∣  =0 或 2.</p>



<p>于是只要我们能构造一个极小图，使</p>



<ul class="wp-block-list">
<li><strong>偶数条边 ⇒ 奇点数为 0 或 2 (阳性)</strong></li>



<li><strong>奇数条边 ⇒ 奇点数 > 2 (阴性)</strong></li>
</ul>



<p>就形成了 “边数奇偶＝类别标签” 的 <strong>奇偶判定</strong>。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">2 构造一个 3 条边的“星形”子问题</h2>



<pre class="wp-block-code"><code>      A
      |
     x1
      |
      O —— x2 —— B
      |
     x3
      |
      C
</code></pre>



<ul class="wp-block-list">
<li>顶点：中心 O和三个端点 A,B,C。</li>



<li>可选边（特征） x<sub>1</sub>=OA,  x<sub>2</sub>=OB,  x<sub>3</sub>=OC ∈{0,1}.</li>
</ul>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>数据集 D</strong>（8 个可能图，用 (x1,x2,x3) 表示）</p>
</blockquote>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>边的组合</th><th>奇点个数</th><th>是否一笔画</th><th>类别</th></tr></thead><tbody><tr><td>(0,0,0)</td><td>0</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td>(1,0,0)</td><td>2</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td>(0,1,0)</td><td>2</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td>(0,0,1)</td><td>2</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td>(1,1,0)</td><td>2</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td>(1,0,1)</td><td>2</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td>(0,1,1)</td><td>2</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>+1</td></tr><tr><td><strong>(1,1,1)</strong></td><td><strong>4</strong></td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /></td><td>–1</td></tr></tbody></table></figure>



<p>观察可知： </p>



<p>类别  =  {</p>



<ul class="wp-block-list">
<li>+1,若 x1+x2+x3 为偶数,</li>



<li>−1,若 x1+x2+x3 为奇数</li>
</ul>



<p>这正是三维 <strong>偶／奇 (Parity)</strong> 函数——它是经典的 <strong>XOR</strong> 在 3 维的推广。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">3 XOR / 奇偶函数为什么线性不可分？</h2>



<p>给出一种简洁的几何论证（Minsky &amp; Papert 1969 同款）：</p>



<ul class="wp-block-list">
<li>正样本集合<br>P={(0,0,0),(1,1,0),(1,0,1),(0,1,1)} 的 <strong>凸包</strong><br>含有点 (1/2,1/2,1/2)。</li>



<li>负样本集合<br>N={(1,0,0),(0,1,0),(0,0,1),(1,1,1)} 的凸包<br><strong>同样</strong> 含有该点(1/2,1/2,1/2)。</li>
</ul>



<p>若两类能被一条超平面分开，则其凸包必须互不相交；<br>但这里两凸包 <strong>相交</strong>，故不存在任何线性分割面。<br>因此 Parity / XOR <strong>非线性可分</strong>。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">4 归结到一笔画问题</h2>



<ul class="wp-block-list">
<li>我们已经把“一笔画”中的一个小子集 D映射成 <strong>Parity</strong>；</li>



<li>若整体问题可被一个超平面分割，则 D 也必能被同一超平面分割——与上一节矛盾。</li>
</ul>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>所以</strong>：区分“一笔画图”和“非一笔画图”的整体二分类任务 <strong>不存在</strong> 任何基于原始 0/1 像素（边存在与否）的线性判别函数；即它是 <strong>非线性可分问题</strong>。</p>
</blockquote>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">5 结论与启示</h2>



<ul class="wp-block-list">
<li><strong>证明思路</strong>：把已知的 XOR 难题嵌入目标任务<br>→ 找到凸包相交<br>→ 否定线性可分性。</li>



<li><strong>工程意义</strong>
<ul class="wp-block-list">
<li>想要自动判别一笔画图片，必须借助 <strong>核技巧 / 深度模型 / 非线性特征</strong>；</li>



<li>单层感知器或任何纯线性模型都无法完成该区分。</li>
</ul>
</li>
</ul>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
