Skip to content

Commit b54f632

Browse files
authored
[PUBLISHER] Merge #29
* PUSH NOTE : 模型蒸馏简单上手.md * PUSH NOTE : 小土堆pytorch教程.md
1 parent 1ac285f commit b54f632

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

docs/ML/小土堆pytorch教程.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ completed?: true
88
keyword_for_dataview: ""
99
share: true
1010
category: docs/ML
11-
modify: 2025-04-05 20:20
11+
modify: 2025-04-06 15:55
1212
---
1313

1414
# Python 的法宝函数
@@ -121,9 +121,11 @@ writer.close()
121121
关注函数描述的输入和输出(见官方文档),注意函数的参数
122122
1. `ToTensor()` 把数据类型转换成 Tensor
123123
2. `Normalize()` 会对输入张量的**每个通道**执行以下计算,它的核心作用是通过对每个通道的像素值进行**线性变换**,将数据调整到特定的均值和标准差范围
124+
124125
$$
125126
\text{output\_channel} = \frac{\text{input\_channel} - \text{mean}}{\text{std}}
126127
$$
128+
127129
3. `Resize()` 将输入图像(PIL Image 或 Tensor)的尺寸调整为指定大小,​**强制统一所有输入图像的尺寸**,以满足深度学习模型对固定输入尺寸的要求
128130
4. `Compose`**的作用是将多个图像变换(`transform`)按顺序组合成一个整体**。是PyTorch中构建图像预处理流水线的核心工具,通过将多个变换按顺序组合,确保数据在输入模型前经过规范化的处理。多个变换的顺序很重要,前一个的输出作为下一个的输入。
129131

@@ -152,7 +154,11 @@ $$
152154

153155

