<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>BeZer0 Space</title>
        <link>https://blog.bezer0.cn/</link>
        <description>Embracing life as a vast wilderness</description>
        <lastBuildDate>Wed, 21 Oct 2026 12:29:28 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>zh-CN</language>
        <copyright>All rights reserved 2026, BeZer0</copyright>
        <item>
            <title><![CDATA[第五章 神经网络]]></title>
            <link>https://blog.bezer0.cn/learn-deep-learning/learn-deep-learning-5</link>
            <guid>https://blog.bezer0.cn/learn-deep-learning/learn-deep-learning-5</guid>
            <pubDate>Tue, 20 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[第五章 神经网络]]></description>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-3ff0bc13b7ac801485fce5c26ea3ea2d"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac8070b81ffe76a5873b22" data-id="3ff0bc13b7ac8070b81ffe76a5873b22"><span><div id="3ff0bc13b7ac8070b81ffe76a5873b22" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8070b81ffe76a5873b22" title="一、神经元模型"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一、神经元模型</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac807095aaee854cc68678"><b>神经网络（neural networks）</b> 中最基本的成分是<b>神经元（neuron）</b> 模型，在这个模型中，神经元接收到来自 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个其他神经元传递过来的输入信号，这些输入信号通过带权重的<b>连接（connection）</b> 进行传递，神经元接收到的总输入值将于神经元的阈值进行比较，然后通过<b>激活函数（activation function）</b> 处理以产生神经元的输出。</div><div class="notion-text notion-block-3ff0bc13b7ac8052bbc2cd0c99862c56">理想的激活函数是阶跃函数，常用的激活函数是 Sigmoid ：</div><div class="notion-text notion-block-3ff0bc13b7ac80d18405e9445783a6e6">在这里插入图片描述</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac800c8b58ccf5b9ef2892"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fa9574d1a868f8a0c0b3875ef2405c338.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-800c-8b58-ccf5b9ef2892&amp;t=3ff0bc13-b7ac-800c-8b58-ccf5b9ef2892" alt="在这里插入图片描述" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">在这里插入图片描述</figcaption></div></figure><div class="notion-text notion-block-3ff0bc13b7ac8001a06ade0c5d5aef23">把许多个这样的神经元按一定的层次结构连接起来，就得到了神经网络。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac803dbe5ac74504492373" data-id="3ff0bc13b7ac803dbe5ac74504492373"><span><div id="3ff0bc13b7ac803dbe5ac74504492373" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac803dbe5ac74504492373" title="二、感知机与多层网络"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二、感知机与多层网络</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac80a4a344f4cdc42836aa"><b>感知机（Perceptron）</b> 由两层神经元组成，输入层接收外界输入信号后传递给输出层，输出层是 M-P 神经元，亦称<b>阈值逻辑单元（threshold logic unit）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac8067a305e7fafb1b387e">在这里插入图片描述</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac8043a559e08e386ea2bb"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fe07126ffcc3a5638998b49e869f11c40.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8043-a559-e08e386ea2bb&amp;t=3ff0bc13-b7ac-8043-a559-e08e386ea2bb" alt="在这里插入图片描述" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">在这里插入图片描述</figcaption></div></figure><div class="notion-text notion-block-3ff0bc13b7ac800cb4b7db3dfbfbf27a">感知机能容易地实现逻辑与、或、非运算。假定 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是阶跃函数，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>，则：</div><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac804f9ba0e6d322712963"><li>与（<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>）：令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>，仅在 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 时，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ；</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac80afb8c2c0b7d4ea8e6c"><li>或（<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>）：令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>，当 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 或 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 时，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ；</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac801390cfeb3a84ec6159"><li>非（<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>）：令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，当 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 时，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ；当 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 时，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。</li></ul><div class="notion-text notion-block-3ff0bc13b7ac80cf9430d06583a0f426">更一般地，给定训练数据集，权重 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 以及阈值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可通过学习得到。阈值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可以看作一个固定输入为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的<b>哑结点（dummy node）</b> 所对应的连接权重 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，这样，权重和阈值的学习就可统一为权重的学习。对训练样例 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，若当前感知机的输出为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则感知机学习规则如下：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80cca78ad867d2aa823b">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 称为<b>学习率（learning rate）</b>。从上式中可以发现，若预测正确，则感知机不发生变化，否则将根据错误的程度进行权重调整。</div><div class="notion-text notion-block-3ff0bc13b7ac80a5a5f7d2c0653641b2">由于感知机的结构过于简单，只能用于解决线性可分的问题。面对复杂的非线性问题时，需要考虑多层功能神经元。包含隐层即为多层网络，神经网络的学习过程就是根据训练数据来调整神经元之间的<b>连接权（connection weight）</b> 以及每个功能神经元的阈值。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac802ead4ee16fa99e7a5d" data-id="3ff0bc13b7ac802ead4ee16fa99e7a5d"><span><div id="3ff0bc13b7ac802ead4ee16fa99e7a5d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac802ead4ee16fa99e7a5d" title="三、误差逆传播算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">三、误差逆传播算法</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac8063aea9cd7186cca7dd">多层网络的学习能力比单层感知机强很多，但要训练多层网络，式 5.1 显然过于简单，因此需要使用<b>误差逆传播（BackPropagation, BP）算法</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac8059bff8c7021688df4f">给定数据集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，即输入示例由 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 各属性描述，输出 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 维实值向量：</div><div class="notion-text notion-block-3ff0bc13b7ac804e9f00f36883397294">在这里插入图片描述</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac8034aa4dcdf98168f8e0"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F37583581405ecc53fc3c3162a35027d0.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8034-aa4d-cdf98168f8e0&amp;t=3ff0bc13-b7ac-8034-aa4d-cdf98168f8e0" alt="在这里插入图片描述" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">在这里插入图片描述</figcaption></div></figure><div class="notion-text notion-block-3ff0bc13b7ac8023b56cc11b1fe52dc5">上图给出了一个拥有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 各输入神经元、<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个输出神经元、<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个隐层神经元的多层前馈网络结构，其中输出层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元的阈值用 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示，隐层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元的阈值用 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示。输入层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元与隐层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元之间的连接权维 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，隐层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元与输出层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元之间的连接权为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。记隐层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元接收到的输入为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，输出层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元接收到的输入为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为隐层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元的输出。假设隐层和输出层神经元都是用 Sigmoid 函数。</div><div class="notion-text notion-block-3ff0bc13b7ac8039ac71e91932be79ff">对训练例 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，假定神经网络的输出为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，即输出层第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个神经元的输入与阈值的差函数：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80399ec2e52d96de9d2a">则网络在 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上的均方误差为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80e2a3e2e22c5f8bab66">上图网络中有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个参数需确定：输入层到隐层的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个权值、隐层到输出层的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个权值、<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个隐层神经元的阈值、<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个输出层神经元的阈值。BP 是一个迭代学习算法，在迭代的每一轮中采用广义的感知机学习规则对参数进行更新估计，即与式 5.1 类似，任意参数 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的更新估计式如下：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80ccb4bfc40925b629db">以隐层到输出层的连接权 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为例来进行推导，BP 算法基于<b>梯度下降（gradient descent）</b> 策略，以目标的负梯度方向对参数进行调整。对式 5.4 的误差 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，给定学习率 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac801badade046e8f15f95">注意到 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 先影响到第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个输出层神经元的输入值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，再影响到其输出值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，然后影响到 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8051ba12dfea2f14cdb6">根据 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的定义，显然有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac802db5abdb5c0acd9bbc">由 Sigmoid 函数的性质得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80b2a022c67ac40b324d">因此由式 5.3 和式 5.4 有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80a6b324dddca6e88415">将式 5.10 和 5.8 代入式 5.7 和 5.6 ，得到 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的更新公式：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac806e8de0f10348dd229d">类似可得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80c28411fbfccb9057a0">式 5.13 和 5.14 中：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8005aeb4d78c00af84c2">学习率 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 控制着算法每一轮迭代中的更新步长，若太大则容易振荡，太小则收敛速度又会过慢。在精细调解过程中，可令式 5.11 和 5.12 中的学习率不同，BP 算法的工作流程如下：</div><div class="notion-text notion-block-3ff0bc13b7ac8052a231dfb6363f75f3">在这里插入图片描述</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac8010893eec4f834a5237"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F5a9cf6c246b6742410c8f956d0b2fe38.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8010-893e-ec4f834a5237&amp;t=3ff0bc13-b7ac-8010-893e-ec4f834a5237" alt="在这里插入图片描述" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">在这里插入图片描述</figcaption></div></figure><div class="notion-text notion-block-3ff0bc13b7ac807b90a0e00fe7bd7558">需要注意的是，BP 算法的目标是要最小化训练集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上的累计误差：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80369a67c302f861a958">但是“标准的BP算法”每次仅针对一个训练样例更新连接权和阈值，也就是说上图中的算法更新规则是基于单个 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 推导得到的。如果类似地推导出基于累计误差最小化的更新规则，就得到了<b>累计误差逆传播算法（accumulated error backpropagation）</b>。累计 BP 算法与标准 BP 算法都很常用。</div><div class="notion-text notion-block-3ff0bc13b7ac8036ac08cb99c5256840">一般来说，标准 BP 算法每次更新只针对单个样例，参数更新得非常频繁，而且对不同样例进行更新的效果可能相互抵消。因此为了达到同样的效果，标准 BP 算法往往需要进行更多次数的迭代。累计 BP 算法直接针对累计误差最小化，它在读取整个训练集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 一遍后才对参数进行更新，其参数更新的频率较低，但是当累计误差下降到一定程度之后，进一步下降会非常缓慢，此时标准 BP 往往可以更快获得更好的解，尤其是在大样本数据的情况下。</div><div class="notion-text notion-block-3ff0bc13b7ac80cb8f25e234c3151d0f">有人已经证明，只需一个包含足够多神经元的隐层，多层前馈网络就能以任意精度逼近任意复杂度的连续函数，但是如何设置隐层神经元个数仍然没有定论，实际中常常采用<b>试错法（trial-by-error）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac8031b991cc2f9fceea35">由于 BP 强大的表示能力，因此经常遭遇过拟合现象。为解决这个问题，一种策略是<b>早停（early stopping）</b>：将数据分成训练集和验证集，训练集用来计算梯度、更新连接权和阈值，验证集用来估计误差，若训练集误差降低但验证集误差升高，则停止训练，同时返回具有最小验证集误差的连接权和阈值。第二种策略是<b>正则化（regularization）</b>：在误差目标函数中增加一个用于描述网络复杂度的部分，例如连接权与阈值的平方和。仍令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个训练样例上的误差，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示连接权和阈值，则误差目标函数变为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80b2b630f5619e932947">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 用于对经验误差与网络复杂度这两项进行折中，常通过交叉验证法来估计。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac806c845cf45246056298" data-id="3ff0bc13b7ac806c845cf45246056298"><span><div id="3ff0bc13b7ac806c845cf45246056298" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac806c845cf45246056298" title="四、全局最小与局部极小"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">四、全局最小与局部极小</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac80d1ab36f44c15c8a884">实际上 BP 的优化目标即为寻找一组最优参数使得 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 最小，在求解过程中会遇到两种“最优”：<b>局部最优（local minimum）</b> 和<b>全局最优（global minimum）</b>。对 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，若存在 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 使得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80dbbffac65fc3fab669">都有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 成立，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为局部极小解；弱队参数空间中的任意 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 都有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为全局最小解。</div><div class="notion-text notion-block-3ff0bc13b7ac8034b4abe7137452945f">基于梯度的搜索是使用最为广泛的参数寻优方法。在此类方法中，从某些初始解出发，迭代寻找最优参数值。每次迭代中，首先计算误差函数在当前点的梯度；然后根据负梯度的方向搜索；若误差函数在当前点的梯度为零，则已达到局部极小，更新量将为零。</div><div class="notion-text notion-block-3ff0bc13b7ac80baa575d662eaee7339">在实际任务中，为跳出局部极小从而接近全局最小，可以采用以下策略“</div><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac805ea487f0233513c175"><li>以多组不同参数值初始化多个神经网络，按标准方法训练后，取其中误差最小的解作为最终参数。</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac80f09498ec9a27a99407"><li><b>模拟退火（simulated annealing）</b>：在每一步都以一定的概率接收比当前解更差的结果，迭代过程中，接收次优解的概率要逐渐降低，从而保证算法稳定。</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac808aa5fdf13a525c5cee"><li>使用随机梯度下降：随机梯度下降在计算梯度时加入了随机因素，因此即便在局部极小点，所计算的梯度可能不为零，因此有助于跳出局部极小继续搜索。</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac80c2abceff9315e58224"><li><b>遗传算法（genetic algorithms）</b></li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac80b3b552ff7091cc68dd" data-id="3ff0bc13b7ac80b3b552ff7091cc68dd"><span><div id="3ff0bc13b7ac80b3b552ff7091cc68dd" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80b3b552ff7091cc68dd" title="五、深度学习"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">五、深度学习</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac80468b09cc929baab399">对神经网络模型，提高<b>容量（capacity）</b> 的一个简单方法是增加隐层数目。虽然单隐层的多层前馈网络已具有强大的学习能力，但从复杂度的角度来看，增加隐层数目更有效。因为增加隐层数不尽增加了拥有激活函数的神经元数目，还增加了激活函数嵌套的层数。然而多因曾神经网络难以直接用经典算法进行训练，因为误差在多隐层内逆传播时，往往会<b>发散（diverge）</b> 而不能收敛到稳定状态。</div><div class="notion-text notion-block-3ff0bc13b7ac80daacbbe27952f0ec64"><b>无监督逐层训练（unsupervised layer-wise training）</b> 是多隐层网络训练的有效手段，其基本思想是每次训练一层隐结点，训练时将上一层隐结点的输出作为输入，而本层隐结点的输出作为下一层隐结点的输入，这称为<b>预训练（pre-training）</b>；在预训练全部完成后，在对整个网络进行<b>微调（fine-tuning）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac80e085affc7e88dfc91b">预训练与微调的做法可以看作将大量参数分组，对每组先找到局部来看比较好的设置，然后再基于这些局部较优的结果联合起来进行全局寻优。这样就在利用了模型大量参数所提供的自由度的同时，有效地节省了训练开销。</div><div class="notion-text notion-block-3ff0bc13b7ac80c6880fe137572f728b">另一种策略是<b>权共享（weight sharing）</b>，即让一组神经元使用相同的连接权。这个策略在<b>卷积神经网络（Convolutional Neural Network, CNN）</b> 中发挥很大的作用。</div><div class="notion-text notion-block-3ff0bc13b7ac80c4adf2eb512a939125">通俗来看，深度学习实际上就是将低层的特征转化为高层特征后，用简单模型来完成分类等学习任务。由此可将深度学习理解为进行<b>特征学习（feature learning）</b> 或<b>表示学习（representation learning）</b>。</div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[第三章 线性模型]]></title>
            <link>https://blog.bezer0.cn/learn-deep-learning/learn-deep-learning-3</link>
            <guid>https://blog.bezer0.cn/learn-deep-learning/learn-deep-learning-3</guid>
            <pubDate>Tue, 20 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[第三章 线性模型]]></description>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-3ff0bc13b7ac80ca9db0f00ba88584ac"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac80839bb8ca4a87ee1e01" data-id="3ff0bc13b7ac80839bb8ca4a87ee1e01"><span><div id="3ff0bc13b7ac80839bb8ca4a87ee1e01" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80839bb8ca4a87ee1e01" title="一、基本形式"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一、基本形式</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac8037b559c49ad368abee">给定由 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个属性描述的示例 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 在第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个属性上的取值，<b>线性模型（linear model）</b> 就是学习一个通过属性的线性组合来进行预测的函数：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80b0b890c096387b481d">一般用向量形式写成：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac809395c5f4da3d1b6bb8"><b>非线性模型（nonlinear model）</b> 可在线性模型的基础上引入层级结构或高维映射得到。线性模型具备较好的解释性。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac80adbfe3ea73abe7cdf0" data-id="3ff0bc13b7ac80adbfe3ea73abe7cdf0"><span><div id="3ff0bc13b7ac80adbfe3ea73abe7cdf0" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80adbfe3ea73abe7cdf0" title="二、线性回归"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二、线性回归</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac8083924bda8aa086274e">给定数据集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。<b>线性回归（linear regression）</b> 是学习一个线性模型以尽可能准确地预测实值输出标记。</div><div class="notion-text notion-block-3ff0bc13b7ac80538eefcec2068b4005">线性回归试图学得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac807e903cc52ce3d0350b">要确定 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，可以使均方误差最小：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac800fa0cbc563a7ca49f6">均方误差对应了<b>欧氏距离（Euclidean distance）</b>，基于均方误差最小化来进行模型求解的方法称为<b>最小二乘法（least square method）</b>。在线性回归中，最小二乘法就是找一条直线，使得所有样本到支线上的欧式距离之和最小。</div><div class="notion-text notion-block-3ff0bc13b7ac805a85cbff549308bf08">求解 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 使 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 最小化的过程，称为线性回归模型的最小二乘<b>参数估计（parameter estimation）</b>。可将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 分别对 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 求导，得到：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80eba2efc787df6dafec">然后令上两个式子为 0 可得到 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 最优解的<b>闭式（closed-form）</b> 解：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac804b8461e98f476abb9b">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的均值。</div><div class="notion-text notion-block-3ff0bc13b7ac808bb536def18097fe7f">若考虑样本的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个属性，此时需要学习：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac800fa1f6cfb395e9ccc7">这称为<b>多元线性回归（multivariate linear regression）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac8015861dc12aae7f79c9">同样可以利用最小二乘法来进行估计，令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，数据集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示为一个 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 大小的矩阵 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，其中每行对应一个示例，该行前 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个元素对应于示例的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个属性，最后一个元素恒置为 1 ：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80febb58e4ebe40464f5">把标记写成向量形式：<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac800aa024f161bb18c1ff">令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，对 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 求导得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac808b8e8dd1cc4ad4f760">由矩阵微分公式：<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8069ad18eb6a10fba370">令上式为 0 ，可得 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 最优解的闭式解，当 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为<b>满秩矩阵（full-rank matrix）</b> 或<b>正定矩阵（positive definite matrix）</b> 时，可得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80efa23ce8657a914fa2">令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span>，则最终学得的多元线性回归模型为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80dd8f05e856755ea422">当 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 不满秩时，可解出多个 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，它们都能使均方误差最小化，选择哪一个解作为输出，将由学习算法的归纳偏好决定，常见做法是引入<b>正则化（regularization）</b> 项。</div><div class="notion-text notion-block-3ff0bc13b7ac80a6aa13ee588a8cb720">对于简单的线性模型，可以将模型预测值逼近 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的衍生物，例如可将输出标记的对数作为线性模型逼近的目标：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80329449c2f0d6090664">这就是<b>对数线性回归（log-linear regression）</b>，虽然该式形式上仍是线性回归，但实质上已是在求取输入控件到输出空间的非线性函数映射，如图，该函数可将线性回归模型的预测值与真实标签联系起来。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac8068bd91dac5c2aaa59c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F4e3e524eb666c022606cb06995c0b0b4.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8068-bd91-dac5c2aaa59c&amp;t=3ff0bc13-b7ac-8068-bd91-dac5c2aaa59c" alt="1" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-3ff0bc13b7ac80cebbe7e54a7c28564d">还可考虑单调可微函数 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8074b72fd232aa0cb76e">这样得到的模型成为广义<b>线性模型（generalized linear model）</b>，其中函数 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 称为<b>联系函数（link function）</b>。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac80b28243c2cf9db60a94" data-id="3ff0bc13b7ac80b28243c2cf9db60a94"><span><div id="3ff0bc13b7ac80b28243c2cf9db60a94" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80b28243c2cf9db60a94" title="三、对数几率回归"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">三、对数几率回归</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac807f8188fb448c7ec0b4">考虑二分类任务，其输出标记 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，需要将线性回归模型产生的预测值（实值）转换为离散值，最理想的是<b>阶跃函数（unit-step function）</b>：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac805e89b0f3430c5fee0a"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F21ba92465f47eb108959f82bbea030ac.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-805e-89b0-f3430c5fee0a&amp;t=3ff0bc13-b7ac-805e-89b0-f3430c5fee0a" alt="1" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-3ff0bc13b7ac8095aa31fe6350cf1445">从图中可以看出，单位阶跃函数不连续，因此不能直接使用，于是可以找到一些<b>替代函数（surrogate function）</b>，这些函数单调可微，如<b>对数几率函数（logistic function）</b>：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80918684de0cfa87c0b1">将对数几率函数作为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，得：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8032a427fb16f83813a5">恒等变形：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80d1b35efaff65d0785f">若将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 视为样本 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 作为正例的可能性，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是其反例可能性，两者比值：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac806b8fc5dd469b68d2ef">称为<b>几率（odds）</b>，反映了 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 作为正例的相对可能性，对几率取对数则得到<b>对数几率（log odds, 亦称 logit）</b>：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac803eb219c587f4c04b07">由此可看出，式（3.18）实际上是在用线性回归模型的预测结果去逼近真是标记的对数几率，因此对应的模型称为<b>对数几率回归（logistic regression，亦称 logit regression）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac808a8b71ca7cbf21ee4c">下面来确定式（3.18）中的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，若将式（3.18）中的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 视为类后验概率估计 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则式（3.19）可重写为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac806b87c8f51747bb768b">显然有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80239a92fb36a89384f5">于是，可以通过<b>极大似然法（maximum likelihood method）</b> 来估计 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。给定数据集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，对率回归模型最大化<b>对然似然（log-likelihood）</b>：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8086ad69c4af04f97440">即令每个样本属于其真实标记的概率越大越好。为便于讨论，令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可简写为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。再令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则上式中的似然项可重写为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac802da2ecc7dee8c30afa">将式（3.26）带入（3.25），并根据式（3.23）和（3.24）可知，最大化式（3.25）等价于最小化：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80b1a280c4f681d64c0c">式（3.27）是关于 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的高阶可导连续凸函数，根据凸优化理论，经典的数值优化算法如<b>梯度下降法（gradient descent method）</b>、<b>牛顿法（Newton method）</b> 等都可求得其最优解，于是就得到：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80f7b5a2ceac3ffeca81">以牛顿法为例，其第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 轮迭代解的更新公式为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80319f48feefc848f379">其中关于 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的一阶、二阶导数分别为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac8027bf39de1edf45092c" data-id="3ff0bc13b7ac8027bf39de1edf45092c"><span><div id="3ff0bc13b7ac8027bf39de1edf45092c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8027bf39de1edf45092c" title="四、线性判别分析"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">四、线性判别分析</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac8081b963e79dceaad749">LDA 也是一种对数据进行降维的方法，与 PCA 不同的是，LDA 是一种有监督的数据降维方法。</div><div class="notion-text notion-block-3ff0bc13b7ac8020baa3ddb99c6b25ae">PCA 存在局限性，例如两组本来可分的数据，降维后在新的空间中不可分。</div><div class="notion-text notion-block-3ff0bc13b7ac8080aa14cc0b389277d8">LDA 降维的目标：将带有标签的数据降维，须满足三个条件：</div><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac80e59658e0583a259ab7"><li>尽可能多的保留样本信息；</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac804582c5c0b335a152f7"><li>寻找使样本尽可能好分的投影方向；</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac809dbea8f865be3608e4"><li>投影后使同类样本尽可能接近，不同类样本尽可能远离；</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac807ab8fce0535d704760" data-id="3ff0bc13b7ac807ab8fce0535d704760"><span><div id="3ff0bc13b7ac807ab8fce0535d704760" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac807ab8fce0535d704760" title="1. 优化目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 优化目标</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac80a1bf47dab9513f678e">线性，指将数据点投影到直线上，直线表达式为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac807fa30afeb1c3999684"><span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示样本向量，如果投影到一条直线上 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 就是一个特征向量或者多个特征向量构成的矩阵。<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为投影后的样本点。</div><div class="notion-text notion-block-3ff0bc13b7ac80d89ffce730eb8072a5">首先考虑两类样本的情况，将数据投影到直线 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上，则两类样本的中心在直线上的投影分别为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类样本的均值，若将所有的样本点都投影到直线上，则两类样本的协方差分别为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，投影后同类样本协方差矩阵的计算步骤如下：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80729152df88c53be19e">其中用到了公式：<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。</div><div class="notion-text notion-block-3ff0bc13b7ac80449116fdf67f11055e">上式中间部分 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是同类样本投影前的协方差矩阵。故可以发现若投影前的协方差矩阵为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则投影后的协方差矩阵为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。</div><div class="notion-text notion-block-3ff0bc13b7ac80ecb156e456c92bccb6">要使得同类样例的投影点尽可能接近，可以让同类样本点的协方差矩阵尽可能小，即 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 尽可能小；要使得异类样例的投影点尽可能远离，可以让类中心之间的距离尽可能大，即 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 尽可能大。于是得到最大化的优化目标：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80f6a397d7bdbba4fa97">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示欧几里得范数，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac80028144cd989d982837" data-id="3ff0bc13b7ac80028144cd989d982837"><span><div id="3ff0bc13b7ac80028144cd989d982837" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80028144cd989d982837" title="2. 协方差与样本分布的关系"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 协方差与样本分布的关系</span></span></h3><blockquote class="notion-quote notion-block-3ff0bc13b7ac8082b3fcd0b8acfc115c"><div>要使得同类样例的投影点尽可能接近，可以让同类样本点的协方差矩阵尽可能小，即 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 尽可能小；要使得异类样例的投影点尽可能远离，可以让类中心之间的距离尽可能大，即 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 尽可能大。</div></blockquote><div class="notion-text notion-block-3ff0bc13b7ac80f6b377eebf7f06cb36">为什么协方差矩阵可以反映出样本或样例点之间的距离关系？首先来看协方差和方差的公式：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8089acc5fe601615c644">可以发现两者非常相似。已知方差可以用来度量数据的离散程度，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 越大，表示数据距离离样本中心越远，数据就越离散，方差也就越大。同样的，对于协方差来说，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 越大，表示数据距离样本中心越远，数据分布就越分散，协方差也就越大。因此上述观点成立。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac80d8a916e3ff9a58b6db" data-id="3ff0bc13b7ac80d8a916e3ff9a58b6db"><span><div id="3ff0bc13b7ac80d8a916e3ff9a58b6db" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80d8a916e3ff9a58b6db" title="3. 类间散度矩阵"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 类间散度矩阵</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac80bf9a13c8baa9fdcf20">类间散度矩阵就是协方差矩阵乘以样本数目，其作用基本与协方差矩阵一致。</div><div class="notion-text notion-block-3ff0bc13b7ac800caae9f278cc22f36a">对于两类样本来说：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8034a57de7d69fac5222">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示协方差矩阵，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示样本数目。</div><div class="notion-text notion-block-3ff0bc13b7ac809ebc8dce5c48aabc41">对于多类问题来说：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8000a390f94c2f1c446b">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示类别数目，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示所有样本的均值向量 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。</div><div class="notion-text notion-block-3ff0bc13b7ac80788084e79e7b7e4074">如果只使用类间散度矩阵一个约束条件来对数据进行降维，即让类间的样本投影尽可能远离，参考 PCA 的降维过程：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80d38505da188c29c025">不同的是，为了保证类间的样本投影后尽可能远离，我们应该选择特征值最大的特征向量代表的方向做投影。这样才能保证不同类样本投影之后方差尽可能大，尽可能远离。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac8013997fd3114546b792" data-id="3ff0bc13b7ac8013997fd3114546b792"><span><div id="3ff0bc13b7ac8013997fd3114546b792" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8013997fd3114546b792" title="4. 类内散度矩阵"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">4. 类内散度矩阵</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac80a1b838d41f69d8c9da">对于两类问题来说：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80499e17ebee452251cb">对于多类问题来说：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8072865de2bd9ca30365">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类样本的协方差矩阵。所以 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类样本协方差矩阵之和。</div><div class="notion-text notion-block-3ff0bc13b7ac80799c79fbfb35c8514f">如果只是用类内散度矩阵一个约束条件来对数据进行降维，即让类内的样本投影尽可能接近，参考 PCA 的将为过程：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80cbb047e4c2cea174d6">不同的是，为了保证类内的样本投影后尽可能接近，我们应该选择特征值最小的特征向量代表的方向做投影。这样才能保证同类样本投影之后方差尽可能小，尽可能接近。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac800caa2defd4de764654" data-id="3ff0bc13b7ac800caa2defd4de764654"><span><div id="3ff0bc13b7ac800caa2defd4de764654" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac800caa2defd4de764654" title="5. 优化"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">5. 优化</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac8067b63adabbcc40b77b">定义了类内散度矩阵和类间散度矩阵后，可以将优化目标转换为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80cdbf5aff5aa4e9bbec">即 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 与 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的广义瑞利商。</div><div class="notion-text notion-block-3ff0bc13b7ac800b8733ce80a0f63bd6">接下来确定 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，注意上式的分子和分母都是关于 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的二次项，因此上式的解与 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的长度无关，只与其方向有关。令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则上式等价于：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8004a615da3104825563">使用拉格朗日乘子法，上式等价于：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac807e9ae7d2e5e35cee99">可以看到上式转化为一个求特征值和特征向量的问题。<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是要求解的特征向量，这就验证了之前所说的式子 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 就是特征向量构成的矩阵。</div><div class="notion-text notion-block-3ff0bc13b7ac80728ce9dc349f15b9c8">但是此时仍有一个问题，那就是 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 通常是不可逆的，因此有以下方法解决：</div><ol start="1" class="notion-list notion-list-numbered notion-block-3ff0bc13b7ac8097a79bdbdb017be0ef" style="list-style-type:decimal"><li>令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是一个特别小的数，从而使得 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可逆；</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-3ff0bc13b7ac80a59016d451bdda8dca" style="list-style-type:decimal"><li>先使用 PCA 降维，使得降维后的数据上 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可逆，然后再使用 LDA 。</li></ol><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac8096ae69d5fe22489591" data-id="3ff0bc13b7ac8096ae69d5fe22489591"><span><div id="3ff0bc13b7ac8096ae69d5fe22489591" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8096ae69d5fe22489591" title="五、多分类学习"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">五、多分类学习</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac80838526f420480fa14a">二分类的方法可以推广至多分类任务，简单来说就是对多分类任务拆分为多个二分类任务。最经典的拆分策略有<b>一对一（One vs. One, OvO），一对其余（One vs. Rest，OvR; One vs. All, OvA）和多对多（Many vs. Many, MvM）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac802f987cf15134c898b4">OvO 方式对 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个元素的数据集会生成 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个分类器，在测试阶段将新样本提交给所有的分类器即可得到最终分类的结果。</div><div class="notion-text notion-block-3ff0bc13b7ac80dd9623c2331d34ea6c">OvR 方式是每次将一个类别的样例作为正例，其余类别作为反例来训练 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个分类器，测试时若仅有一个分类器预测为正类，则对应的类别标记作为最终分类结果；若有多个分类器预测为正类，则通常考虑各分类器的预测置信度，选择置信度最大的类别标记作为分类结果。</div><div class="notion-text notion-block-3ff0bc13b7ac802dbebfea44d1e486ed">从上述两种方法可以发现，前者空间开销更大，后者时间开销更大。这是因为前者训练的分类器个数更多，后者每次训练时用到的样本个数更多。</div><div class="notion-text notion-block-3ff0bc13b7ac80d8beddedf0a06890ac">MvM 方法是每次将若干类作为正类，若干类作为反类，因此该方法的构造需要有特殊的设计。一种常用的方法为：<b>纠错输出码（Error Correcting Output Codes, ECOC）</b>，该方法是将编码的思想引入类别拆分，并尽可能在解码过程中具有容错性。ECOC 工作过程主要分为两步：</div><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac8022b292ed731bfdede2"><li>编码：对 N 个类别做 M 次划分，每次划分将一部分类别划为正类，一部分划为反类，从而形成一个二分类训练集；这样一共产生 M 个训练集，可训练出 M 个分类器。</li></ul><ul class="notion-list notion-list-disc notion-block-3ff0bc13b7ac80f09517fc0f741c2175"><li>解码：M 个分类器分别对测试样本进行预测，这些预测标记组成一个编码。将这个预测编码与每个类别各自的编码进行比较，返回其中距离最小的类别作为最终预测结果。</li></ul><div class="notion-text notion-block-3ff0bc13b7ac807eb707daacc58407f5">类别划分通过<b>编码矩阵（coding matrix）</b>指定。编码矩阵有多种形式，常见的主要有<b>二元码</b>和<b>三元码</b>。前者将每个类别分别指定为正类和反类，后者在正、反类之外，还可指定<b>停用类</b>。在图（a）中，分类器 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类的样例作为正例，将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类的样例作为反例；在图（b）中，分类器 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类的样例作为正例，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类作为反例。在解码阶段，各分类器的预测结果联合起来形成了测试示例的编码，该编码与各类所对应的编码进行比较，将距离最小的编码所对应的类别作为预测结果。例如图（a）中，若基于欧氏距离，预测结果将是 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac807d94bcf264aa2458c5"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fbb4aa58fb5db2d162796d2765984b0e9.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-807d-94bc-f264aa2458c5&amp;t=3ff0bc13-b7ac-807d-94bc-f264aa2458c5" alt="在这里插入图片描述" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-3ff0bc13b7ac807a8b15d8df1685136e">之所以叫纠错输出码，是因为在测试阶段，ECOC 编码器对分类器的错误有一定的容忍与修正能力。例如图（a）所示，测试样例的正确预测编码为（-1,+1,+1,-1,+1），假设在预测时 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 分类器出错导致编码为（-1,-1,+1,-1,+1），但是基于该分类仍能正确得出分类结果 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。一般来说，对同一个学习任务，ECOC 编码越长，纠错能力越强，但同时也意味着所需训练的分类器越多，各方开销会增大。另一方面，对有限类别数，可能的组合数目是有限的，这就限定了码长的范围。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac8036b73ec352d1da7b45" data-id="3ff0bc13b7ac8036b73ec352d1da7b45"><span><div id="3ff0bc13b7ac8036b73ec352d1da7b45" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8036b73ec352d1da7b45" title="六、类别不平衡问题"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">六、类别不平衡问题</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac804db3f4d60b27accc7f"><b>类别不平衡（class-imbalance）</b> 就是指分类任务中不同类别的训练样例数目差别很大的情况。本节假设正类样例较少，反类样例较多。</div><div class="notion-text notion-block-3ff0bc13b7ac807cb94af87b1d31b3d9">从线性分类器的角度讨论，使用 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 对新样本进行分类时，事实上是再用预测出的 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 值与一个阈值（通常为 0.5）比较。<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 实际上表达了正例的可能性，几率 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 则反映了正例可能性与反例可能性之比值，阈值设置为 0.5 表明分类器认为而这可能性相同，即分类器决策规则为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80b7b477f3cffdb594c7">然而当二者树木不同时，令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 分别表示正例数目和反例数目，则观测几率是 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，由于通常假设训练集是真实样本总体的无偏采样（表示采样的数据很好的代表了真是样本的情况），因此观测几率代表了真实几率。于是，只要分类器的预测几率高于观测几率就应该判定为正例，即：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80a78387cb2dba61c859">为将式 3.49 转换为式 3.50 ，则令：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80a8bfc9f78a881d2ef3">这就是类别不平衡学习的一个基本策略——<b>再缩放（rescaling），再平衡（rebalance）</b>。该方法也是<b>代价敏感学习（cost-sensitive learning）</b> 的基础。这个方法的重要前提是：训练集是真是样本总体的无偏采样，但是这个假设往往不成立。因此有另外三种方法：</div><ol start="1" class="notion-list notion-list-numbered notion-block-3ff0bc13b7ac8058a420cdafad0bac78" style="list-style-type:decimal"><li>直接对训练集里的反类样例进行<b>欠采样（undersampling）</b>，即去除一些反例使得正、反例数目接近；</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-3ff0bc13b7ac80749a78c77ad28f64d3" style="list-style-type:decimal"><li>对训练集里的正类样例进行<b>过采样（oversampling）</b>，即增加一些正例使得正、反例数目接近；</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-3ff0bc13b7ac80bba0e4cd37ec35ad8f" style="list-style-type:decimal"><li><b>阈值移动（threshold-moving）</b>，直接基于原始训练集进行学习，但在用训练好的分类器进行预测时，将上式嵌入到决策过程中。</li></ol><div class="notion-text notion-block-3ff0bc13b7ac8046a864c0fbcb4c0ff2">需要注意的是，过采样法不能简单地对初始样例样本进行重复采样，否则会导致过拟合。一种代表性的算法为 <b>SMOTE </b>，通过对训练集里的正例进行插值来生成额外的正例。欠采样法若随机丢弃反例，则可能丢失部分重要信息。一种代表性的算法为<b>EasyEnsemble</b>，利用集成学习机制，将反例划分为若干个集合供不同学习器使用，这样对每个学习起来看完成了欠采样，但在全局来看并未丢失信息。</div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[第四章 决策树]]></title>
            <link>https://blog.bezer0.cn/learn-deep-learning/learn-deep-learning-4</link>
            <guid>https://blog.bezer0.cn/learn-deep-learning/learn-deep-learning-4</guid>
            <pubDate>Tue, 20 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[第四章 决策树]]></description>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-3ff0bc13b7ac8060b45cf43ce50046cf"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac8015b758dc3982fb3742" data-id="3ff0bc13b7ac8015b758dc3982fb3742"><span><div id="3ff0bc13b7ac8015b758dc3982fb3742" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8015b758dc3982fb3742" title="一、基本流程"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一、基本流程</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac80648602e9a32d72867e"><b>决策树（decision tree）</b> 是一种常见的机器学习方法。一般的，一棵决策树包含一个根结点、若干个内部结点和若干个叶结点；叶节点对应于决策结果，其他每个结点则对应于一个属性测试；每个节点包含的样本集合根据属性测试的结果被划分到子结点中；根结点包含样本全集。从根结点到每个叶结点的路径对应了一个判定测试序列。决策树学习的目的是为了产生一棵泛化能力强的决策树，算法如下：</div><div class="notion-text notion-block-3ff0bc13b7ac80b1b914e10c4e5da0fc">由此可见，决策树的生成是一个递归过程。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac80e29defc4c518b2e708" data-id="3ff0bc13b7ac80e29defc4c518b2e708"><span><div id="3ff0bc13b7ac80e29defc4c518b2e708" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80e29defc4c518b2e708" title="二、划分选择"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二、划分选择</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac80b8978ae26ada818184">从算法中可以看出，决策树学习的关键是划分最优属性。一般来说，随着划分过程不断进行，要使决策树的分支结点所包含的样本尽可能属于同一类别，即结点的<b>纯度（purity）</b> 越来越高。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac8052ac99e932dbae5b6c" data-id="3ff0bc13b7ac8052ac99e932dbae5b6c"><span><div id="3ff0bc13b7ac8052ac99e932dbae5b6c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac8052ac99e932dbae5b6c" title="1. 信息增益"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 信息增益</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac8090aaf8c83fb193cdc0"><b>信息熵（information entropy）</b> 是度量样本集合纯度最常用的一种指标。假定当前样本集合 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类样本所占比例为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的信息熵定义为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8032bbd1c2ecbff3ca62"><span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的值越小，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的纯度越高。</div><div class="notion-text notion-block-3ff0bc13b7ac80a2bce8e7c878d6e128">假定离散属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个可能的取值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，若使用 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 来对样本集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 进行划分，则会产生 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个分支结点，其中第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个分支结点包含了 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中所有在属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上取值为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的样本，记为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。利用上式可以计算出 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的信息熵，再考虑到不同的分支结点所包含的样本数不同，给分支结点赋予权重 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，即样本数越多的分支结点的影响越大，于是可计算出用属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 对样本集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 进行划分所获得的<b>信息增益（information gain）</b>：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8077a0d0c537f6fee1c7">信息增益越大，则意味着使用属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 来进行划分所获得的<b>纯度提升</b>越大。因此，可用信息增益来进行决策树的划分属性选择。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac804393c0e3f8bcd93558" data-id="3ff0bc13b7ac804393c0e3f8bcd93558"><span><div id="3ff0bc13b7ac804393c0e3f8bcd93558" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac804393c0e3f8bcd93558" title="2. 增益率"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 增益率</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac806590fcdac7c72e7994">信息增益准则对可取值树木较多的属性有所偏好，为减少这种偏好可能带来的不利影响，可以使用<b>增益率（gain ratio）</b>：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac8054a571f641509fe381">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 称为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的<b>固有值（intrinsic value）</b>。属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的可能取值数目越多，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的值通常会越大。需要注意的是，增益率准则对可取值数目较少的属性有所偏好，因此 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 算法使用了一个启发式：先从候选划分属性中找出信息增益高于平均水平的属性，再从中选择增益率最高的。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac80688c22fb8b8eaca63b" data-id="3ff0bc13b7ac80688c22fb8b8eaca63b"><span><div id="3ff0bc13b7ac80688c22fb8b8eaca63b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80688c22fb8b8eaca63b" title="3. 基尼指数"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 基尼指数</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac80f9a24de6423dc5199b"><b>基尼值</b>可以度量数据集纯度：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac807683a1ff9975998153"><span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 反映了从数据集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中随机抽取两个样本，其类别标记不一致的概率，因此基尼值越小则数据集纯度越高。<b>基尼指数（Gini index）</b> 定义：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80789720d1a6c584b6c1">因此在侯选属性集合 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中，选择使基尼指数最小的属性作为最优划分属性。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac806684eede98d557faaa" data-id="3ff0bc13b7ac806684eede98d557faaa"><span><div id="3ff0bc13b7ac806684eede98d557faaa" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac806684eede98d557faaa" title="三、剪枝处理"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">三、剪枝处理</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac807f9d30fdab08677f07"><b>剪枝（pruning）</b> 是决策树学习算法中解决过拟合的主要手段。</div><div class="notion-text notion-block-3ff0bc13b7ac8004b90cf57a4b9db80a">决策树剪枝的基本策略有<b>预剪枝（prepruning）</b>和<b>后剪枝（post-pruning）</b>。预剪枝指在决策树生成过程中，对每个结点在划分前进行估计，若当前结点的划分不能带来决策树泛化性能提升，则停止划分并将当前结点标记为叶结点；后剪枝则是先从训练集生成一棵完整的决策树，然后自底向上地对非叶结点进行考察，若将该结点对应的子树替换为叶结点能带来决策树泛化性能提升，则将该子树替换为叶结点。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac80789933e879ab401e6d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F367cea3694f3dd9d13372e92f930f91e.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8078-9933-e879ab401e6d&amp;t=3ff0bc13-b7ac-8078-9933-e879ab401e6d" alt="1" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac80edb605d1c9c9dbed7e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fcbd89f32138976d8746fca0cd11ddf9c.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-80ed-b605-d1c9c9dbed7e&amp;t=3ff0bc13-b7ac-80ed-b605-d1c9c9dbed7e" alt="2" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac80659b25c636e930829e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F505a401ed830dd83c5c7cc7672cef49a.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8065-9b25-c636e930829e&amp;t=3ff0bc13-b7ac-8065-9b25-c636e930829e" alt="3" loading="lazy" decoding="async"/></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac808ca9e0c23e694bb5d1" data-id="3ff0bc13b7ac808ca9e0c23e694bb5d1"><span><div id="3ff0bc13b7ac808ca9e0c23e694bb5d1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac808ca9e0c23e694bb5d1" title="四、连续与缺失值"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">四、连续与缺失值</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac80808c22c9e63721391b" data-id="3ff0bc13b7ac80808c22c9e63721391b"><span><div id="3ff0bc13b7ac80808c22c9e63721391b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80808c22c9e63721391b" title="1. 连续值处理"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 连续值处理</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac80f79e1bc8425be04a80">要想使用连续属性来生成决策树，就需要对连续值进行处理。最简单的策略是采用<b>二分法（bi-partition）</b>。</div><div class="notion-text notion-block-3ff0bc13b7ac804786eaefdee719882e">假定有一个连续属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，它具有多个不同的取值，将这些值从小到大排序，使得其在某个划分点 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可以对样本集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 进行划分，此时 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 就等于相邻两被划分元素取值的均值：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac801c89b7f83e4b5e1cc8">为选取最优的划分点，可对式 4.2 进行改造：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80ae932dfba112191552">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是样本集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 基于划分点 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 二分后的信息增益，因此可选择使其最大化的划分点。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3ff0bc13b7ac800da826e21e5dc4b5fd" data-id="3ff0bc13b7ac800da826e21e5dc4b5fd"><span><div id="3ff0bc13b7ac800da826e21e5dc4b5fd" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac800da826e21e5dc4b5fd" title="2. 缺失值处理"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 缺失值处理</span></span></h3><div class="notion-text notion-block-3ff0bc13b7ac8079b466fde43e75e015">如何在属性值缺失的情况下进行划分属性选择？</div><div class="notion-text notion-block-3ff0bc13b7ac8001a7f6c883c589cd12">给定训练集 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中在属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上没有缺失值的样本子集。假定属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个可取值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，令 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中在属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上取值为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的样本子集，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 中属于第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类的样本子集，则显然有 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。假定为每个样本 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 赋予一个权重 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，并定义：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80a6aa99e875878bb006">直观来看，堆属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示无缺失值样本所占的比例，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示无缺失值样本中第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 类所占的比例，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示无缺失值样本中在属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上取值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的样本所占的比例。显然 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> ，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。基于上述定义，可对式 4.2 进行改造：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80f19025c11d4135cd64">其中，由式 4.1 ，有：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3ff0bc13b7ac80b39f29d4204c1ed8c0">给定划分属性，若样本在该属性上的值缺失，如何对样本进行划分？</div><div class="notion-text notion-block-3ff0bc13b7ac80649f37fa418ed167e9">若样本 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 在划分属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上的取值已知，则将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 划入与其取值对应的子结点，且样本权值在子结点中保持为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。若样本 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 在划分属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 上的取值未知，则将 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 同时划入所有子结点，且样本权值在与属性值 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 对应的子结点中调整为 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 。直观的看，这就是让同一个样本以不同的概率划入到不同的子结点中去。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3ff0bc13b7ac80e4a02cdf8d7e5b3d12" data-id="3ff0bc13b7ac80e4a02cdf8d7e5b3d12"><span><div id="3ff0bc13b7ac80e4a02cdf8d7e5b3d12" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3ff0bc13b7ac80e4a02cdf8d7e5b3d12" title="五、多变量决策树"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">五、多变量决策树</span></span></h2><div class="notion-text notion-block-3ff0bc13b7ac807c8ca1cd0af265d675">若把每个属性视为坐标空间中的一个坐标轴，则 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 个属性描述的样本就对应了 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 维空间中的一个数据点，对样本分类则意味着在这个空间中寻找不同类样本之间的分类边界。决策树所形成分类边界的特点是<b>轴平行（axis-parallel）</b>，即它的分类边界由若干个与坐标轴平行的分段组成。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac808781e4c552225a96b4"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F94c9d0a850ea014b5fb6dc044252de68.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-8087-81e4-c552225a96b4&amp;t=3ff0bc13-b7ac-8087-81e4-c552225a96b4" alt="4" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-3ff0bc13b7ac80cc8346e6dac5b8f7c1">因此，这样的分类边界使得学习结果有较好的可解释性，因为每一段划分都直接对应了某个属性取值。但是当学习任务的真实分类边界比较复杂时，必须使用很多段划分才能获得较好的近似。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac804da552c6678d425f59"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Ff8386d2903b060522a22f174ca0587d5.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-804d-a552-c6678d425f59&amp;t=3ff0bc13-b7ac-804d-a552-c6678d425f59" alt="5" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-3ff0bc13b7ac80689d6fd05587a7e11f">此时决策树非常复杂，由于要进行大量的属性测试，因此预测时间开销很大。若能使用斜的划分边界，如上图红色线断所示，决策树模型将被简化。<b>多变量决策树（multivariate decision tree）</b>可以实现这个目标。在此类决策树中，非叶结点不再是仅对某个属性，而是对属性的线性组合进行测试，即每个叶结点是一个形如 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的线性分类器，其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 是属性 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 的权重，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 和 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 可在该结点所含的样本集和属性集上学得。于是，与传统的<b>单决策树（univariate decision tree）</b>不同，在多变量决策树的学习过程中，不是为每个非叶结点寻找一个最优划分属性，而是建立一个合适的线性分类器。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac80b99affc0569a45692e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fa243b404a6cfade5c29687681b5eb014.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-80b9-9aff-c0569a45692e&amp;t=3ff0bc13-b7ac-80b9-9aff-c0569a45692e" alt="6" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3ff0bc13b7ac802ea745e8e92ae9bf6f"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F0f0ebc04bc17f302bbe625059ff017c8.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c?table=block&amp;id=3ff0bc13-b7ac-802e-a745-e8e92ae9bf6f&amp;t=3ff0bc13-b7ac-802e-a745-e8e92ae9bf6f" alt="7" loading="lazy" decoding="async"/></div></figure></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[eddsa 算法]]></title>
            <link>https://blog.bezer0.cn/program-design/eddsa</link>
            <guid>https://blog.bezer0.cn/program-design/eddsa</guid>
            <pubDate>Thu, 15 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[eddsa 算法]]></description>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-3fa0bc13b7ac80569a02ffce53a02a26"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3fa0bc13b7ac8044b36aed9bf76c12ce" data-id="3fa0bc13b7ac8044b36aed9bf76c12ce"><span><div id="3fa0bc13b7ac8044b36aed9bf76c12ce" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3fa0bc13b7ac8044b36aed9bf76c12ce" title="信息安全课程设计：eddsa 算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">信息安全课程设计：<a class="notion-link" href="https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.3bc13d0d0UnilV&amp;ft=t&amp;id=704650673077" target="_blank" rel="noopener noreferrer">eddsa 算法</a></span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3fa0bc13b7ac806c93e1c93a28bea62e" data-id="3fa0bc13b7ac806c93e1c93a28bea62e"><span><div id="3fa0bc13b7ac806c93e1c93a28bea62e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3fa0bc13b7ac806c93e1c93a28bea62e" title="一、项目要求"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一、项目要求</span></span></h2><ol start="1" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8066b934fcdbe729a46e" style="list-style-type:decimal"><li>使用 C++ 语言开发；</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac802ba50efe5e722fec80" style="list-style-type:decimal"><li>可以实现公私钥生成、签名、认证；</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8077910fc38047431b88" style="list-style-type:decimal"><li>只需要手动输入明文，代码会自动生成公私钥、签名、认证；</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8064811ad752d5c9796d" style="list-style-type:decimal"><li>记录公私钥生成、签名、认证的时间；</li></ol><ol start="5" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac80f2957adb5a6022909d" style="list-style-type:decimal"><li>在 VS 上运行，使用 miracl 库。
