<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Finetuning on Ray's Blog</title><link>https://raychi.site/tags/finetuning/</link><description>Recent content in Finetuning on Ray's Blog</description><generator>Hugo</generator><language>zh-CN</language><copyright>Copyright © 2025, Ray</copyright><lastBuildDate>Mon, 11 Nov 2024 00:00:00 +0800</lastBuildDate><atom:link href="https://raychi.site/tags/finetuning/index.xml" rel="self" type="application/rss+xml"/><item><title>LoRA微调</title><link>https://raychi.site/low-rank-adaptation/</link><pubDate>Mon, 11 Nov 2024 00:00:00 +0800</pubDate><guid>https://raychi.site/low-rank-adaptation/</guid><description>&lt;p>现在的大模型动辄几十、上百亿参数，如果我们想要对模型进行微调，放到本地一般而言很少有足够的显存。&lt;/p>
&lt;p>大模型微调的几种方法：&lt;/p>
&lt;ul>
&lt;li>低秩微调LoRA&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>LoRA（Low-Rank Adaptation）简介：高效的深度学习模型微调方法&lt;/strong>&lt;/p>
&lt;p>在深度学习领域，尤其是大规模预训练模型（如BERT、GPT系列等）的微调过程中，传统的微调方法通常需要修改模型的所有参数。这样做虽然可以获得较好的性能，但代价非常高，特别是在面对非常大的预训练模型时。LoRA（Low-Rank Adaptation）作为一种新的微调技术，提出了一种更加高效且参数量更少的方法来进行微调。&lt;/p>
&lt;p>&lt;strong>什么是LoRA？&lt;/strong>&lt;/p>
&lt;p>LoRA是“低秩适应”技术的缩写，其核心思想是在进行模型微调时，通过将权重矩阵分解为低秩矩阵来减少可调参数的数量。这使得我们在不改变原始模型结构的情况下，能够以更少的计算和存储开销，快速适应下游任务。&lt;/p>
&lt;p>具体来说，LoRA通过在神经网络中的每一层加入一个低秩适应矩阵，这些矩阵只会在微调过程中进行更新，而原始的预训练参数保持不变。&lt;/p>
&lt;p>&lt;strong>LoRA的原理&lt;/strong>&lt;/p>
&lt;p>在传统的神经网络中，参数矩阵通常是一个高维矩阵。例如，在一个线性层中，假设权重矩阵是 ，其中 和 分别是输入和输出的维度。&lt;/p>
&lt;p>LoRA的关键想法是将这个高维矩阵 分解为两个低秩矩阵 和 ，即：&lt;/p>
&lt;p>其中， 是我们在微调过程中需要学习的低秩适应矩阵。通过这种方式，只有 和 两个矩阵需要更新，且它们的秩通常设置为较小的值（例如，秩为 ）。这样可以显著减少需要训练的参数数量，同时保留原始模型的表示能力。&lt;/p>
&lt;p>&lt;strong>为什么LoRA有效？&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>减少计算开销&lt;/strong>：由于LoRA只需要更新较小的低秩矩阵，避免了对整个模型参数的更新，因此计算资源消耗大大降低，尤其是在大规模预训练模型上。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>适用于大模型&lt;/strong>：大规模预训练模型通常包含数亿甚至数十亿个参数，直接微调这些模型往往需要巨大的计算和存储资源。LoRA通过限制可调参数的数量，使得在有限资源下仍然能够有效地进行微调。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>保持预训练知识&lt;/strong>：LoRA不会修改原始的预训练权重，只是引入了少量的适应性变化。因此，模型能够保留从大规模数据集学到的知识，同时针对具体任务进行微调。&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>LoRA的实现&lt;/strong>&lt;/p>
&lt;p>假设我们有一个现成的神经网络模型（例如BERT或GPT），而我们想要进行LoRA微调。LoRA的实现步骤如下：&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>选择适合微调的层&lt;/strong>：首先，我们需要选择模型中哪些层需要进行LoRA微调。通常会选择自注意力层（Attention）和前馈神经网络（FeedForward）层。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>插入低秩适应矩阵&lt;/strong>：在选定的层中，我们将原始权重矩阵 替换为 ，其中 和 是低秩矩阵。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>训练低秩矩阵&lt;/strong>：在微调过程中，我们只更新低秩矩阵 和 ，而保持原始模型权重不变。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>评估性能&lt;/strong>：在完成微调后，我们评估模型在下游任务上的性能，看看LoRA是否能够有效地提高模型在特定任务上的表现。&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>LoRA的优点&lt;/strong>&lt;/p>
&lt;p>• &lt;strong>参数效率高&lt;/strong>：LoRA只需要更新少量参数，通常比传统微调方法的参数更新量要小得多，尤其是在大规模预训练模型中。&lt;/p>
&lt;p>• &lt;strong>节省计算资源&lt;/strong>：LoRA的低秩适应矩阵可以显著减少训练和推理时的计算开销。&lt;/p>
&lt;p>• &lt;strong>简洁易用&lt;/strong>：LoRA的实现相对简单，可以作为一个插件式的微调方法，适用于多种不同的神经网络架构。&lt;/p>
&lt;p>&lt;strong>LoRA的应用场景&lt;/strong>&lt;/p>
&lt;p>LoRA非常适合用于以下几种场景：&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>大规模预训练模型微调&lt;/strong>：在处理诸如BERT、GPT等大型模型时，LoRA能够有效减少微调时的计算资源消耗。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>低资源环境下的迁移学习&lt;/strong>：如果资源有限（例如显存不足），LoRA能够帮助在不牺牲性能的前提下进行迁移学习。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>模型压缩&lt;/strong>：LoRA也可以用于模型压缩，尤其是在需要在嵌入式设备或移动设备上运行大模型时，LoRA可以通过减少训练参数的数量来降低模型的存储需求。&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>示例代码&lt;/strong>&lt;/p>
&lt;p>以下是一个简单的Python示例，展示了如何在PyTorch中实现LoRA微调：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 1&lt;/span>&lt;span>&lt;span style="color:#ff7b72">import&lt;/span> &lt;span style="color:#ff7b72">torch&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 2&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 3&lt;/span>&lt;span>&lt;span style="color:#ff7b72">import&lt;/span> &lt;span style="color:#ff7b72">torch.nn&lt;/span> &lt;span style="color:#ff7b72">as&lt;/span> &lt;span style="color:#ff7b72">nn&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 4&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 5&lt;/span>&lt;span>&lt;span style="color:#ff7b72">import&lt;/span> &lt;span style="color:#ff7b72">torch.optim&lt;/span> &lt;span style="color:#ff7b72">as&lt;/span> &lt;span style="color:#ff7b72">optim&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 6&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 7&lt;/span>&lt;span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 8&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681"> 9&lt;/span>&lt;span>&lt;span style="color:#ff7b72">class&lt;/span> &lt;span style="color:#f0883e;font-weight:bold">LoRALayer&lt;/span>(nn&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>Module):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">10&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">11&lt;/span>&lt;span>    &lt;span style="color:#ff7b72">def&lt;/span> &lt;span style="color:#d2a8ff;font-weight:bold">__init__&lt;/span>(self, input_dim, output_dim, rank):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">12&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">13&lt;/span>&lt;span>        super(LoRALayer, self)&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>&lt;span style="color:#d2a8ff;font-weight:bold">__init__&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">14&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">15&lt;/span>&lt;span>        self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>input_dim &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> input_dim
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">16&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">17&lt;/span>&lt;span>        self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>output_dim &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> output_dim
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">18&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">19&lt;/span>&lt;span>        self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>rank &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> rank
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">20&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">21&lt;/span>&lt;span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">22&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">23&lt;/span>&lt;span>        &lt;span style="color:#8b949e;font-style:italic"># 初始化低秩矩阵 A 和 B&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">24&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">25&lt;/span>&lt;span>        self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>A &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> nn&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>Parameter(torch&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>randn(input_dim, rank))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">26&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">27&lt;/span>&lt;span>        self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>B &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> nn&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>Parameter(torch&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>randn(rank, output_dim))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">28&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">29&lt;/span>&lt;span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">30&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">31&lt;/span>&lt;span>    &lt;span style="color:#ff7b72">def&lt;/span> &lt;span style="color:#d2a8ff;font-weight:bold">forward&lt;/span>(self, x):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">32&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">33&lt;/span>&lt;span>        _&lt;span style="color:#8b949e;font-style:italic"># 计算低秩适应矩阵的贡献_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">34&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">35&lt;/span>&lt;span>        delta_W &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> torch&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>mm(self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>A, self&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>B)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">36&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">37&lt;/span>&lt;span>        &lt;span style="color:#ff7b72">return&lt;/span> torch&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>matmul(x, delta_W)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">38&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">39&lt;/span>&lt;span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">40&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">41&lt;/span>&lt;span>_&lt;span style="color:#8b949e;font-style:italic"># 模型层的使用示例_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">42&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">43&lt;/span>&lt;span>input_dim &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> &lt;span style="color:#a5d6ff">768&lt;/span>  _&lt;span style="color:#8b949e;font-style:italic"># 输入维度_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">44&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">45&lt;/span>&lt;span>output_dim &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> &lt;span style="color:#a5d6ff">256&lt;/span>  _&lt;span style="color:#8b949e;font-style:italic"># 输出维度_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">46&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">47&lt;/span>&lt;span>rank &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> &lt;span style="color:#a5d6ff">8&lt;/span>  _&lt;span style="color:#8b949e;font-style:italic"># 低秩矩阵的秩_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">48&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">49&lt;/span>&lt;span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">50&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">51&lt;/span>&lt;span>_&lt;span style="color:#8b949e;font-style:italic"># 创建LoRA层_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">52&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">53&lt;/span>&lt;span>lora_layer &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> LoRALayer(input_dim, output_dim, rank)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">54&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">55&lt;/span>&lt;span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">56&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">57&lt;/span>&lt;span>&lt;span style="color:#8b949e;font-style:italic"># 假设输入是一个batch的样本_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">58&lt;/span>&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">59&lt;/span>&lt;span>x &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> torch&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>randn(&lt;span style="color:#a5d6ff">32&lt;/span>, input_dim)  _&lt;span style="color:#8b949e;font-style:italic"># 假设batch size为32_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">60&lt;/span>&lt;span>&lt;span style="color:#8b949e;font-style:italic"># 前向传播_&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#6e7681">61&lt;/span>&lt;span>output &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> lora_layer(x)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>总结&lt;/strong>&lt;/p></description></item></channel></rss>