154156
输入特征图 $X \in \mathbb{R}^{H×W×C}$,卷积核 $W \in \mathbb{R}^{k×k×C×N}$:
155-
$$Z_{i,j,n} = \sum_{c=1}^C \sum_{u=-k}^{k} \sum_{v=-k}^{k} W_{u,v,c,n} \cdot X_{i+u,j+v,c} + b_n$$
157+
158+
$$
159+
Z_{i,j,n} = \sum_{c=1}^C \sum_{u=-k}^{k} \sum_{v=-k}^{k} W_{u,v,c,n} \cdot X_{i+u,j+v,c} + b_n
160+
$$
161+
156162
* 在 pytorch 中的尝试
157163
```python
158164
import torch
@@ -197,7 +203,11 @@ tensor([[[[ 1, 3, 4, 10, 8],
197203
通过反向传播自动学习滤波器参数,网络能够自适应地发现对任务最有判别力的特征组合。
198204
**参数学习机制**
199205
虽然未执行核翻转,但通过梯度下降算法:
200-
$$\frac{\partial L}{\partial K} = \frac{\partial L}{\partial (f \star K)} \ast f$$
206+
207+
$$
208+
\frac{\partial L}{\partial K} = \frac{\partial L}{\partial (f \star K)} \ast f
209+
$$
210+
201211
网络会自动学习到与数学卷积等效的旋转滤波器参数。
202212
## 通道是什么?
203213
通道(Channel)是深度学习中对数据特征的维度抽象,在不同领域有不同表现形式:
@@ -237,10 +247,15 @@ $$\frac{\partial L}{\partial K} = \frac{\partial L}{\partial (f \star K)} \ast f
237247
池化过程在一般卷积过程后。池化(pooling) 的本质,其实就是采样。Pooling 对于输入的 Feature Map,选择某种方式对其进行降维压缩,以加快运算速度。
238248
本质是特征提取,用来压缩或者降维。要损失一部分信息。
239249
* 池化层没有参数
250+
240251
以最大池化为例子,$3\times3$ 的池化核就是对于数据每 $3\times3$ 的数据取最大值,比如就可以把 $9\times9$ 压缩为 $3\times3$
241252
## 线性层
242253
给定输入向量 $\mathbf{x} \in \mathbb{R}^{d_{in}}$,权重矩阵 $W \in \mathbb{R}^{d_{out} \times d_{in}}$,偏置 $\mathbf{b} \in \mathbb{R}^{d_{out}}$:
243-
$$\mathbf{z} = W\mathbf{x} + \mathbf{b}$$
254+
255+
$$
256+
\mathbf{z} = W\mathbf{x} + \mathbf{b}
257+
$$
258+
244259

245260
| 特性 | 描述 | 典型应用场景 |
246261
|------|------|--------------|

docs/ML/模型蒸馏简单上手.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ completed?: true
88
keyword_for_dataview: ""
99
share: true
1010
category: docs/ML
11-
modify: 2025-04-05 20:19
11+
modify: 2025-04-06 15:56
1212
---
1313
# 数据和模型
1414
利用 `CIFAR-10` 数据集,用两个层次不一样的模型作为教师模型和学生模型
@@ -127,16 +127,20 @@ def train_knowledge_distillation(teacher, student, train_loader, epochs, learnin
127127
## T 对蒸馏的影响
128128
**软化**:通过增大 T ,减少 logits 的相对差异,使概率分布更均匀。
129129
利用 T 软化 softmax 的概率分布,让输出更倾向于分布而不是分类本身
130+
130131
$$
131132
p(i) = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}
132133
$$
134+
133135
从而区别于数据集给出的这个图象是什么,而是让教师模型告诉学生模型,这张图很像 a,但也有一点像 b
134136

135137
T 越大,分布越平滑,次要类别概率越高。
136138
## 蒸馏损失公式
139+
137140
$$
138141
\text{soft\_targets\_loss} = \frac{\sum \left( p_{teacher} \cdot (\log p_{teacher} - \log p_{student}) \right)}{\text{batch\_size}} \cdot T^2
139142
$$
143+
140144
* $T^2$ 补偿梯度减小,确保损失对优化的贡献稳定
141145
* 此处的蒸馏损失是 KL 散度的变体,用于衡量两个概率分布的差异
142146
## 蒸馏结果 1
@@ -149,18 +153,22 @@ Student accuracy with CE + KD: 71.40%
149153

150154
# CosineEmbeddingLoss
151155
* 余弦相似度
156+
152157
$$
153158
\text{cosine\_similarity}(x_1, x_2) = \cos(\theta) = \frac{x_1 \cdot x_2}{\|x_1\| \|x_2\|}
154159
$$
155160

161+
156162
* 损失定义
163+
157164
$$
158165
\text{loss}(x_1, x_2, y) =
159166
\begin{cases}
160167
1 - \cos(x_1, x_2), & \text{if } y = 1 \\
161168
\max(0, \cos(x_1, x_2) - \text{margin}), & \text{if } y = -1
162169
\end{cases}
163170
$$
171+
164172
1. 对于正样本 `y=1`
165173
1. 让两个向量尽肯能的对齐,如果完全对齐的话 $cos(x_1,x_2)$ 为 1,损失为 0
166174
2. 对于负样本 `y=-1`
@@ -308,8 +316,11 @@ def test_multiple_outputs(model, test_loader, device):
308316
- 卷积核在输入上滑动(卷积运算),每次计算局部区域的加权和,生成一个值。
309317
- 滑动完成后,形成一个二维特征图。
310318
- **公式**
311-
$$\text{Feature Map}_{i,j} = \sum_{m,n} \text{Input}_{i+m, j+n} \cdot \text{Kernel}_{m,n} + \text{Bias}
319+
312320
$$
321+
\text{Feature Map}_{i,j} = \sum_{m,n} \text{Input}_{i+m, j+n} \cdot \text{Kernel}_{m,n} + \text{Bias}
322+
$$
323+
313324
因此,一次卷积操作(用一个卷积核扫一遍图像),输出就是一张特征图,他代表神经网络在某一方面提取到的某个特征
314325
## 查看特征图的尺寸
315326
```text

0 commit comments

Comments
 (0)