# 二、算法原理参考</li></ol><ol start="6" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac80258ea1c6d58c9e6eb2" style="list-style-type:decimal"><li><a class="notion-link" href="https://zhuanlan.zhihu.com/p/97953640" target="_blank" rel="noopener noreferrer">一文读懂ECDSA算法如何保护数据</a></li></ol><ol start="7" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac80358521f9f3f346be97" style="list-style-type:decimal"><li><a class="notion-link" href="https://www.rfc-editor.org/rfc/rfc8032" target="_blank" rel="noopener noreferrer">Edwards-Curve Digital Signature Algorithm (EdDSA)</a></li></ol><ol start="8" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac80bfbcaac88d4640a14c" style="list-style-type:decimal"><li><a class="notion-link" href="https://duo.com/labs/tech-notes/whats-an-eddsa" target="_blank" rel="noopener noreferrer">WHAT’S AN EDDSA?</a></li></ol><ol start="9" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8005add2eaaa48c7fba2" style="list-style-type:decimal"><li><a class="notion-link" href="https://www.cnblogs.com/librarookie/p/15389876.html" target="_blank" rel="noopener noreferrer">ECDSA、EdDSA 和 Ed25519 的区别</a></li></ol><ol start="10" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8084a707d019aa343641" style="list-style-type:decimal"><li><a class="notion-link" href="https://cryptobook.nakov.com/digital-signatures/eddsa-and-ed25519" target="_blank" rel="noopener noreferrer">EdDSA and Ed25519</a>
# 三、效果展示</li><ol class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8084a707d019aa343641" style="list-style-type:lower-alpha"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac809b8215f84b003bab8a"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F27ebf47eb16e247a4a73ebda455627ad.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-809b-8215-f84b003bab8a&amp;t=3fa0bc13-b7ac-809b-8215-f84b003bab8a" alt="15" loading="lazy" decoding="async"/></div></figure></ol></ol><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac809385edf9e64193cf85"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F27ebf47eb16e247a4a73ebda455627ad.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-8093-85ed-f9e64193cf85&amp;t=3fa0bc13-b7ac-8093-85ed-f9e64193cf85" alt="15" loading="lazy" decoding="async"/></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3fa0bc13b7ac80cfbce0ef9f9aa134f3" data-id="3fa0bc13b7ac80cfbce0ef9f9aa134f3"><span><div id="3fa0bc13b7ac80cfbce0ef9f9aa134f3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3fa0bc13b7ac80cfbce0ef9f9aa134f3" title="四、使用方法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">四、使用方法</span></span></h2><div class="notion-text notion-block-3fa0bc13b7ac803581f3c84f5536738b">注：该算法使用了 MIRACL 的核心库（MIRACL-core）</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3fa0bc13b7ac8030b3e6cb026f82be0a" data-id="3fa0bc13b7ac8030b3e6cb026f82be0a"><span><div id="3fa0bc13b7ac8030b3e6cb026f82be0a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3fa0bc13b7ac8030b3e6cb026f82be0a" title="1. 简单使用版"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 简单使用版</span></span></h3><ol start="1" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac8001886dd19f8e782bf7" style="list-style-type:decimal"><li>解压文件夹</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-3fa0bc13b7ac80ee8608cec2820fb8d7" style="list-style-type:decimal"><li>运行 testecc.exe 即可</li></ol><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-3fa0bc13b7ac806490ebe838b9e85da1" data-id="3fa0bc13b7ac806490ebe838b9e85da1"><span><div id="3fa0bc13b7ac806490ebe838b9e85da1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3fa0bc13b7ac806490ebe838b9e85da1" title="2. vs 配置方法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. vs 配置方法</span></span></h3><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80adbf78e68bfbbb2074"><li>以 VS 2019 为例，首先创建一个新项目，放在你能找到的位置：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80adbf78e68bfbbb2074"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac80358e59d494d449f61d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F5441ea0fd00cb3a40ad48b1506301571.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-8035-8e59-d494d449f61d&amp;t=3fa0bc13-b7ac-8035-8e59-d494d449f61d" alt="1" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac80cbbc18f7439973f630"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Ff313d09c2958d595e467dfb0ed1f37ae.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-80cb-bc18-f7439973f630&amp;t=3fa0bc13-b7ac-80cb-bc18-f7439973f630" alt="2" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac80bd8febda6444dfa1fb"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F6bb628bc1b538e1247c4f116a5548fb2.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-80bd-8feb-da6444dfa1fb&amp;t=3fa0bc13-b7ac-80bd-8feb-da6444dfa1fb" alt="3" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80179a80ef688a790f0a"><li>把解压的文件全都复制进去：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80179a80ef688a790f0a"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac800fbc68c4e67cfb1c69"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fd5f25b2d7182be7656973edddcee55d1.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-800f-bc68-c4e67cfb1c69&amp;t=3fa0bc13-b7ac-800f-bc68-c4e67cfb1c69" alt="4" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80b0bf67c71a2fdd1329"><li>添加源文件：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80b0bf67c71a2fdd1329"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac807492cce637d0872471"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F9a5c4f404ee5f6bb15ea19999305cdbb.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-8074-92cc-e637d0872471&amp;t=3fa0bc13-b7ac-8074-92cc-e637d0872471" alt="5" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac80898dc4e513d2eba7d0"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F13c63b15f0cbbdf9cab44bbc857685fc.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-8089-8dc4-e513d2eba7d0&amp;t=3fa0bc13-b7ac-8089-8dc4-e513d2eba7d0" alt="6" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac801789c3efca655746e5"><li>添加头文件：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac801789c3efca655746e5"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac802ba234eb8c289b1c0b"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F9b54812e56d261b3ce93de1e20087658.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-802b-a234-eb8c289b1c0b&amp;t=3fa0bc13-b7ac-802b-a234-eb8c289b1c0b" alt="7" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac802d92f8efcbf321ffda"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fbe66cef9f9ce1a8668e7368c0c80d7d3.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-802d-92f8-efcbf321ffda&amp;t=3fa0bc13-b7ac-802d-92f8-efcbf321ffda" alt="8" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80a2b361e90341f7826c"><li>由于 MIRACL 核心库 core.a 不能直接使用，因此需要配置外部工具：

