Repository navigation
Expand file tree
/
Copy pathch08.html
More file actions
439 lines (386 loc) · 20.9 KB
/
Copy pathch08.html
File metadata and controls
439 lines (386 loc) · 20.9 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!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="第八章:模型评估与调优 - AI学习笔记">
<meta name="keywords" content="AI, 人工智能, 机器学习, 深度学习">
<meta name="author" content="AI Learner">
<title>第八章:模型评估与调优 - 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;">第八章:模型评估与调优</span>
</div>
<div class="chapter-links" style="display: flex; gap: 1rem;">
<a href="ch07.html" class="prev-chapter" style="color: #4299e1; font-size: 0.9rem;"><i class="fas fa-arrow-left"></i> 上一章</a>
<a href="ch09.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">
<!-- 第八章:模型评估与调优 -->
<!-- ============================================================ -->
<h2 id="sec1" class="ai-section-title"><i class="fas fa-chart-bar"></i> 第八章:模型评估与调优</h2>
<div class="ai-box ai-box--objectives">
<h3 id="sub1-1"><i class="fas fa-bullseye"></i> 学习目标</h3>
<ul>
<li>理解模型评估的意义和核心指标</li>
<li>掌握准确率、精确率、召回率、F1分数的计算与适用场景</li>
<li>理解过拟合与欠拟合的成因及偏差-方差权衡</li>
<li>掌握正则化方法(L1、L2、Dropout、数据增强)</li>
<li>了解交叉验证的原理与应用</li>
</ul>
</div>
<div class="ai-box ai-box--prereq">
<h3 id="sub1-2"><i class="fas fa-book"></i> 前置要求</h3>
<p>第七章:训练(理解损失函数、训练过程和常见问题)。</p>
</div>
<figure class="ai-figure">
<img src="https://trae-api-cn.mchost.guru/api/ide/v1/text_to_image?prompt=Model%20evaluation%20metrics%20confusion%20matrix%20ROC%20curve%20precision%20recall%20accuracy%2C%20clean%20educational%20infographic%20style%2C%20flat%20design%2C%20white%20background&image_size=landscape_4_3" alt="模型评估指标概念图" loading="lazy">
<figcaption><strong>图8-1</strong> 模型评估指标:混淆矩阵与ROC曲线的可视化示意</figcaption>
</figure>
<div class="concept-section">
<h2 id="sec2"><i class="fas fa-tachometer-alt"></i> 为什么需要评估?</h2>
<p>训练完成后,需要评估模型性能,判断模型是否达到要求,是否可以投入使用。</p>
<div class="concept-box">
<h4><i class="fas fa-question-circle"></i> 核心问题</h4>
<p>• 模型准确吗?<br/>• 模型稳定吗?<br/>• 模型能处理各种情况吗?</p>
</div>
</div>
<div class="concept-section">
<h2 id="sec3"><i class="fas fa-bullseye"></i> 核心评估指标</h2>
<h3 id="sub3-3">准确率(Accuracy)</h3>
<p><span class="keyword-highlight">准确率</span>是模型预测正确的比例。</p>
<div class="concept-box success">
<h4><i class="fas fa-calculator"></i> 公式</h4>
<p>\[ \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN} \]<br/><br/>
例如:100个样本,预测正确90个,准确率=90%</p>
</div>
<h3 id="sub3-4">精确率与召回率</h3>
<p>这两个指标用于评估分类模型的不同角度:</p>
<table class="comparison-table">
<tr>
<th>指标</th>
<th>含义</th>
<th>公式</th>
<th>应用场景</th>
</tr>
<tr>
<td><strong>精确率(Precision)</strong></td>
<td>预测为正的样本中,真正为正的比例</td>
<td>\(P = \frac{TP}{TP + FP}\)</td>
<td>"我说对的,有多少真的对"</td>
</tr>
<tr>
<td><strong>召回率(Recall)</strong></td>
<td>真正的正样本中,被预测为正的比例</td>
<td>\(R = \frac{TP}{TP + FN}\)</td>
<td>"真的对的,我找出来多少"</td>
</tr>
</table>
<div class="concept-box warning">
<h4><i class="fas fa-balance-scale"></i> 精确率 vs 召回率权衡</h4>
<p>• 高精确率:宁可漏过,不可错报(医疗诊断——不误诊健康人)<br/>
• 高召回率:宁可错报,不可漏过(垃圾邮件检测——不漏掉垃圾邮件)<br/>
• 通常需要根据场景权衡</p>
</div>
<h3 id="sub3-5">F1分数</h3>
<p><span class="keyword-highlight">F1分数</span>是精确率和召回率的调和平均,综合评估模型性能。</p>
<div class="concept-box">
<h4><i class="fas fa-calculator"></i> F1分数公式</h4>
<p>\[ F1 = 2 \times \frac{P \times R}{P + R} \]<br/><br/>
当精确率和召回率都很重要时,使用F1分数作为综合指标。</p>
</div>
<h3 id="sub3-6">损失值(Loss)</h3>
<p><span class="keyword-highlight">损失值</span>衡量模型预测与真实答案的差距,训练过程中持续下降说明模型在学习。</p>
<ul>
<li>训练初期:损失值高,模型在学习</li>
<li>训练中期:损失值下降,模型进步</li>
<li>训练后期:损失值稳定,模型收敛</li>
</ul>
<h3 id="sub3-7">评估指标的Python代码示例</h3>
<pre><code class="language-python">from sklearn.metrics import (
accuracy_score, precision_score,
recall_score, f1_score, confusion_matrix
)
# 真实标签和模型预测
y_true = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0, 1, 0]
# 计算各项指标
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
cm = confusion_matrix(y_true, y_pred)
print(f"准确率: {accuracy:.2%}")
print(f"精确率: {precision:.2%}")
print(f"召回率: {recall:.2%}")
print(f"F1分数: {f1:.2%}")
print(f"混淆矩阵:\n{cm}")
# 输出示例:
# 准确率: 80.00%
# 精确率: 75.00%
# 召回率: 83.33%
# F1分数: 78.95%
# 混淆矩阵:
# [[3 1]
# [1 5]]</code></pre>
<h3 id="sub3-8">混淆矩阵</h3>
<p><span class="keyword-highlight">混淆矩阵</span>是展示分类结果的表格,清晰显示预测正确和错误的情况。</p>
<table class="comparison-table">
<tr>
<th></th>
<th>预测为正</th>
<th>预测为负</th>
</tr>
<tr>
<td><strong>实际为正</strong></td>
<td>真正例(TP)</td>
<td>假负例(FN)</td>
</tr>
<tr>
<td><strong>实际为负</strong></td>
<td>假正例(FP)</td>
<td>真负例(TN)</td>
</tr>
</table>
</div>
<div class="concept-section">
<h2 id="sec4"><i class="fas fa-balance-scale"></i> 过拟合与欠拟合</h2>
<p>模型训练中最重要的两个问题就是过拟合和欠拟合,它们与<span class="keyword-highlight">偏差-方差权衡(Bias-Variance Tradeoff)</span>密切相关。</p>
<h3 id="sub4-9">偏差与方差</h3>
<table class="comparison-table">
<tr>
<th>概念</th>
<th>含义</th>
<th>对应问题</th>
</tr>
<tr>
<td><strong>偏差(Bias)</strong></td>
<td>模型预测的平均值与真实值的差距</td>
<td>偏差大 → 欠拟合(模型太简单)</td>
</tr>
<tr>
<td><strong>方差(Variance)</strong></td>
<td>模型在不同训练集上的预测波动</td>
<td>方差大 → 过拟合(模型太复杂)</td>
</tr>
</table>
<div class="concept-box">
<h4><i class="fas fa-bullseye"></i> 靶心类比</h4>
<p>想象射击靶心:<br/>
• <strong>低偏差+低方差</strong>:所有弹孔都集中在靶心 → 理想模型<br/>
• <strong>高偏差+低方差</strong>:弹孔集中但偏离靶心 → 欠拟合<br/>
• <strong>低偏差+高方差</strong>:弹孔散布在靶心周围 → 过拟合<br/>
• <strong>高偏差+高方差</strong>:弹孔散布且偏离靶心 → 最差情况</p>
</div>
<h3 id="sub4-10">学习曲线</h3>
<p>通过观察学习曲线(训练损失和验证损失随训练进行的变化)可以诊断问题:</p>
<ul>
<li><strong>欠拟合</strong>:训练损失和验证损失都很高,且差距不大 → 模型太简单</li>
<li><strong>过拟合</strong>:训练损失很低,但验证损失很高且在上升 → 模型太复杂</li>
<li><strong>良好拟合</strong>:两者都逐渐下降并趋于稳定,差距适中</li>
</ul>
</div>
<div class="concept-section">
<h2 id="sec5"><i class="fas fa-shield-alt"></i> 正则化方法</h2>
<p><span class="keyword-highlight">正则化</span>是防止过拟合的核心技术,通过限制模型复杂度来提高泛化能力。</p>
<h3 id="sub5-11">L1 正则化(Lasso)</h3>
<p>在损失函数中添加参数绝对值之和:</p>
<div class="concept-box">
<h4><i class="fas fa-calculator"></i> L1正则化公式</h4>
<p>\[ L_{\text{total}} = L_{\text{original}} + \lambda \sum_{i} |w_i| \]<br/><br/>
效果:使部分参数变为0,起到特征选择的作用,产生稀疏模型。</p>
</div>
<h3 id="sub5-12">L2 正则化(Ridge)</h3>
<p>在损失函数中添加参数平方和:</p>
<div class="concept-box">
<h4><i class="fas fa-calculator"></i> L2正则化公式</h4>
<p>\[ L_{\text{total}} = L_{\text{original}} + \lambda \sum_{i} w_i^2 \]<br/><br/>
效果:使参数值变小但不会变为0,防止任何单一参数对结果影响过大。<br/>
这是最常用的正则化方法,PyTorch中的weight_decay就是L2正则化。</p>
</div>
<h3 id="sub5-13">Dropout</h3>
<p><span class="keyword-highlight">Dropout</span>在训练过程中随机"关闭"一部分神经元,迫使网络不依赖任何单个神经元。</p>
<div class="concept-box success">
<h4><i class="fas fa-lightbulb"></i> Dropout原理</h4>
<p>• 训练时:每个神经元以概率p被"丢弃"(输出设为0)<br/>
• 测试时:所有神经元都参与,但输出按比例缩小<br/>
• 效果:相当于训练了多个子网络的集成,减少过拟合<br/>
• 常用丢弃率:0.2~0.5</p>
</div>
<h3 id="sub5-14">数据增强(Data Augmentation)</h3>
<p>通过变换训练数据来增加数据多样性,让模型学到更鲁棒的特征。</p>
<ul>
<li><strong>图像</strong>:旋转、翻转、裁剪、颜色变换、添加噪声</li>
<li><strong>文本</strong>:同义词替换、随机删除、回译</li>
<li><strong>语音</strong>:变速、变调、添加背景噪声</li>
</ul>
</div>
<div class="concept-section">
<h2 id="sec6"><i class="fas fa-sync-alt"></i> 交叉验证</h2>
<p><span class="keyword-highlight">交叉验证</span>是一种更可靠的模型评估方法,能充分利用数据,减少评估结果的偶然性。</p>
<h3 id="sub6-15">K折交叉验证(K-Fold Cross Validation)</h3>
<p>将数据集分成K个大小相等的子集("折"),每次用其中一折作为验证集,其余K-1折作为训练集,重复K次,取K次评估结果的平均值。</p>
<div class="flow-diagram">
<div class="flow-item">第1折: [验证] [训练] [训练] [训练] [训练]</div>
<span class="flow-arrow">→</span>
<div class="flow-item">第2折: [训练] [验证] [训练] [训练] [训练]</div>
<span class="flow-arrow">→</span>
<div class="flow-item">第3折: [训练] [训练] [验证] [训练] [训练]</div>
<span class="flow-arrow">→</span>
<div class="flow-item">第4折: [训练] [训练] [训练] [验证] [训练]</div>
<span class="flow-arrow">→</span>
<div class="flow-item">第5折: [训练] [训练] [训练] [训练] [验证]</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-info-circle"></i> K折交叉验证要点</h4>
<p>• 常用K值:5或10<br/>
• 每个样本都会被用作验证集一次<br/>
• 最终性能 = K次评估的平均值<br/>
• 比简单的训练/测试分割更可靠<br/>
• 计算成本更高(需要训练K次模型)</p>
</div>
</div>
<div class="ai-box ai-box--summary" id="summary">
<h3 id="sub6-16"><i class="fas fa-clipboard-list"></i> 本章小结</h3>
<ol>
<li>模型评估的核心指标包括准确率、精确率、召回率和F1分数,不同场景应选择不同指标。</li>
<li>过拟合(训练好、测试差)和欠拟合(训练差、测试差)是最常见的两个问题,对应偏差-方差权衡。</li>
<li>正则化是防止过拟合的主要手段,包括L1正则化(稀疏化)、L2正则化(参数衰减)、Dropout和数据增强。</li>
<li>K折交叉验证通过多次训练-验证循环提供更可靠的性能评估,减少评估结果的偶然性。</li>
<li>混淆矩阵是分析分类结果的重要工具,从中可以推导出所有核心评估指标。</li>
</ol>
</div>
<div class="ai-box ai-box--exercise" id="exercises">
<h3 id="sub6-17"><i class="fas fa-pencil-alt"></i> 练习题</h3>
<ol>
<li>
<p><strong>在医疗诊断场景中,以下哪个指标最重要?</strong></p>
<p>A. 准确率<br/>B. 精确率(宁可漏过,不可错报)<br/>C. 召回率(宁可错报,不可漏过)<br/>D. 以上都不重要</p>
<p><strong>答案:B</strong>。医疗诊断中,将健康人误诊为患病(低精确率)的代价很高,因此更看重精确率。</p>
</li>
<li>
<p><strong>以下哪种方法不能有效防止过拟合?</strong></p>
<p>A. 增加训练数据<br/>B. 使用Dropout<br/>C. 增加模型层数和参数<br/>D. 使用L2正则化</p>
<p><strong>答案:C</strong>。增加模型复杂度会加剧过拟合。增加数据、Dropout和L2正则化都是防止过拟合的有效方法。</p>
</li>
<li>
<p><strong>5折交叉验证中,模型总共需要训练多少次?</strong></p>
<p>A. 1次<br/>B. 5次<br/>C. 4次<br/>D. 10次</p>
<p><strong>答案:B</strong>。K折交叉验证需要训练K次,每次使用不同的验证折,5折交叉验证就是训练5次。</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="ch07.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;">第八章:模型评估与调优</span>
<a href="ch09.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="sub6-18"><i class="fas fa-graduation-cap"></i> AI学习笔记</h3>
<p>从零开始系统学习人工智能</p>
<p>涵盖前置基础、核心原理、工程实践、应用工具、进阶展望</p>
</div>
<div class="footer-section">
<h3 id="sub6-19"><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="sub6-20"><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>