Repository navigation
Expand file tree
/
Copy pathch11.html
More file actions
418 lines (372 loc) · 20.1 KB
/
Copy pathch11.html
File metadata and controls
418 lines (372 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="第十一章:大语言模型(LLM)详解 - AI学习笔记">
<meta name="keywords" content="AI, 人工智能, 机器学习, 深度学习">
<meta name="author" content="AI Learner">
<title>第十一章:大语言模型(LLM)详解 - AI学习笔记</title>
<!-- Google Fonts - Inter -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<!-- KaTeX - 轻量级数学公式渲染 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"
onload="renderMathInElement(document.body, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '\\[', right: '\\]', display: true},
{left: '\\(', right: '\\)', display: false},
{left: '$', right: '$', display: false}
],
throwOnError: false
});"></script>
<!-- Prism.js 代码高亮 - tomorrow-night 主题 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-bash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
<!-- 自定义样式表 -->
<link rel="stylesheet" href="../../css/main.css">
<link rel="stylesheet" href="../../css/dark-mode.css">
<link rel="stylesheet" href="../../css/mobile-optimizations.css">
<!-- 内联CSS样式 -->
<!-- AI-Notes共享样式 -->
<link rel="stylesheet" href="assets/ai-notes.css">
</head>
<body>
<!-- ==================== 导航栏 ==================== -->
<nav class="ai-nav">
<a href="../../index.html" class="ai-nav-brand">
<i class="fas fa-brain"></i>
AI学习笔记
</a>
<ul class="ai-nav-links" id="navLinks">
<li><a href="index.html"><i class="fas fa-home"></i> 首页</a></li>
<li><a href="ch06.html"><i class="fas fa-box"></i> 模型</a></li>
<li><a href="ch07.html"><i class="fas fa-dumbbell"></i> 训练</a></li>
<li><a href="ch08.html"><i class="fas fa-chart-bar"></i> 评估调优</a></li>
<li><a href="ch09.html"><i class="fas fa-th"></i> CNN</a></li>
<li><a href="ch10.html"><i class="fas fa-random"></i> Transformer</a></li>
<li><a href="ch11.html"><i class="fas fa-language"></i> LLM</a></li>
<li><a href="ch12.html"><i class="fas fa-rocket"></i> 推理部署</a></li>
<li><a href="ch13.html"><i class="fas fa-search"></i> RAG</a></li>
<li><a href="ch14.html"><i class="fas fa-arrows-alt"></i> 向量Embedding</a></li>
</ul>
<button class="ai-nav-toggle" id="navToggle" aria-label="切换导航">
<i class="fas fa-bars"></i>
</button>
</nav>
<!-- ==================== 章节导航 ==================== -->
<nav class="ai-chapter-nav" style="background: #f7fafc; border-bottom: 1px solid #e2e8f0; padding: 1rem 2rem; margin-top: 64px;">
<div style="max-width: 1400px; margin: 0 auto; display: flex; justify-content: space-between; align-items: center;">
<div class="breadcrumb" style="font-size: 0.9rem; color: #718096;">
<a href="../../index.html" style="color: #4299e1;"><i class="fas fa-home"></i> 首页</a>
<span style="margin: 0 0.5rem;">/</span>
<a href="index.html" style="color: #4299e1;">AI学习笔记</a>
<span style="margin: 0 0.5rem;">/</span>
<span style="color: #2d3748; font-weight: 600;">第十一章:大语言模型(LLM)详解</span>
</div>
<div class="chapter-links" style="display: flex; gap: 1rem;">
<a href="ch10.html" class="prev-chapter" style="color: #4299e1; font-size: 0.9rem;"><i class="fas fa-arrow-left"></i> 上一章</a>
<a href="ch12.html" class="next-chapter" style="color: #4299e1; font-size: 0.9rem;">下一章 <i class="fas fa-arrow-right"></i></a>
</div>
</div>
</nav>
<!-- ==================== 主内容区域 ==================== -->
<div class="content-wrapper">
<!-- ========== 主内容 ========== -->
<main class="main-content">
<!-- 第十一章:大语言模型(LLM)详解 -->
<!-- ============================================================ -->
<h2 id="sec1" class="ai-section-title"><i class="fas fa-language"></i> 第十一章:大语言模型(LLM)详解</h2>
<div class="ai-box ai-box--objectives">
<h3 id="sub1-1"><i class="fas fa-bullseye"></i> 学习目标</h3>
<ul>
<li>理解LLM的预训练任务和训练流程</li>
<li>掌握分词算法(BPE)的基本原理</li>
<li>理解推理采样策略(Greedy、Beam Search、Temperature、Top-k、Top-p)</li>
<li>了解模型压缩技术(量化、蒸馏、剪枝)</li>
<li>能够使用transformers库进行文本生成</li>
</ul>
</div>
<div class="ai-box ai-box--prereq">
<h3 id="sub1-2"><i class="fas fa-book"></i> 前置要求</h3>
<p>第十章:Transformer架构(理解Self-Attention、位置编码和Decoder结构)。</p>
</div>
<figure class="ai-figure">
<img src="https://trae-api-cn.mchost.guru/api/ide/v1/text_to_image?prompt=Large%20language%20model%20architecture%20transformer%20decoder%20layers%20stacked%20token%20prediction%2C%20clean%20educational%20infographic%20style%2C%20flat%20design%2C%20white%20background&image_size=landscape_4_3" alt="大语言模型Transformer架构概念图" loading="lazy">
<figcaption><strong>图11-1</strong> 大语言模型架构:Transformer Decoder层堆叠与下一词预测机制</figcaption>
</figure>
<div class="concept-section">
<h2 id="sec2"><i class="fas fa-cogs"></i> LLM的工作原理</h2>
<p><span class="keyword-highlight">大语言模型(Large Language Model,LLM)</span>是基于Transformer Decoder架构的大规模预训练模型,通过预测下一个词来生成文本。</p>
<div class="concept-box">
<h4><i class="fas fa-language"></i> LLM工作的5个步骤</h4>
<p>1. <strong>接收输入</strong>:用户输入文本(如"人工智能的未来是")<br/>
2. <strong>分词处理</strong>:将文本切分为Token序列<br/>
3. <strong>嵌入编码</strong>:将Token转换为向量表示,加上位置编码<br/>
4. <strong>Transformer处理</strong>:通过多层Self-Attention和前馈网络处理<br/>
5. <strong>预测输出</strong>:根据最后一个Token的表示,预测下一个Token的概率分布</p>
</div>
<h3 id="sub2-3">预训练任务详解</h3>
<p>LLM的预训练通常基于以下两种任务之一:</p>
<h4>1. Next Token Prediction(下一个词预测)</h4>
<p>这是GPT系列使用的预训练任务。模型看到前面的词,预测下一个词。</p>
<div class="concept-box success">
<h4><i class="fas fa-lightbulb"></i> 示例</h4>
<p>输入:"人工智能正在"<br/>
目标:预测下一个Token<br/>
可能输出:"改变"(概率30%)、"发展"(概率25%)、"进步"(概率15%)...<br/><br/>
模型通过在海量文本上反复做这个任务,学会了语言的规律、知识和推理能力。</p>
</div>
<h4>2. 掩码语言模型(Masked Language Model)</h4>
<p>这是BERT使用的预训练任务。随机遮盖输入中的一些词,让模型预测被遮盖的词。</p>
<div class="concept-box">
<h4><i class="fas fa-mask"></i> 示例</h4>
<p>原文:"人工智能正在改变世界"<br/>
掩码后:"[MASK]正在[MASK]世界"<br/>
目标:预测[MASK]位置的词("人工智能"和"改变")</p>
</div>
<h3 id="sub2-4">分词算法</h3>
<p><span class="keyword-highlight">分词(Tokenization)</span>是将文本切分为模型可处理的Token序列的过程。</p>
<h4>BPE(Byte Pair Encoding)原理</h4>
<p>BPE是最常用的分词算法,核心思想是:从字符开始,反复合并最高频的字符对,逐步构建词表。</p>
<div class="concept-box">
<h4><i class="fas fa-puzzle-piece"></i> BPE简单示例</h4>
<p>假设语料中出现很多次 "low"、"lower"、"newest":<br/><br/>
<strong>初始</strong>(字符级别):<br/>
l o w l o w e r n e w e s t<br/><br/>
<strong>第1步</strong>:e 和 w 最常相邻 → 合并为 ew<br/>
l o w l o w e r n <strong>ew</strong> e s t<br/><br/>
<strong>第2步</strong>:n 和 ew 最常相邻 → 合并为 new<br/>
l o w l o w e r <strong>new</strong> e s t<br/><br/>
<strong>第3步</strong>:lo 和 w 最常相邻 → 合并为 low<br/>
<strong>low</strong> <strong>low</strong> e r new e s t<br/><br/>
最终词表包含:l, o, w, e, r, n, ew, new, lo, low, lowe, lower, ...</p>
</div>
<h3 id="sub2-5">推理采样策略</h3>
<p>LLM在生成文本时,需要从概率分布中选择下一个Token。不同的采样策略影响输出的多样性和质量。</p>
<table class="comparison-table">
<tr>
<th>策略</th>
<th>原理</th>
<th>优点</th>
<th>缺点</th>
<th>适用场景</th>
</tr>
<tr>
<td><strong>Greedy(贪心)</strong></td>
<td>每步选概率最高的Token</td>
<td>确定性强,速度快</td>
<td>容易重复,缺乏多样性</td>
<td>翻译、摘要等确定性任务</td>
</tr>
<tr>
<td><strong>Beam Search</strong></td>
<td>同时保留多个候选序列</td>
<td>全局最优,质量高</td>
<td>计算量大,输出保守</td>
<td>机器翻译</td>
</tr>
<tr>
<td><strong>Temperature</strong></td>
<td>调整概率分布的"尖锐度"</td>
<td>控制输出的随机性</td>
<td>需要调参</td>
<td>通用(低=事实,高=创意)</td>
</tr>
<tr>
<td><strong>Top-k</strong></td>
<td>只从概率最高的k个Token中采样</td>
<td>过滤低质量选项</td>
<td>k值固定,不够灵活</td>
<td>对话、故事生成</td>
</tr>
<tr>
<td><strong>Top-p(核采样)</strong></td>
<td>从概率累积达到p的最小Token集合中采样</td>
<td>动态调整候选集大小</td>
<td>实现稍复杂</td>
<td>通用,最推荐</td>
</tr>
</table>
<div class="concept-box warning">
<h4><i class="fas fa-sliders-h"></i> 采样参数选择建议</h4>
<p>• <strong>编程/数学</strong>:temperature=0.1~0.2,top_p=0.9(确定性优先)<br/>
• <strong>一般对话</strong>:temperature=0.5~0.7,top_p=0.9(平衡)<br/>
• <strong>创意写作</strong>:temperature=0.8~1.0,top_p=0.95(多样性优先)<br/>
• <strong>事实问答</strong>:temperature=0,使用Greedy(最准确)</p>
</div>
<h3 id="sub2-6">使用transformers库生成文本</h3>
<pre><code class="language-python">from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载模型和分词器
model_name = "gpt2" # 可替换为 "meta-llama/Llama-2-7b-hf" 等
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# 编码输入文本
prompt = "人工智能的未来发展方向包括"
input_ids = tokenizer.encode(prompt, return_tensors="pt")
# 生成文本(使用不同的采样策略)
# 方式1:贪心搜索(确定性输出)
output_greedy = model.generate(input_ids, max_new_tokens=50, do_sample=False)
print("Greedy:", tokenizer.decode(output_greedy[0], skip_special_tokens=True))
# 方式2:带温度的采样(创造性输出)
output_temp = model.generate(
input_ids,
max_new_tokens=50,
do_sample=True,
temperature=0.8,
top_p=0.9
)
print("Temperature:", tokenizer.decode(output_temp[0], skip_special_tokens=True))
# 方式3:Top-k采样
output_topp = model.generate(
input_ids,
max_new_tokens=50,
do_sample=True,
top_k=50,
top_p=0.95
)
print("Top-k+p:", tokenizer.decode(output_topp[0], skip_special_tokens=True))</code></pre>
<h3 id="sub2-7">模型压缩技术</h3>
<p>为了在资源有限的设备上运行大模型,需要使用模型压缩技术:</p>
<table class="comparison-table">
<tr>
<th>技术</th>
<th>原理</th>
<th>效果</th>
<th>代价</th>
</tr>
<tr>
<td><strong>量化(Quantization)</strong></td>
<td>降低参数精度(FP16→INT8→INT4)</td>
<td>显存减少50%-75%</td>
<td>精度略有下降</td>
</tr>
<tr>
<td><strong>知识蒸馏(Distillation)</strong></td>
<td>用大模型教小模型</td>
<td>小模型获得接近大模型的效果</td>
<td>需要训练过程</td>
</tr>
<tr>
<td><strong>剪枝(Pruning)</strong></td>
<td>删除不重要的参数或层</td>
<td>减少模型大小和计算量</td>
<td>可能影响性能</td>
</tr>
<tr>
<td><strong>LoRA(低秩适配)</strong></td>
<td>只训练少量额外参数</td>
<td>微调成本大幅降低</td>
<td>推理时需要合并参数</td>
</tr>
</table>
<div class="concept-box success">
<h4><i class="fas fa-lightbulb"></i> 实用建议</h4>
<p><strong>本地运行大模型</strong>:<br/>
• 7B模型 + INT4量化 ≈ 4GB显存(RTX 3060可运行)<br/>
• 13B模型 + INT4量化 ≈ 8GB显存(RTX 4070可运行)<br/>
• 推荐工具:llama.cpp、Ollama、vLLM<br/>
• 量化方法:GPTQ、AWQ、GGUF</p>
</div>
</div>
<div class="concept-section video-section">
<h2 id="sec3"><i class="fas fa-play-circle"></i> 推荐视频</h2>
<p>如果你觉得文字讲解不够直观,推荐观看以下视频:</p>
<div class="video-card">
<div class="video-icon"><i class="fab fa-bilibili"></i></div>
<div class="video-info">
<h4>生成式AI导论 2025版</h4>
<p class="video-author">作者:李宏毅</p>
<p class="video-desc">为什么推荐:最新的大语言模型科普</p>
<a href="https://www.bilibili.com/video/BV1bHASeXEBh" target="_blank" class="video-link">
<i class="fas fa-external-link-alt"></i> 点击观看
</a>
</div>
</div>
</div>
<div class="ai-box ai-box--summary" id="summary">
<h3 id="sub3-8"><i class="fas fa-clipboard-list"></i> 本章小结</h3>
<ol>
<li>LLM基于Transformer Decoder架构,通过Next Token Prediction预训练任务在海量文本上学习语言规律和知识。</li>
<li>BPE分词算法从字符级别开始,通过反复合并高频字符对构建词表,平衡了词表大小和语义完整性。</li>
<li>推理采样策略决定了生成文本的多样性和质量:Greedy确定性最强,Temperature和Top-p提供灵活的随机性控制。</li>
<li>模型压缩技术(量化、蒸馏、剪枝、LoRA)使大模型能在资源有限的设备上运行,是AI落地应用的关键。</li>
<li>Hugging Face的transformers库提供了便捷的API来加载预训练模型、进行文本生成和模型微调。</li>
</ol>
</div>
<div class="ai-box ai-box--exercise" id="exercises">
<h3 id="sub3-9"><i class="fas fa-pencil-alt"></i> 练习题</h3>
<ol>
<li>
<p><strong>GPT系列模型使用的预训练任务是什么?</strong></p>
<p>A. 掩码语言模型(MLM)<br/>B. Next Token Prediction(下一个词预测)<br/>C. 图像分类<br/>D. 聚类分析</p>
<p><strong>答案:B</strong>。GPT系列使用Next Token Prediction作为预训练任务,即根据前面的词预测下一个词。</p>
</li>
<li>
<p><strong>如果需要生成创意性强的文本,应该怎样设置采样参数?</strong></p>
<p>A. temperature=0.1, do_sample=False<br/>B. temperature=0.9, top_p=0.95, do_sample=True<br/>C. temperature=0, top_k=1<br/>D. 不使用任何采样策略</p>
<p><strong>答案:B</strong>。较高的temperature(0.8-1.0)和较高的top_p(0.95)会增加输出的多样性和创造性。</p>
</li>
<li>
<p><strong>BPE分词算法的核心思想是什么?</strong></p>
<p>A. 随机将文本切分为固定长度的片段<br/>B. 从字符开始,反复合并最高频的字符对来构建词表<br/>C. 使用词典匹配,遇到未知词就跳过<br/>D. 将每个汉字作为一个Token</p>
<p><strong>答案:B</strong>。BPE(Byte Pair Encoding)从字符级别开始,通过迭代合并语料中最高频的相邻字符对来构建子词词表。</p>
</li>
</ol>
</div>
<!-- ============================================================ -->
</main>
</div>
<!-- ==================== 章节底部导航 ==================== -->
<nav class="chapter-nav-bottom" style="background: #f7fafc; border-top: 1px solid #e2e8f0; padding: 2rem; margin-top: 3rem;">
<div style="max-width: 900px; margin: 0 auto; display: flex; justify-content: space-between; align-items: center;">
<a href="ch10.html" class="prev-chapter" style="color: #4299e1; font-size: 1rem; padding: 0.75rem 1.5rem; border: 2px solid #4299e1; border-radius: 8px; transition: all 0.3s;">
<i class="fas fa-arrow-left"></i> 上一章
</a>
<span style="color: #718096; font-size: 0.9rem;">第十一章:大语言模型(LLM)详解</span>
<a href="ch12.html" class="next-chapter" style="color: #4299e1; font-size: 1rem; padding: 0.75rem 1.5rem; border: 2px solid #4299e1; border-radius: 8px; transition: all 0.3s;">
下一章 <i class="fas fa-arrow-right"></i>
</a>
</div>
</nav>
<!-- ==================== 页脚 ==================== -->
<footer class="footer">
<div class="footer-content">
<div class="footer-section">
<h3 id="sub3-10"><i class="fas fa-graduation-cap"></i> AI学习笔记</h3>
<p>从零开始系统学习人工智能</p>
<p>涵盖前置基础、核心原理、工程实践、应用工具、进阶展望</p>
</div>
<div class="footer-section">
<h3 id="sub3-11"><i class="fas fa-bookmark"></i> 内容导航</h3>
<p><a href="ch06.html" style="color: rgba(255,255,255,0.9);">第六章:模型</a></p>
<p><a href="ch07.html" style="color: rgba(255,255,255,0.9);">第七章:训练</a></p>
<p><a href="ch08.html" style="color: rgba(255,255,255,0.9);">第八章:模型评估与调优</a></p>
<p><a href="ch09.html" style="color: rgba(255,255,255,0.9);">第九章:CNN</a></p>
<p><a href="ch10.html" style="color: rgba(255,255,255,0.9);">第十章:Transformer</a></p>
</div>
<div class="footer-section">
<h3 id="sub3-12"><i class="fas fa-info-circle"></i> 说明</h3>
<p>本笔记仅供学习参考,内容如有错误欢迎指正。</p>
<p>AI技术发展迅速,部分信息可能随时间变化。</p>
</div>
</div>
<div class="footer-bottom">
<p>AI学习笔记 © 2024 | 持续更新中</p>
</div>
</footer>
<!-- Scripts -->
<script src="../../js/mobile-menu.js"></script>
<script src="../../js/dark-mode.js"></script>
<script src="assets/ai-notes.js"></script>
</body>
</html>