-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch02.html
More file actions
810 lines (706 loc) · 41.4 KB
/
Copy pathch02.html
File metadata and controls
810 lines (706 loc) · 41.4 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<!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="数学基础,包括线性代数、微积分、概率论与统计">
<meta name="keywords" content="AI, 人工智能, 机器学习, 深度学习, Python, 数学基础">
<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">
<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="#" class="ai-nav-brand">
<i class="fas fa-brain"></i>
AI学习笔记
</a>
<ul class="ai-nav-links" id="navLinks">
<li><a href="#chapter0"><i class="fas fa-map"></i> 学习路线</a></li>
<li><a href="#chapter1"><i class="fab fa-python"></i> Python基础</a></li>
<li><a href="#chapter2"><i class="fas fa-square-root-variable"></i> 数学基础</a></li>
<li><a href="#chapter3"><i class="fas fa-robot"></i> 机器学习</a></li>
<li><a href="#chapter4"><i class="fas fa-network-wired"></i> 深度学习</a></li>
<li><a href="#chapter5"><i class="fas fa-tools"></i> 工程实践</a></li>
<li><a href="#chapter6"><i class="fas fa-rocket"></i> 进阶展望</a></li>
</ul>
<button class="ai-nav-toggle" id="navToggle" aria-label="切换导航">
<i class="fas fa-bars"></i>
</button>
</nav>
<!-- 章节导航 -->
<div class="ai-chapter-nav">
<a href="ch01.html" class="prev-chapter"><i class="fas fa-arrow-left"></i> 上一章</a>
<a href="index.html" class="toc-link"><i class="fas fa-list"></i> 目录</a>
<a href="ch02b.html" class="next-chapter">下一章 <i class="fas fa-arrow-right"></i></a>
</div>
<!-- 主内容 -->
<main class="ai-main">
<!-- ========================================== -->
<!-- 第二章:数学基础 -->
<!-- ========================================== -->
<section id="chapter2">
<h2 class="ai-section-title">
<i class="fas fa-square-root-variable"></i>
第二章:数学基础
</h2>
<div class="ai-box ai-box--objectives">
<h4><i class="fas fa-bullseye"></i> 学习目标</h4>
<ul>
<li>掌握线性代数核心概念:向量、矩阵、矩阵乘法及其几何意义</li>
<li>理解微积分基本概念:导数、偏导数、链式法则、梯度</li>
<li>熟悉概率统计基础:概率分布、期望、方差、贝叶斯定理</li>
<li>了解信息论基本概念:熵、交叉熵及其在AI中的应用</li>
<li>理解每个数学概念在AI中的具体应用场景</li>
</ul>
</div>
<div class="ai-box ai-box--prereq">
<h4><i class="fas fa-clipboard-check"></i> 前置要求</h4>
<ul>
<li>高中数学基础(函数、方程、基本概率)</li>
<li>了解Python和NumPy基本操作(第一章内容)</li>
<li>对抽象数学概念有一定的接受能力</li>
</ul>
</div>
<figure class="ai-figure">
<img src="https://trae-api-cn.mchost.guru/api/ide/v1/text_to_image?prompt=Mathematics%20fundamentals%20for%20AI%20linear%20algebra%20calculus%20probability%20statistics%20three%20pillars%20diagram%2C%20clean%20educational%20infographic%20style%2C%20flat%20design%2C%20white%20background&image_size=landscape_4_3" alt="AI数学基础三大支柱概念图" loading="lazy">
<figcaption><strong>图2-1</strong> AI数学基础三大支柱:线性代数、微积分、概率与统计</figcaption>
</figure>
<!-- 线性代数 -->
<h3 class="ai-section-subtitle">线性代数</h3>
<p>线性代数是AI的数学基石。神经网络的前向传播本质上是矩阵乘法,反向传播需要计算梯度,这些操作都建立在线性代数之上。</p>
<div class="concept-section">
<div class="concept-box">
<h4><i class="fas fa-arrows-alt-h"></i> 向量 (Vector)</h4>
<p>向量是具有大小和方向的量。在AI中,向量用于表示数据点、特征、词嵌入等。</p>
<div class="math-block">
<button class="math-toggle" onclick="toggleMath(this)"><i class="fas fa-chevron-up"></i></button>
<div class="math-content">
\[ \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} \in \mathbb{R}^n \]
</div>
<div class="math-description">n维列向量,表示一个具有n个特征的数据点</div>
</div>
<p><strong>数值示例:</strong>假设一个学生的成绩用向量表示:</p>
<div class="math-block">
<div class="math-content">
\[ \mathbf{s} = \begin{bmatrix} 90 \\ 85 \\ 78 \\ 92 \end{bmatrix} \]
</div>
<div class="math-description">分别对应:数学、英语、物理、化学成绩</div>
</div>
<p><strong>向量运算:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n \]
</div>
<div class="math-description">点积(内积):衡量两个向量的相似度</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>词向量(Word Embedding)将词语映射为高维向量;特征向量表示数据样本;注意力机制中的Query、Key、Value都是向量。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-th"></i> 矩阵 (Matrix)</h4>
<p>矩阵是按照矩形排列的数字阵列。在AI中,矩阵用于表示数据集、权重矩阵、变换等。</p>
<div class="math-block">
<div class="math-content">
\[ A = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix} \in \mathbb{R}^{m \times n} \]
</div>
<div class="math-description">m行n列的矩阵</div>
</div>
<p><strong>数值示例:</strong>一个3个样本、4个特征的数据集:</p>
<div class="math-block">
<div class="math-content">
\[ X = \begin{bmatrix} 1.0 & 2.3 & 0.5 & 3.1 \\ 0.8 & 1.9 & 0.7 & 2.8 \\ 1.2 & 2.1 & 0.3 & 3.5 \end{bmatrix} \]
</div>
<div class="math-description">3行(样本)x 4列(特征)</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>数据集通常组织为矩阵形式(样本数 x 特征数);神经网络每一层的权重都是一个矩阵;图像可以看作像素矩阵。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-times"></i> 矩阵乘法 (Matrix Multiplication)</h4>
<p>矩阵乘法是神经网络前向传播的核心运算。</p>
<div class="math-block">
<div class="math-content">
\[ C = A \times B, \quad c_{ij} = \sum_{k=1}^{K} a_{ik} \cdot b_{kj} \]
</div>
<div class="math-description">A(m x K) 乘以 B(K x n) 得到 C(m x n)</div>
</div>
<p><strong>数值示例:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \times \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 1 \times 5 + 2 \times 7 & 1 \times 6 + 2 \times 8 \\ 3 \times 5 + 4 \times 7 & 3 \times 6 + 4 \times 8 \end{bmatrix} = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix} \]
</div>
<div class="math-description">逐步计算矩阵乘法</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>神经网络层的计算 \( \mathbf{h} = \sigma(W\mathbf{x} + \mathbf{b}) \),其中 \( W \) 是权重矩阵,\( \mathbf{x} \) 是输入向量,\( \mathbf{b} \) 是偏置向量,\( \sigma \) 是激活函数。整个深度网络就是一系列矩阵乘法的组合。
</div>
</div>
</div>
<!-- 微积分 -->
<h3 class="ai-section-subtitle">微积分</h3>
<p>微积分是理解AI优化过程的关键。神经网络的训练本质上就是通过微积分中的梯度下降来最小化损失函数。</p>
<div class="concept-section">
<div class="concept-box">
<h4><i class="fas fa-chart-line"></i> 导数 (Derivative)</h4>
<p>导数描述函数在某一点处的变化率,即"瞬时速度"。</p>
<div class="math-block">
<div class="math-content">
\[ f'(x) = \frac{df}{dx} = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} \]
</div>
<div class="math-description">导数的定义</div>
</div>
<p><strong>常用导数公式:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \frac{d}{dx}x^n = nx^{n-1}, \quad \frac{d}{dx}e^x = e^x, \quad \frac{d}{dx}\ln(x) = \frac{1}{x}, \quad \frac{d}{dx}\sin(x) = \cos(x) \]
</div>
<div class="math-description">基本函数的导数</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>导数告诉我们损失函数在某个参数值处的变化方向,是梯度下降法的基础。激活函数的导数决定了反向传播中梯度的流动。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-project-diagram"></i> 偏导数 (Partial Derivative)</h4>
<p>当函数有多个变量时,偏导数是固定其他变量,对某一个变量求导。</p>
<div class="math-block">
<div class="math-content">
\[ f(x, y) = x^2 + 3xy + y^2 \]
\[ \frac{\partial f}{\partial x} = 2x + 3y, \quad \frac{\partial f}{\partial y} = 3x + 2y \]
</div>
<div class="math-description">多变量函数的偏导数</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>损失函数通常有大量参数(可能数百万个),我们需要分别对每个参数求偏导数,以确定每个参数应该如何调整来减小损失。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-link"></i> 链式法则 (Chain Rule)</h4>
<p>链式法则是微积分中最重要的法则之一,用于求复合函数的导数。</p>
<div class="math-block">
<div class="math-content">
\[ \frac{d}{dx}[f(g(x))] = f'(g(x)) \cdot g'(x) \]
</div>
<div class="math-description">链式法则基本形式</div>
</div>
<p><strong>多变量链式法则:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \frac{\partial L}{\partial w} = \frac{\partial L}{\partial a} \cdot \frac{\partial a}{\partial z} \cdot \frac{\partial z}{\partial w} \]
</div>
<div class="math-description">多层复合函数的链式求导</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong><strong>反向传播算法(Backpropagation)的核心就是链式法则。</strong>神经网络由多层组成,损失函数对第一层参数的梯度需要逐层传递,每一层都通过链式法则将梯度向后传递。这是深度学习能够高效训练的关键。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-mountain"></i> 梯度 (Gradient)</h4>
<p>梯度是一个向量,由函数对所有变量的偏导数组成,指向函数值增长最快的方向。</p>
<div class="math-block">
<div class="math-content">
\[ \nabla f = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \\ \vdots \\ \frac{\partial f}{\partial x_n} \end{bmatrix} \]
</div>
<div class="math-description">梯度向量(n个变量的函数)</div>
</div>
<p><strong>梯度下降更新公式:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \theta_{t+1} = \theta_t - \alpha \cdot \nabla_\theta L(\theta_t) \]
</div>
<div class="math-description">梯度下降:沿梯度的反方向更新参数,\(\alpha\) 为学习率</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong><strong>梯度下降是几乎所有机器学习模型的训练方法。</strong>SGD、Adam、RMSprop等优化器都是基于梯度下降的变体。理解梯度是理解模型训练过程的关键。
</div>
</div>
</div>
<!-- 概率统计 -->
<h3 class="ai-section-subtitle">概率统计</h3>
<p>概率论为AI提供了处理不确定性的数学框架。从朴素贝叶斯分类器到贝叶斯神经网络,概率统计无处不在。</p>
<div class="concept-section">
<div class="concept-box">
<h4><i class="fas fa-chart-bar"></i> 概率分布 (Probability Distribution)</h4>
<p>概率分布描述了随机变量取各个值的可能性。</p>
<p><strong>常见分布:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \text{正态分布: } f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}} \]
</div>
<div class="math-description">高斯分布,\(\mu\)为均值,\(\sigma\)为标准差</div>
</div>
<div class="math-block">
<div class="math-content">
\[ \text{伯努利分布: } P(X=k) = p^k(1-p)^{1-k}, \quad k \in \{0, 1\} \]
</div>
<div class="math-description">二值分布,如抛硬币</div>
</div>
<div class="math-block">
<div class="math-content">
\[ \text{Softmax分布: } P(y_i) = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}} \]
</div>
<div class="math-description">多分类中常用的概率分布</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>正态分布用于权重初始化和数据假设;Softmax将网络输出转化为概率分布,用于多分类任务(如ImageNet的1000类分类)。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-balance-scale"></i> 期望与方差 (Expectation & Variance)</h4>
<p>期望是随机变量的"平均值",方差衡量数据的离散程度。</p>
<div class="math-block">
<div class="math-content">
\[ \mathbb{E}[X] = \sum_{i} x_i \cdot P(x_i) \quad \text{(离散)} \]
\[ \mathbb{E}[X] = \int_{-\infty}^{\infty} x \cdot f(x) \, dx \quad \text{(连续)} \]
</div>
<div class="math-description">期望(数学期望)</div>
</div>
<div class="math-block">
<div class="math-content">
\[ \text{Var}(X) = \mathbb{E}[(X - \mathbb{E}[X])^2] = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 \]
</div>
<div class="math-description">方差:衡量随机变量偏离均值的程度</div>
</div>
<p><strong>数值示例:</strong>掷骰子的期望和方差:</p>
<div class="math-block">
<div class="math-content">
\[ \mathbb{E}[X] = \frac{1+2+3+4+5+6}{6} = 3.5 \]
\[ \text{Var}(X) = \frac{(1-3.5)^2 + (2-3.5)^2 + \cdots + (6-3.5)^2}{6} \approx 2.92 \]
</div>
<div class="math-description">公平骰子的期望和方差</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>期望用于计算预测值;方差用于衡量模型的不确定性;Batch Normalization利用批次的均值和方差来稳定训练。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-random"></i> 贝叶斯定理 (Bayes' Theorem)</h4>
<p>贝叶斯定理描述了在获得新证据后如何更新我们的信念。</p>
<div class="math-block">
<div class="math-content">
\[ P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)} \]
</div>
<div class="math-description">贝叶斯定理:后验概率 = 似然 x 先验概率 / 证据</div>
</div>
<p><strong>数值示例:</strong>疾病检测</p>
<div class="math-block">
<div class="math-content">
\[ \text{假设:疾病发病率} P(D) = 0.01, \text{检测灵敏度} P(+|D) = 0.99, \text{误报率} P(+|\neg D) = 0.05 \]
\[ P(D|+) = \frac{P(+|D) \cdot P(D)}{P(+|D) \cdot P(D) + P(+|\neg D) \cdot P(\neg D)} = \frac{0.99 \times 0.01}{0.99 \times 0.01 + 0.05 \times 0.99} \approx 0.167 \]
</div>
<div class="math-description">即使检测为阳性,实际患病的概率也仅有约16.7%(贝叶斯定理的经典应用)</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>朴素贝叶斯分类器(垃圾邮件过滤);贝叶斯优化(超参数调优);贝叶斯神经网络(量化模型不确定性);最大后验估计(MAP)。
</div>
</div>
</div>
<!-- 信息论 -->
<h3 class="ai-section-subtitle">信息论</h3>
<p>信息论为AI提供了衡量"信息量"和"不确定性"的工具,是理解损失函数设计的基础。</p>
<div class="concept-section">
<div class="concept-box">
<h4><i class="fas fa-question-circle"></i> 熵 (Entropy)</h4>
<p>熵衡量一个随机变量的不确定性,熵越大表示不确定性越高。</p>
<div class="math-block">
<div class="math-content">
\[ H(X) = -\sum_{i=1}^{n} p(x_i) \log_2 p(x_i) \]
</div>
<div class="math-description">信息熵(单位:比特)</div>
</div>
<p><strong>直观理解:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \text{公平硬币: } H = -(0.5 \log_2 0.5 + 0.5 \log_2 0.5) = 1 \text{ bit} \]
\[ \text{作弊硬币(总是正面): } H = -(1 \log_2 1 + 0 \log_2 0) = 0 \text{ bit} \]
</div>
<div class="math-description">越确定的事物,熵越低;越不确定的事物,熵越高</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong>决策树使用信息增益(基于熵)来选择最优分裂特征;信息熵用于衡量数据集的纯度。
</div>
</div>
<div class="concept-box">
<h4><i class="fas fa-exchange-alt"></i> 交叉熵 (Cross-Entropy)</h4>
<p>交叉熵衡量两个概率分布之间的"距离",是分类任务中最常用的损失函数。</p>
<div class="math-block">
<div class="math-content">
\[ H(p, q) = -\sum_{i=1}^{n} p(x_i) \log q(x_i) \]
</div>
<div class="math-description">交叉熵:p为真实分布,q为预测分布</div>
</div>
<p><strong>二分类交叉熵损失:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \mathcal{L} = -\frac{1}{N}\sum_{i=1}^{N} \left[ y_i \log \hat{y}_i + (1 - y_i) \log(1 - \hat{y}_i) \right] \]
</div>
<div class="math-description">Binary Cross-Entropy Loss,\(\hat{y}\)为预测概率,\(y\)为真实标签</div>
</div>
<p><strong>多分类交叉熵损失:</strong></p>
<div class="math-block">
<div class="math-content">
\[ \mathcal{L} = -\sum_{i=1}^{C} y_i \log \hat{y}_i \]
</div>
<div class="math-description">Categorical Cross-Entropy Loss,C为类别数</div>
</div>
<div class="ai-usage">
<strong><i class="fas fa-robot"></i> AI中哪里用到:</strong><strong>交叉熵是深度学习中最常用的损失函数。</strong>几乎所有的分类任务(图像分类、文本分类、机器翻译等)都使用交叉熵作为损失函数。它衡量的是模型预测分布与真实分布之间的差异,模型训练的目标就是最小化这个差异。
</div>
</div>
</div>
<!-- 数学概念汇总表 -->
<h3 class="ai-section-subtitle">数学概念与AI应用对照表</h3>
<table class="comparison-table">
<thead>
<tr>
<th><i class="fas fa-book"></i> 数学概念</th>
<th><i class="fas fa-cog"></i> AI应用</th>
<th><i class="fas fa-layer-group"></i> 涉及章节</th>
</tr>
</thead>
<tbody>
<tr>
<td>向量/矩阵</td>
<td>数据表示、权重矩阵、特征提取</td>
<td>全部章节</td>
</tr>
<tr>
<td>矩阵乘法</td>
<td>神经网络前向传播</td>
<td>深度学习</td>
</tr>
<tr>
<td>导数/梯度</td>
<td>梯度下降、反向传播</td>
<td>机器学习、深度学习</td>
</tr>
<tr>
<td>链式法则</td>
<td>反向传播算法</td>
<td>深度学习</td>
</tr>
<tr>
<td>概率分布</td>
<td>Softmax输出、数据建模</td>
<td>机器学习、深度学习</td>
</tr>
<tr>
<td>贝叶斯定理</td>
<td>朴素贝叶斯、贝叶斯优化</td>
<td>机器学习</td>
</tr>
<tr>
<td>交叉熵</td>
<td>分类损失函数</td>
<td>机器学习、深度学习</td>
</tr>
</tbody>
</table>
<!-- 练习题 -->
<div class="ai-box ai-box--exercise">
<h4><i class="fas fa-pen-fancy"></i> 练习题</h4>
<div class="exercise-item">
<span class="exercise-number">[1]</span>
给定矩阵 \( A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \) 和向量 \( \mathbf{x} = \begin{bmatrix} 5 \\ 6 \end{bmatrix} \),手动计算 \( A\mathbf{x} \),然后用NumPy验证结果。
</div>
<div class="exercise-item">
<span class="exercise-number">[2]</span>
对函数 \( f(x, y) = x^2 y + y^3 \),分别求 \( \frac{\partial f}{\partial x} \) 和 \( \frac{\partial f}{\partial y} \),并计算在点 \( (2, 1) \) 处的梯度 \( \nabla f \)。
</div>
<div class="exercise-item">
<span class="exercise-number">[3]</span>
一个二分类问题中,真实标签为 \( y = 1 \),模型预测概率为 \( \hat{y} = 0.8 \)。计算该样本的交叉熵损失 \( \mathcal{L} = -[y \log \hat{y} + (1-y)\log(1-\hat{y})] \)。如果预测概率提高到 \( \hat{y} = 0.95 \),损失如何变化?
</div>
</div>
<!-- 代码实践 -->
<div class="concept-section">
<h3 class="ai-section-subtitle"><i class="fas fa-code"></i> 代码实践</h3>
<p>以下代码示例展示了AI数学基础中常用的<strong>NumPy矩阵运算</strong>和<strong>matplotlib函数可视化</strong>。这些操作在深度学习框架中随处可见。</p>
<h4>示例1:NumPy矩阵运算</h4>
<pre><code class="language-python">
import numpy as np
# 创建矩阵
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
# 矩阵加法
C = A + B
print("A + B =")
print(C)
# 矩阵乘法(点积)
D = np.dot(A, B)
print("\nA · B =")
print(D)
# 矩阵转置
print("\nA的转置:")
print(A.T)
# 矩阵求逆
A_inv = np.linalg.inv(A)
print("\nA的逆矩阵:")
print(A_inv)
# 验证:A · A_inv = 单位矩阵
print("\n验证 A · A_inv:")
print(np.dot(A, A_inv))
</code></pre>
<div class="concept-box">
<h4><i class="fas fa-lightbulb"></i> 运行结果</h4>
<pre><code>
A + B =
[[ 6 8]
[10 12]]
A · B =
[[19 22]
[43 50]]
A的转置:
[[1 3]
[2 4]]
A的逆矩阵:
[[-2. 1. ]
[ 1.5 -0.5]]
验证 A · A_inv:
[[1. 0.]
[0. 1.]]
</code></pre>
</div>
<h4>示例2:绘制激活函数图像</h4>
<pre><code class="language-python">
import numpy as np
import matplotlib.pyplot as plt
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
# 生成数据
x = np.linspace(-5, 5, 100)
# Sigmoid 函数
sigmoid = 1 / (1 + np.exp(-x))
# ReLU 函数
relu = np.maximum(0, x)
# Tanh 函数
tanh = np.tanh(x)
# 绘制图像
plt.figure(figsize=(10, 6))
plt.plot(x, sigmoid, label='Sigmoid', linewidth=2)
plt.plot(x, relu, label='ReLU', linewidth=2)
plt.plot(x, tanh, label='Tanh', linewidth=2)
plt.axhline(y=0, color='k', linestyle='-', linewidth=0.5)
plt.axvline(x=0, color='k', linestyle='-', linewidth=0.5)
plt.grid(True, alpha=0.3)
plt.legend(fontsize=12)
plt.title('常见激活函数', fontsize=14)
plt.xlabel('x', fontsize=12)
plt.ylabel('f(x)', fontsize=12)
plt.tight_layout()
plt.show()
</code></pre>
<div class="concept-box">
<h4><i class="fas fa-lightbulb"></i> 运行结果</h4>
<p>运行后会弹出一个窗口,显示三条曲线:</p>
<ul>
<li><strong>Sigmoid</strong>:S形曲线,输出范围 (0, 1)</li>
<li><strong>ReLU</strong>:x<0时为0,x>0时为直线</li>
<li><strong>Tanh</strong>:S形曲线,输出范围 (-1, 1)</li>
</ul>
<p>这些激活函数是神经网络的核心组件,理解它们的图像有助于理解模型行为。</p>
</div>
</div>
<!-- 章节小结 -->
<div class="ai-box ai-box--summary">
<h4><i class="fas fa-lightbulb"></i> 章节小结</h4>
<ul>
<li>线性代数是AI计算的基础,矩阵乘法是神经网络的核心运算</li>
<li>微积分中的梯度和链式法则是反向传播算法的数学基础</li>
<li>概率论为处理不确定性提供了框架,贝叶斯定理在多个AI领域有重要应用</li>
<li>交叉熵是最常用的分类损失函数,理解其含义对模型训练至关重要</li>
<li>不必一开始就精通所有数学,随着后续章节的学习,这些概念会越来越清晰</li>
</ul>
</div>
</section>
</main>
<!-- ========== 右侧粘性目录导航 ========== -->
<aside class="toc-sidebar">
<h3><i class="fas fa-list"></i> 目录导航</h3>
<ul>
<li>
<a href="#chapter0">
<i class="fas fa-map-signs"></i> 第零章:学习路线图
</a>
</li>
<li class="toc-divider"></li>
<li>
<a href="#chapter1">
<i class="fab fa-python"></i> 第一章:Python编程基础
</a>
<ul class="toc-sub">
<li><a href="#python-env">环境安装</a></li>
<li><a href="#python-syntax">基础语法</a></li>
<li><a href="#python-numpy">NumPy基础</a></li>
<li><a href="#python-matplotlib">Matplotlib基础</a></li>
</ul>
</li>
<li class="toc-divider"></li>
<li>
<a href="#chapter2">
<i class="fas fa-square-root-variable"></i> 第二章:数学基础
</a>
<ul class="toc-sub">
<li><a href="#math-linear">线性代数</a></li>
<li><a href="#math-calculus">微积分</a></li>
<li><a href="#math-probability">概率统计</a></li>
<li><a href="#math-information">信息论</a></li>
</ul>
</li>
<li class="toc-divider"></li>
<li>
<a href="#chapter3">
<i class="fas fa-robot"></i> 第三章:机器学习基础
</a>
</li>
<li>
<a href="#chapter4">
<i class="fas fa-network-wired"></i> 第四章:深度学习基础
</a>
</li>
<li>
<a href="#chapter5">
<i class="fas fa-tools"></i> 第五章:工程实践与工具
</a>
</li>
<li>
<a href="#chapter6">
<i class="fas fa-rocket"></i> 第六章:进阶展望
</a>
</li>
</ul>
</aside>
</div><!-- /.content-wrapper -->
<!-- 导航栏切换脚本 -->
<script>
// 移动端导航栏切换
document.getElementById('navToggle').addEventListener('click', function() {
document.getElementById('navLinks').classList.toggle('active');
});
// 代码复制功能
function copyCode(button) {
var codeBlock = button.closest('.code-block');
var code = codeBlock.querySelector('code').textContent;
navigator.clipboard.writeText(code).then(function() {
var originalHTML = button.innerHTML;
button.innerHTML = '<i class="fas fa-check"></i> 已复制';
button.style.color = '#48bb78';
setTimeout(function() {
button.innerHTML = originalHTML;
button.style.color = '';
}, 2000);
});
}
// 数学公式块展开/收起
function toggleMath(button) {
var content = button.parentElement.querySelector('.math-content');
var description = button.parentElement.querySelector('.math-description');
var icon = button.querySelector('i');
if (content.style.display === 'none') {
content.style.display = '';
if (description) description.style.display = '';
icon.className = 'fas fa-chevron-up';
} else {
content.style.display = 'none';
if (description) description.style.display = 'none';
icon.className = 'fas fa-chevron-down';
}
}
// 目录导航高亮当前章节
(function() {
var sections = document.querySelectorAll('section[id]');
var tocLinks = document.querySelectorAll('.toc-sidebar a');
function updateActiveLink() {
var scrollPos = window.scrollY + 100;
sections.forEach(function(section) {
var top = section.offsetTop;
var height = section.offsetHeight;
var id = section.getAttribute('id');
var link = document.querySelector('.toc-sidebar a[href="#' + id + '"]');
if (link) {
if (scrollPos >= top && scrollPos < top + height) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
}
});
}
window.addEventListener('scroll', updateActiveLink);
updateActiveLink();
})();
</script>
</main>
<!-- 底部导航 -->
<div class="chapter-nav bottom">
<a href="ch01.html" class="prev-chapter"><i class="fas fa-arrow-left"></i> 上一章</a>
<a href="index.html" class="toc-link"><i class="fas fa-list"></i> 目录</a>
<a href="ch02b.html" class="next-chapter">下一章 <i class="fas fa-arrow-right"></i></a>
</div>
<footer class="footer">
<div class="footer-content">
<div class="footer-section">
<h3><i class="fas fa-graduation-cap"></i> AI学习笔记</h3>
<p>从零开始系统学习人工智能</p>
<p>涵盖前置基础、核心原理、工程实践、应用工具、进阶展望</p>
</div>
<div class="footer-section">
<h3><i class="fas fa-bookmark"></i> 内容导航</h3>
<p>第1-11章:基础与核心(见 ai-notes-core.html)</p>
<p>第12-23章:工程实践与进阶(本文件)</p>
<p>附录A:GPU与算力指南</p>
<p>附录B:术语表</p>
</div>
<div class="footer-section">
<h3><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>
</div><!-- /ai-notes-container -->
<script src="../../js/mobile-menu.js"></script>
<script src="../../js/dark-mode.js"></script>
<script src="assets/ai-notes.js"></script>
</body>
</html>