<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>LLM on Ray's Blog</title><link>https://raychi.site/tags/llm/</link><description>Recent content in LLM on Ray's Blog</description><generator>Hugo</generator><language>zh-CN</language><copyright>Copyright © 2025, Ray</copyright><lastBuildDate>Tue, 29 Sep 2026 00:00:00 +0800</lastBuildDate><atom:link href="https://raychi.site/tags/llm/index.xml" rel="self" type="application/rss+xml"/><item><title>LLM的Ai味何来？</title><link>https://raychi.site/llm-defensive-narrative/</link><pubDate>Tue, 29 Sep 2026 00:00:00 +0800</pubDate><guid>https://raychi.site/llm-defensive-narrative/</guid><description>&lt;h2 id="避免-llm-的-ai-味">避免 LLM 的 AI 味&lt;/h2>
&lt;p>我觉得很多事情都是表面上看起来，他这样可以做，那如果这样做，那他肯定可以走通的。正所谓不知者无畏。&lt;/p>
&lt;p>然后觉得这个事其实是一个很简单的事情。但这只是宏观上粗浅地看到这个问题，它真正真的简单吗？这要打一个大大的问号。&lt;/p>
&lt;p>举一个例子，接 LLMAPI，然后自己搭建一个问答助手。首先这个模型它学到了很多知识，你问它什么，它也会答给你，所以作为问答助手是没问题的。&lt;/p>
&lt;p>但是实际使用过程中，并不是按照预期情况来的。最常见的就是所谓豆包式的回答方法，我将不拐弯抹角、一针见血、直截了当地回复你。&lt;/p>
&lt;p>也不是说它说得不对，只是这样说话极其不利于信息的直接传递。&lt;/p>
&lt;p>在我们日常交流中，我们通常是这样子的：当我们询问一个问题时，往往希望对方直接给出答案，而不是在开场时先来一堆“找补”和“铺垫”。&lt;/p>
&lt;p>但 LLM 却截然相反——它极度喜欢把话说得“万无一失”。这种情况往往出现在某些模型上，代表性就是GPT 5 系列。&lt;/p>
&lt;p>在我看来，他也就是说在防御性的叙事，也就是说他说这么多，你挑不出来什么刺，但是就是让你感觉很不舒服，就是一股 AI 味。&lt;/p>
&lt;p>那到底为什么导致他这样子呢？&lt;/p>
&lt;hr>
&lt;p>举个例子。&lt;/p>
&lt;p>假设你向模型定义或询问一个稍显新奇的概念——比如你提出一个更高级的概念 A，它是由大家熟知的旧概念 B 引申而来的。也就是说，在B的概念上包装出一个A。&lt;/p>
&lt;p>如果是人类正常交流的话，通常会直接借助旧概念建立认知：“A 其实就是 B 的升级版，多了某某功能。”&lt;/p>
&lt;p>因为我们默认听众具备基本的背景知识。&lt;/p>
&lt;p>但 LLM 通常会怎么说？它会机械地输出一段极其严谨却毫无重点的文字：&lt;/p>
&lt;blockquote>
&lt;p>“A 是一种具有 X 特征、具备 Y 功能的独立系统。虽然它与 B 在某些方面存在联系，但二者在定义、适用范围和运行机制上并不相同……”&lt;/p>&lt;/blockquote>
&lt;p>这种表达方式看似极其准确描述了 A，并且“防御性”地澄清了“此 A 非 B”，完美避免了误会。但它的根本问题在于：
&lt;strong>模型的表达太过于“面面俱到”了。它默认假定电脑前的人是个一无所知的门外汉，为了防止被挑出毛病，它选择了最保守的表述。&lt;/strong>&lt;/p>
&lt;p>但是模型也是通过人类的语料来训练的，而人类对话通常就不这样说，所以它很有可能是在后训练的过程中学到这种特别的癖好。&lt;/p>
&lt;hr>
&lt;p>从 LLM 为什么能这样对话讲起，首先模型要经过预训练，学习大量的语料，掌握知识，然后要经过后训练，也就是让它说人话。后训练出来之后，就会提供给用户，然后此时用户就会通过提词来引导它来进行生成，然后这个就是推理采样的过程。&lt;/p>
&lt;hr>
&lt;p>首先，什么是预训练?&lt;/p>
&lt;p>在预训练阶段，LLM 的本质只是一个极其庞大的“接话游戏”。&lt;/p>
&lt;p>给它前半句话，它根据概率分布去填下一个词。&lt;/p>
&lt;p>例如输入“法国的首都是”，它输出的概率分布里“巴黎”极高，“柏林”或“伦敦”极低。&lt;/p>
&lt;p>预训练决定了模型“知道多少知识”。&lt;/p>
&lt;p>但纯粹的预训练模型是无法直接当“助手”用的。&lt;/p>
&lt;p>如果你问它：“How are you?”，预训练模型可能会顺着这个开头，
直接帮你续写出一篇包含对话的英语课文，因为它根本不知道“自己现在是一个正在回答问题的助手”，它只知道在它的训练语料中，How are you 开头要接什么？可能接一篇文章，可能接一段对话的开始。&lt;/p>
&lt;p>为了让它变得可用，就需要&lt;strong>后训练&lt;/strong>。现在模型的效果突飞猛进，主要就是由于后训练的强大。&lt;/p>
&lt;p>后训练的本质，就是&lt;strong>强行改变预训练得到的概率分布，让模型的回答更偏向人类的喜好&lt;/strong>。&lt;/p>
&lt;p>面对用户的提问，预训练模型可能会产生多种输出方向：直接回答、拒绝回答、胡言乱语或给出一个错误答案。&lt;/p></description></item><item><title>RLHF: 基于人类偏好的强化学习</title><link>https://raychi.site/rlhf/</link><pubDate>Sat, 15 Mar 2025 00:00:00 +0800</pubDate><guid>https://raychi.site/rlhf/</guid><description>&lt;p>RLHF是&amp;quot;Reinforcement Learning from Human Feedback&amp;quot;的缩写，中文意思是&amp;quot;基于人类反馈的强化学习&amp;quot;。这是一种结合了强化学习和人类反馈的机器学习方法，旨在训练智能体（agent）在特定任务上表现得更好。以下是RLHF的一些关键特点：&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>强化学习&lt;/strong>：智能体通过与环境的交互来学习，不断调整策略以最大化累积奖励。它不需要显式的指导信号，而是通过试错来学习。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>人类反馈&lt;/strong>：在RLHF中，人类提供反馈来指导智能体的学习过程。这些反馈可以是奖励信号、偏好比较或直接的策略建议。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>结合两者&lt;/strong>：RLHF将强化学习与人类反馈相结合，以提高学习效率和性能。人类反馈可以纠正智能体的错误，提供更丰富的学习信号，帮助智能体更快地收敛到好的策略。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>应用广泛&lt;/strong>：RLHF可以应用于各种任务，如游戏、机器人控制、自然语言处理等。通过人类反馈，智能体可以更好地理解和适应人类的偏好和需求。&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>可解释性和安全性&lt;/strong>：通过引入人类反馈，RLHF可以提高智能体行为的可解释性，减少潜在的不安全或不道德的行为。&lt;/p>
&lt;/li>
&lt;/ol>
&lt;p>总的来说，RLHF是一种有效的学习方法，通过结合强化学习和人类反馈，可以训练出更智能、更符合人类需求的智能体。这种方法在人工智能领域具有广泛的应用前景。&lt;/p>
&lt;h2 id="rlhf在大语言模型中的应用">RLHF在大语言模型中的应用&lt;/h2>
&lt;p>RLHF最初在自然语言生成任务中得到了广泛应用，尤其是大语言模型（如GPT系列）的训练。它帮助解决了以下关键问题：&lt;/p>
&lt;h3 id="1-对齐问题alignment">1. 对齐问题（Alignment）&lt;/h3>
&lt;p>&lt;strong>问题&lt;/strong>：预训练的大语言模型虽然能够生成流畅的文本，但可能会产生有害、偏见或不符合人类价值观的内容。&lt;/p>
&lt;p>&lt;strong>解决方案&lt;/strong>：通过RLHF，可以使模型的行为与人类的价值观和偏好对齐。&lt;/p>
&lt;h3 id="2-指令遵循">2. 指令遵循&lt;/h3>
&lt;p>&lt;strong>问题&lt;/strong>：模型很难理解并遵循复杂的人类指令。&lt;/p>
&lt;p>&lt;strong>解决方案&lt;/strong>：通过人类反馈训练模型，使其能够更好地理解和执行人类的指令。&lt;/p>
&lt;h3 id="3-创造性和安全性平衡">3. 创造性和安全性平衡&lt;/h3>
&lt;p>&lt;strong>问题&lt;/strong>：需要模型既有创造性，又不产生不当内容。&lt;/p>
&lt;p>&lt;strong>解决方案&lt;/strong>：通过精心设计的奖励函数，引导模型在创造性和安全性之间找到平衡。&lt;/p>
&lt;h2 id="rlhf的三个阶段">RLHF的三个阶段&lt;/h2>
&lt;h3 id="第一阶段监督微调sft---supervised-fine-tuning">第一阶段：监督微调（SFT - Supervised Fine-Tuning）&lt;/h3>
&lt;p>在这个阶段，研究者会收集高质量的问答对数据，让模型学习如何回答问题。这些数据通常由人类专家编写，确保高质量和准确性。&lt;/p>
&lt;p>&lt;strong>步骤&lt;/strong>：&lt;/p>
&lt;ol>
&lt;li>收集高质量的指令-回答数据&lt;/li>
&lt;li>使用这些数据对预训练模型进行监督微调&lt;/li>
&lt;li>得到一个能够理解和遵循指令的基础模型&lt;/li>
&lt;/ol>
&lt;h3 id="第二阶段奖励模型训练reward-model-training">第二阶段：奖励模型训练（Reward Model Training）&lt;/h3>
&lt;p>在这个阶段，研究者训练一个奖励模型，用于评估模型输出的质量。这个模型学会了人类的偏好和价值观。&lt;/p>
&lt;p>&lt;strong>步骤&lt;/strong>：&lt;/p>
&lt;ol>
&lt;li>收集人类偏好数据（通常是比较数据）&lt;/li>
&lt;li>训练一个奖励模型，能够给不同的输出打分&lt;/li>
&lt;li>这个奖励模型将用于后续的强化学习训练&lt;/li>
&lt;/ol>
&lt;h3 id="第三阶段强化学习优化ppo">第三阶段：强化学习优化（PPO）&lt;/h3>
&lt;p>在这个阶段，模型使用PPO（Proximal Policy Optimization）算法进行训练，优化其输出以最大化从奖励模型得到的分数。&lt;/p>
&lt;p>&lt;strong>步骤&lt;/strong>：&lt;/p>
&lt;ol>
&lt;li>使用PPO算法&lt;/li>
&lt;li>让模型生成多个候选回答&lt;/li>
&lt;li>使用奖励模型评估这些回答&lt;/li>
&lt;li>根据奖励信号更新模型参数&lt;/li>
&lt;/ol>
&lt;h2 id="rlhf的优缺点">RLHF的优缺点&lt;/h2>
&lt;h3 id="优点">优点&lt;/h3>
&lt;ol>
&lt;li>&lt;strong>对齐效果显著&lt;/strong>：能够有效使模型行为与人类价值观对齐&lt;/li>
&lt;li>&lt;strong>提高安全性&lt;/strong>：显著减少了模型生成有害内容的概率&lt;/li>
&lt;li>&lt;strong>提升指令遵循能力&lt;/strong>：模型能够更好地理解和执行复杂指令&lt;/li>
&lt;li>&lt;strong>改善输出质量&lt;/strong>：整体上提高了模型输出的质量和有用性&lt;/li>
&lt;/ol>
&lt;h3 id="缺点">缺点&lt;/h3>
&lt;ol>
&lt;li>&lt;strong>成本高昂&lt;/strong>：需要大量人工标注和反馈数据&lt;/li>
&lt;li>&lt;strong>训练复杂&lt;/strong>：RLHF的训练过程复杂且不稳定&lt;/li>
&lt;li>&lt;strong>数据依赖性&lt;/strong>：性能很大程度上依赖于人类反馈数据的质量和多样性&lt;/li>
&lt;li>&lt;strong>潜在偏见&lt;/strong>：可能继承训练数据中的偏见&lt;/li>
&lt;/ol>
&lt;h2 id="常见的rlhf实现">常见的RLHF实现&lt;/h2>
&lt;h3 id="instructgpt">InstructGPT&lt;/h3>
&lt;p>InstructGPT是OpenAI基于GPT-3.5开发的模型，通过RLHF技术显著提高了模型的指令遵循能力。&lt;/p>
&lt;h3 id="chatgpt">ChatGPT&lt;/h3>
&lt;p>ChatGPT是OpenAI开发的大型语言模型，基于GPT-3.5/4.0，使用了RLHF技术来实现更好的对话能力和安全性。&lt;/p>
&lt;h3 id="claude">Claude&lt;/h3>
&lt;p>Claude是Anthropic开发的AI助手，也使用了Constitutional AI和RLHF技术来实现安全性和有用性的平衡。&lt;/p></description></item><item><title>hugging face 抱抱脸</title><link>https://raychi.site/huggingface/</link><pubDate>Thu, 28 Nov 2024 00:00:00 +0800</pubDate><guid>https://raychi.site/huggingface/</guid><description>&lt;h2 id="什么是hugging-face">什么是hugging face？🤔&lt;/h2>
&lt;h2 id="如何使用">如何使用？🔧&lt;/h2>
&lt;h3 id="hugging-face模型下载">hugging face模型下载&lt;/h3>
&lt;blockquote>
&lt;p>首先安装相关库&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-bash" data-lang="bash">&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>pip install -U huggingface_hub
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/blockquote>
&lt;p>然后编写一个python文件，如下&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:#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"> 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">os&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>os&lt;span style="color:#ff7b72;font-weight:bold">.&lt;/span>environ[&lt;span style="color:#a5d6ff">&amp;#34;HF_ENDPOINT&amp;#34;&lt;/span>] &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> &lt;span style="color:#a5d6ff">&amp;#34;https://hf-mirror.com&amp;#34;&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 style="color:#ff7b72">from&lt;/span> &lt;span style="color:#ff7b72">huggingface_hub&lt;/span> &lt;span style="color:#ff7b72">import&lt;/span> hf_hub_download
&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:#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">10&lt;/span>&lt;span>repo_id &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> &lt;span style="color:#a5d6ff">&amp;#34;Duffy/Llama-820m&amp;#34;&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>&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>file_list &lt;span style="color:#ff7b72;font-weight:bold">=&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> &lt;span style="color:#a5d6ff">&amp;#34;.gitattributes&amp;#34;&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 style="color:#a5d6ff">&amp;#34;README.md&amp;#34;&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> &lt;span style="color:#a5d6ff">&amp;#34;config.json&amp;#34;&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">16&lt;/span>&lt;span> &lt;span style="color:#a5d6ff">&amp;#34;generation_config.json&amp;#34;&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> &lt;span style="color:#a5d6ff">&amp;#34;xxx.py&amp;#34;&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">18&lt;/span>&lt;span> &lt;span style="color:#a5d6ff">&amp;#34;pytorch_model.bin&amp;#34;&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> &lt;span style="color:#a5d6ff">&amp;#34;special_tokens_map.json&amp;#34;&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">20&lt;/span>&lt;span> &lt;span style="color:#a5d6ff">&amp;#34;tokenizer.json&amp;#34;&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 style="color:#a5d6ff">&amp;#34;tokenizer_config.json&amp;#34;&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>&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>local_dir &lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span> &lt;span style="color:#a5d6ff">&amp;#34;./your_model_file_dir&amp;#34;&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>
&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 style="color:#ff7b72">for&lt;/span> file_name &lt;span style="color:#ff7b72;font-weight:bold">in&lt;/span> file_list:
&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> hf_hub_download(repo_id&lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span>repo_id, filename&lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span>file_name, local_dir&lt;span style="color:#ff7b72;font-weight:bold">=&lt;/span>local_dir)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="hugging-face-hub">Hugging Face Hub&lt;/h2>
&lt;p>Hugging Face Hub 是一个集成模型、数据集、和其他机器学习资源的平台，它使得分享和使用模型变得非常方便。Hub 提供了易于使用的 API、GitHub 风格的模型管理，并且是目前许多 NLP 和计算机视觉任务中最流行的模型资源。&lt;/p></description></item><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>