青草蝴蝶 發表於 2026-4-13 16:53:00

深度学习进阶(七)Data-efficient Image Transformer

<p>在前面的分析中,我们已经明确了 ViT 的核心问题:</p>
<blockquote>
<p><strong>由于归纳偏置较弱,ViT 对数据规模高度依赖。</strong></p>
</blockquote>
<p>就这个问题,我们又展开了一种改进思路:</p>
<blockquote>
<p><strong>通过蒸馏人为引入一个“软约束”,缩小搜索空间,从而减少数据依赖。</strong></p>
</blockquote>
<p>于是,我们就得到了 ViT 的其中一种改进:<strong>Data-efficient Image Transformer,即 DeiT</strong>。</p>
<h1 id="1-deit-的两种蒸馏方式">1. DeiT 的两种蒸馏方式</h1>
<p>DeiT 出自 ViT 的同年论文: <strong><em>Training data-efficient image transformers &amp; distillation through attention</em></strong>.<br>
它并不是简单地套用标准蒸馏,而是针对 Transformer 的结构,设计了两种蒸馏策略:</p>
<h2 id="11-硬蒸馏-hard-distillation">1.1 硬蒸馏 Hard Distillation</h2>
<p>这种方式非常直接: <strong>把 Teacher 的预测类别,当作额外的监督信号。</strong><br>
也就是说,Student 同时学习两个标签:<strong>数据的真实标签 <span class="math inline">\(y\)</span> 和 Teacher 给出的预测标签 <span class="math inline">\(\hat{y}_t\)</span>。</strong><br>
其损失函数可以写为:</p>
<p></p><div class="math display">\[\mathcal{L} = \frac{1}{2} \mathcal{L}_{CE}(y, p_s) + \frac{1}{2} \mathcal{L}_{CE}(\hat{y}_t, p_s)
\]</div><p></p><p><strong>要提前强调的是,这里的公式只是通用形式,实际上在 DeiT 中使用的不是同一个 <span class="math inline">\(p_s\)</span>,下一部分就会展开。</strong></p>
<p>回到这里,其实对比我们在上一篇展开的蒸馏过程,这种方法看起来有些没必要:</p>
<blockquote>
<p><strong>只是多加一个标签,预测正确没有更多信息,预测错误反而影响拟合,为什么要这么做?</strong></p>
</blockquote>
<p>要理解来说,其实还是和 Teacher 本身的蒸馏意义相关:而在于通过 Teacher 的预测结果,引导 Student 在优化过程中靠近一个“更合理的决策边界”,从而提升训练稳定性。</p>
<p>比如真实标签是“狼”,但 Teacher 预测结果是“狐狸”,这样就可以让模型在“语义接近类别”之间间接引导模型学习更合理的决策边界。</p>
<p><strong>不过,对比现代的蒸馏方法,这种方法已经很少单独使用了,我们知道就好。</strong></p>
<h2 id="12-软蒸馏-soft-distillation">1.2 软蒸馏 Soft Distillation</h2>
<p>第二种方式就是我们前面介绍的标准蒸馏:<strong>让 Student 去拟合 Teacher 的概率分布。</strong><br>
其损失函数为:</p>
<p></p><div class="math display">\[\mathcal{L} = \alpha \mathcal{L}_{CE} + (1-\alpha) D_{KL}(p_t \parallel p_s)
\]</div><p></p><p>显然,相比硬蒸馏,这种方式包含更多信息,在现代使用中也更主流。</p>
<p>不过,在 Transformer 之前,蒸馏技术就已经出现,如果只是在 ViT 中加入蒸馏技术,本质上只是 A+B 的工作罢了。<br>
DeiT 的关键创新点在于:</p>
<blockquote>
<p><strong>把“蒸馏过程”也做成了 Transformer 结构的一部分。</strong></p>
</blockquote>
<p>我们下面来详细展开这部分逻辑:</p>
<h1 id="2-deit-的核心改进">2. DeiT 的核心改进</h1>
<h2 id="21-输入处理">2.1 输入处理</h2>
<p>DeiT 实现蒸馏逻辑的具体做法是:</p>
<blockquote>
<p><strong>在输入序列中,再额外引入一个 distillation token。</strong></p>
</blockquote>
<p>我们来理顺一遍,首先,是原始图像切分出的 patch token :</p>
<p></p><div class="math display">\[[\mathbf{z}^1, \dots, \mathbf{z}^N]
\]</div><p></p><p>在此基础上,我们为了整合全局信息用于 CV 任务,又加入了 token :</p>
<p></p><div class="math display">\[[\mathbf{x}_{cls}, \mathbf{z}^1, \dots, \mathbf{z}^N]
\]</div><p></p><p>而现在,在 DeiT 的设计中,我们再加入一个 Distillation token,即蒸馏 token :</p>
<p></p><div class="math display">\[[\mathbf{x}_{cls}, \mathbf{x}_{dist}, \mathbf{z}^1, \dots, \mathbf{z}^N]
\]</div><p></p><p>这个 <span class="math inline">\(\mathbf{x}_{dist}\)</span> <strong>就专门用于学习 Teacher 的信息</strong>。</p>
<h2 id="22-传播逻辑">2.2 传播逻辑</h2>
<p>知道又加了一个 distillation token 后,和 token 一样的问题是:</p>
<blockquote>
<p><strong>我们如何实现 distillation token 专门学习 Teacher 信息的语义?</strong></p>
</blockquote>
<p>答案的关键词是:<strong>双输出结构</strong>。<br>
我们从原本的 ViT 来展开改进过程:</p>
<p><img src="https://img2024.cnblogs.com/blog/3708248/202604/3708248-20260413165014781-2022308252.png" alt="image.png" loading="lazy"><br>
<img src="https://img2024.cnblogs.com/blog/3708248/202604/3708248-20260413165011664-180624755.png" alt="image.png" loading="lazy"></p>
<p>到这里,你就会发现:<strong>DeiT 并没有把学习真实标签和 Teacher 给出的预测标签混在一起,而是解耦二者, token 只学习真正标签,distillation token 只学习蒸馏标签。</strong></p>
<p>再展开一下:<br>
在训练阶段,DeiT 会产生两个输出:</p>
<ol>
<li><strong>CLS 分支(主任务)</strong></li>
</ol>
<p></p><div class="math display">\[p_{cls} = \text{MLP}(\mathbf{x}_{cls})
\]</div><p></p><ol start="2">
<li><strong>Distillation 分支(蒸馏任务)</strong></li>
</ol>
<p></p><div class="math display">\[p_{dist} = \text{MLP}(\mathbf{x}_{dist})
\]</div><p></p><p>最终,损失可以写为:</p>
<p></p><div class="math display">\[\mathcal{L} = \mathcal{L}_{CE}(y, p_{cls}) + \mathcal{L}_{KD}(p_t, p_{dist})
\]</div><p></p><p>当然,你也可以加入权重的设计。<br>
这样,与传统蒸馏仅在模型输出端施加监督不同,distillation token 会作为 Transformer 的一部分参与 self-attention 计算,<strong>使得 teacher 信息能够在特征形成阶段被逐层融合,从而影响模型的内部表示学习过程。</strong><br>
<img src="https://img2024.cnblogs.com/blog/3708248/202604/3708248-20260413165201518-766413175.png" alt="image.png" loading="lazy"></p>
<p>值得一提的是,DeiT 原论文的主要实验结论是基于 hard distillation 的,soft 作为对照方法存在但未成为最优结果,其损失形式如下:</p>
<p></p><div class="math display">\[\mathcal{L} = \frac{1}{2} \mathcal{L}_{CE}(y, p_{cls}) + \frac{1}{2} \mathcal{L}_{CE}(y_t, p_{dist})
\]</div><p></p><p>实验结果表明,在 CNN 到 ViT 的跨结构蒸馏场景中,soft distillation 并未展现出优势。<br>
相反,<strong>基于 hard label 的 distillation 更稳定,也更符合 ViT 的优化特性,因此成为最终采用的主方案。</strong></p>
<p>显然,这带来的一个额外问题是:</p>
<blockquote>
<p><strong>那为什么 soft distillation 反而更主流?</strong></p>
</blockquote>
<p>答案并不是它在所有任务中更强,而是它提供更多信息,从而在<strong>跨任务、跨模型、跨领域的知识迁移中具有更高的通用性。</strong></p>
<p>可以简单概括来说:<strong>hard distillation 学的是“答案”,soft distillation 学的是“思考方式”。</strong><br>
因此,在 DeiT 这一分类应用中前者的作用更突出,后者在这一模型中虽没有前者稳定,但却是更广泛领域中的选择。</p>
<p>实际上,蒸馏技术不断发展,早已不再满足这种仅仅对输出的蒸馏了,现在已经有了对中间特征的蒸馏,甚至自己蒸馏自己等更多、更高效的形式,我们遇到再详细展开。</p>
<p>到这里,我们就了解到 DeiT 通过蒸馏,引入外部归纳偏置,从而缓解数据依赖问题。</p>
<p>不过,其实还有另外一种改进思路:<strong>不依靠外力,通过结构本身重建归纳偏置。</strong><br>
而这种自身结构的优化,才是 Vision Transformer 走向主流应用的关键转折点。<br>
我们在下一篇就来详细展开。</p><br><br>
来源:https://www.cnblogs.com/Goblinscholar/p/19860718
頁: [1]
查看完整版本: 深度学习进阶(七)Data-efficient Image Transformer