Skip to content

Commit dee6760

Browse files
📝 更新文档,优化机器学习示例和增加损失函数拓展
- 在机器学习文档中删除了冗余的注释,简化了平方根求解的示例代码,提升了可读性。 - 新增损失函数拓展部分,详细介绍了多种损失函数的数学形式及适用场景,增强了文档的实用性和完整性。
1 parent b98717f commit dee6760

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ yarn-debug.log*
2222
yarn-error.log*
2323

2424
# draft
25-
draft
25+
draft

docs/docs/机器学习/index.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,16 @@ title: 🚧机器学习
1010
具体的内容我们可以尝试编写一个求某个数的平方根的例子:
1111

1212
```python showLineNumbers
13-
14-
import numpy as np
15-
import matplotlib.pyplot as plt
16-
1713
# 我们要求解的数
1814
target_number = 17.0
1915

2016
# 初始化权重(我们的猜测值)
2117
weight = 1
22-
print(f"初始随机猜测值: {weight}")
2318

2419
# 超参数
2520
learning_rate = 0.01 # 学习率
2621
epochs = 1000 # 训练轮数
2722

28-
# 存储训练过程
29-
weights_history = []
30-
loss_history = []
31-
3223
# 训练过程
3324
for epoch in range(epochs):
3425
# 前向传播: 计算预测值 (weight * weight 应该等于 target_number)
@@ -42,14 +33,6 @@ for epoch in range(epochs):
4233

4334
# 更新权重 (梯度下降)
4435
weight = weight - learning_rate * gradient
45-
46-
# 记录历史
47-
weights_history.append(weight)
48-
loss_history.append(loss)
49-
50-
# 每隔100轮打印一次结果
51-
if epoch % 100 == 0 or epoch == epochs - 1:
52-
print(f"轮次 {epoch}, 当前估计值: {weight}, 预测值的平方: {prediction}, 损失: {loss}")
5336

5437
# 最终结果
5538
print(f"\n训练后的平方根估计值: {weight}")

docs/docs/机器学习/传统算法/线性回归.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,14 @@ print(model.score(data_X, data_y))
182182
print("回归系数 (斜率):", model.coef_)
183183
print("截距:", model.intercept_)
184184
```
185+
186+
## 损失函数拓展
187+
188+
| 损失函数名称 | 适用场景 | 数学形式 | 特点 |
189+
|----------------------|------------------|-----------------------------------------------------------------------|--------------------------------------------------------------|
190+
| 交叉熵损失(Cross Entropy) | 多分类,常配合Softmax | $L = -\sum_{i} y_i \log(\hat{y}_i)$ | 最常用,适合 one-hot 标签,梯度清晰,收敛快 |
191+
| MSE(均方误差) | 少见于分类任务 | $L = \frac{1}{n} \sum_{i} (y_i - \hat{y}_i)^2$ | 简单直观,但不适合分类,会导致梯度问题 |
192+
| KL散度(KL Divergence) | 预测分布 vs 真分布 | $L = \sum_i y_i \log\left(\frac{y_i}{\hat{y}_i}\right)$ | 衡量分布差异,常用于模型输出概率分布 |
193+
| Focal Loss | 类别不均衡时 | $L = -\alpha(1 - \hat{y}_t)^\gamma \log(\hat{y}_t)$ | 抑制易分类样本的损失,对困难样本关注更多 |
194+
| Hinge Loss(合页损失)| SVM等场景 | $L = \sum_i \max(0, 1 - y_i \cdot \hat{y}_i)$ | 主要用于“硬间隔”分类问题,不常用于深度学习中的多分类任务 |
195+

docs/docs/机器学习/传统算法/逻辑回归.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ $y = f(β0 + β1x1 + β2x2+… βnxn)$
3131

3232
逻辑回归分类器更接近 KNN,要解决多分类问题时,常常需要针对不同类别分别建立多个模型。
3333

34+
3435
```python showLineNumbers
3536
import numpy as np
3637
from matplotlib import pyplot as plt
@@ -259,3 +260,15 @@ plt.show()
259260

260261

261262
```
263+
264+
## 激活函数拓展
265+
266+
以下是 Sigmoid、ReLU、Softmax、Tanh 的多维对比表,包含定义、值域、优缺点、导数等内容:
267+
268+
| 名称 | 数学表达式 | 值域 | 导数表达式 | 优点 | 缺点 |
269+
|----------|--------------------------------|--------------|--------------------------------------------------|--------------------------------------------|------------------------------------------------|
270+
| Sigmoid | $\sigma(x) = \frac{1}{1 + e^{-x}}$ | $(0, 1)$ | $\sigma'(x) = \sigma(x)(1 - \sigma(x))$ | 平滑,有概率解释 | 梯度消失、输出非0均值 |
271+
| ReLU | $\text{ReLU}(x) = \max(0, x)$ | $[0, +\infty)$ | $\text{ReLU}'(x) = \begin{cases} 1, & x > 0 \\ 0, & x \le 0 \end{cases}$ | 计算简单,收敛快 | 不可导于0,死神经元问题 |
272+
| Tanh | $\tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$ | $(-1, 1)$ | $\tanh'(x) = 1 - \tanh^2(x)$ | 平滑,输出均值为0 | 梯度消失 |
273+
| Softmax | $\text{softmax}(x_i) = \frac{e^{x_i}}{\sum_j e^{x_j}}$ | $(0,1)$ 且 $\sum=1$ | $\frac{\partial y_i}{\partial x_j} = y_i(\delta_{ij} - y_j)$ | 多分类概率输出,归一化 | 对大值敏感,数值不稳定 |
274+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
sidebar_position: 0
3+
---
4+
5+
想象有一位驾驶着小电动车的冒险家,车上装着一根透明的神奇长管,与电动车行驶方向完美一致。这根长管的末端象征着正无穷,正中央恰是零点,而前端则代表着负无穷。长管内,一颗小球安静地待着。
6+
7+
当电动车在平坦大道上飞驰时,长管内的数值宛如静止的湖面,始终定格在零,小球也稳稳地待在正中间。
8+
9+
可当电动车挑战一座小山,开始爬坡之旅时,坡度之陡让小球仿佛被一股无形的力量推向正无穷的深处,随着上坡的进程,它缓缓但坚定地向前滑动。而当电动车驶下坡时,小球又如离弦之箭般向负无穷的方向奔去。
10+
11+
倘若电动车不幸遭遇悬崖,直接坠落,这便是函数的不连续之处,也是不可导的惊险瞬间。
12+
13+
现在,让我们大胆设想,如果电动车行驶在一条连绵起伏、波浪般的道路上,那么小球画出的线条,岂不是也如同波浪一般?是的,sin 的导数优雅地变身为 cos,而 cos 的导数则奇妙地化作 sin。
14+
15+

0 commit comments

Comments
 (0)