其中内容为：配置成功后会在工具栏出现 g++ 工具：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80a2b361e90341f7826c"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac80b08e8bf03613fe6be6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fb5fe97c40607ee9ebb9f688d49fba28d.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-80b0-8e8b-f03613fe6be6&amp;t=3fa0bc13-b7ac-80b0-8e8b-f03613fe6be6" alt="9" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac80cfa8b3d2f92e55eaa3"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2Fc6f7d63af015bf2564e383c609027196.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-80cf-a8b3-d2f92e55eaa3&amp;t=3fa0bc13-b7ac-80cf-a8b3-d2f92e55eaa3" alt="10" loading="lazy" decoding="async"/></div></figure><li><code class="notion-inline-code">g++</code></li><li><code class="notion-inline-code">D:\Software\mingw64\bin\g++.exe</code> （注：找到你自己电脑下的该文件目录才行，如果找不到自行百度安装 mingw64）</li><li><code class="notion-inline-code">o2 $(ItemFileName).cpp core.a -o $(ItemFileName)</code></li><li><code class="notion-inline-code">$(SolutionDir)</code></li><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac808b8a7aee8ea4e7b6c1"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F17b1d97e4aec80c906810f9c6b11d106.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-808b-8a7a-ee8ea4e7b6c1&amp;t=3fa0bc13-b7ac-808b-8a7a-ee8ea4e7b6c1" alt="11" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac800b8eeaf8fb155f0719"><li>打开 testecc.cpp 文件，运行 g++：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac800b8eeaf8fb155f0719"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac8033b4dcfd56a7cab8b6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F75839e627369c7e761ec5a0407dd1aba.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-8033-b4dc-fd56a7cab8b6&amp;t=3fa0bc13-b7ac-8033-b4dc-fd56a7cab8b6" alt="12" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac8002af1bf60ee7b14c62"><li>如果输出为空，且 testecc.cpp 文件的时间发生变化则表示编译成功：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac8002af1bf60ee7b14c62"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac801c917ae3333e6cac1f"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F7c4ab48a548ae27081150ff49d5235ea.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-801c-917a-e3333e6cac1f&amp;t=3fa0bc13-b7ac-801c-917a-e3333e6cac1f" alt="13" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80b79201ef4c686ab355"><li>由于VS不能直接打开文件，因此需在文件夹内打开 testecc.exe 文件查看：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80b79201ef4c686ab355"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-3fa0bc13b7ac8034aeadf99dafcc42d9"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/https%3A%2F%2Fi-blog.csdnimg.cn%2Fblog_migrate%2F9003982cbe8b9d88a8622bbb7da7e671.png%3FspaceId%3Dffe0bc13-b7ac-8162-8b1d-00038fd89e6c%23pic_center?table=block&amp;id=3fa0bc13-b7ac-8034-aead-f99dafcc42d9&amp;t=3fa0bc13-b7ac-8034-aead-f99dafcc42d9" alt="14" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80cdb9cadcb25f059492"><li>如果需要修改源文件，请先保存文件后再用 g++ 工具编译，即每次修改代码后都需要：</li><ul class="notion-list notion-list-disc notion-block-3fa0bc13b7ac80cdb9cadcb25f059492"><li>保存文件</li><li>g++ 工具编译</li><li>运行文件夹中的 testecc.exe</li></ul></ul></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[天牛须算法]]></title>
            <link>https://blog.bezer0.cn/program-design/3f70bc13-b7ac-809b-8041-f337dbdcf871</link>
            <guid>https://blog.bezer0.cn/program-design/3f70bc13-b7ac-809b-8041-f337dbdcf871</guid>
            <pubDate>Mon, 12 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[天牛须算法]]></description>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-3f70bc13b7ac809b8041f337dbdcf871"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3f70bc13b7ac8099981fd95b46d59e47" data-id="3f70bc13b7ac8099981fd95b46d59e47"><span><div id="3f70bc13b7ac8099981fd95b46d59e47" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3f70bc13b7ac8099981fd95b46d59e47" title="一、介绍"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一、介绍</span></span></h2><div class="notion-text notion-block-3f70bc13b7ac80498131e42ee37c5535">天牛须算法（Beetle Antennae search algorithm, BAS）是由 Jiang 等人于2017年提出的一种智能优化算法，该算法模拟了天牛寻食物时的搜索方式，是一种单体搜索算法，该算法原理简单、参数少、计算量少，在处理低维优化目标时具有优势。</div><div class="notion-text notion-block-3f70bc13b7ac80e0aa41fd123126aa96">天牛在觅食过程中，会被食物的气味吸引。天牛通过其两只触角对空气中的食物气味进行感知，由于食物距离两只触角的距离远近不同，因此触角所感知的气味浓度也有所差异。当食物处于天牛左侧时，左侧触角感知的气味浓度强于右侧触角感知的气味浓度，因此天牛可以根据两只触角所感知的浓度差，向着浓度强的一侧随机前进。多次迭代后最终找到食物的位置。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3f70bc13b7ac80f5808cf3ca5365f269" data-id="3f70bc13b7ac80f5808cf3ca5365f269"><span><div id="3f70bc13b7ac80f5808cf3ca5365f269" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3f70bc13b7ac80f5808cf3ca5365f269" title="二、原理"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二、原理</span></span></h2><div class="notion-text notion-block-3f70bc13b7ac805e88aaf2dca8ea8982">介绍算法前可以先设定一些概念：</div><div class="notion-text notion-block-3f70bc13b7ac80f2ac12f27c0a3a831b">在 n 维空间中天牛的位置为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3f70bc13b7ac809ea349e725ac0d1491">天牛左右两只触角的位置：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3f70bc13b7ac80b6a645d5bd0f485fbd">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示天牛质心与触须的距离，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示随机单位向量，需要对单位向量进行归一化操作：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3f70bc13b7ac8057b263fa682f7c4eac">由于两只触角获得两个位置的目标函数信息，因此需要对两个位置择优前进：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3f70bc13b7ac800698fbdcaf2970094a">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示迭代次数，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 函数为目标函数，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示第 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 次探索时的步长，<span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 为符号函数。通常在每次迭代过程中步长需要不断变小，因此可以将其设置为：</div><span role="button" tabindex="0" class="notion-equation notion-equation-block"><span></span></span><div class="notion-text notion-block-3f70bc13b7ac80b0b755fa39a1370cdf">其中 <span role="button" tabindex="0" class="notion-equation notion-equation-inline"><span></span></span> 表示每次迭代步长缩短的速度，通常设定为 0.95 。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3f70bc13b7ac8036904ff9d81827075d" data-id="3f70bc13b7ac8036904ff9d81827075d"><span><div id="3f70bc13b7ac8036904ff9d81827075d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3f70bc13b7ac8036904ff9d81827075d" title="三、参考代码"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">三、参考代码</span></span></h2><div class="notion-text notion-block-3f70bc13b7ac8036ac0ff78a8a13692b">此代码为最大值模板，若要改为最小值，需要改变下面的代码：</div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[代理配置]]></title>
            <link>https://blog.bezer0.cn/program-design/3f70bc13-b7ac-8092-8257-f86d7a8daa37</link>
            <guid>https://blog.bezer0.cn/program-design/3f70bc13-b7ac-8092-8257-f86d7a8daa37</guid>
            <pubDate>Mon, 12 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[代理配置]]></description>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-3f70bc13b7ac80928257f86d7a8daa37"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3f70bc13b7ac80cc8d47fef8c3f33dbd" data-id="3f70bc13b7ac80cc8d47fef8c3f33dbd"><span><div id="3f70bc13b7ac80cc8d47fef8c3f33dbd" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3f70bc13b7ac80cc8d47fef8c3f33dbd" title="Git 代理配置"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">Git 代理配置</span></span></h2><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-3f70bc13b7ac80e19d4cdcc6bf7c6a67" data-id="3f70bc13b7ac80e19d4cdcc6bf7c6a67"><span><div id="3f70bc13b7ac80e19d4cdcc6bf7c6a67" class="notion-header-anchor"></div><a class="notion-hash-link" href="#3f70bc13b7ac80e19d4cdcc6bf7c6a67" title="conda 代理配置"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">conda 代理配置</span></span></h2></main></div>]]></content:encoded>
        </item>
    </channel>
</rss>