Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
de6b6ef1
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
大约 2 年 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
de6b6ef1
编写于
12月 31, 2019
作者:
B
baiyfbupt
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix some details in distillation demo
上级
7bc5e3c1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
10 addition
and
7 deletion
+10
-7
docs/docs/tutorials/distillation_demo.md
docs/docs/tutorials/distillation_demo.md
+10
-7
未找到文件。
docs/docs/tutorials/distillation_demo.md
浏览文件 @
de6b6ef1
本示例将介绍如何使用PaddleSlim蒸馏接口来对模型进行蒸馏训练
本示例将介绍如何使用PaddleSlim蒸馏接口来对模型进行蒸馏训练
。
## 接口介绍
## 接口介绍
...
@@ -28,9 +28,9 @@ with fluid.program_guard(student_program, student_startup):
...
@@ -28,9 +28,9 @@ with fluid.program_guard(student_program, student_startup):
### 2. 定义teacher_program
### 2. 定义teacher_program
在定义好
teacher_program后,可以一并加载训练好的pretrained_model
在定义好
`teacher_program`
后,可以一并加载训练好的pretrained_model。
在
teacher_program内需要加上
`with fluid.unique_name.guard():`
,保证teacher的变量命名不被student_program影响,从而跟能够正确地加载预训练参数
在
`teacher_program`
内需要加上
`with fluid.unique_name.guard():`
,保证teacher的变量命名不被
`student_program`
影响,从而能够正确地加载预训练参数。
```
python
```
python
teacher_program
=
fluid
.
Program
()
teacher_program
=
fluid
.
Program
()
...
@@ -55,7 +55,7 @@ fluid.io.load_vars(
...
@@ -55,7 +55,7 @@ fluid.io.load_vars(
### 3.选择特征图
### 3.选择特征图
定义好
student_program和teacher_program后,我们需要从中两两对应地挑选出若干个特征图,留待后续为其添加知识蒸馏损失函数
定义好
`student_program`
和
`teacher_program`
后,我们需要从中两两对应地挑选出若干个特征图,留待后续为其添加知识蒸馏损失函数。
```
python
```
python
# get all student variables
# get all student variables
...
@@ -91,12 +91,15 @@ student_program = merge(teacher_program, student_program, data_name_map, place)
...
@@ -91,12 +91,15 @@ student_program = merge(teacher_program, student_program, data_name_map, place)
### 5.添加蒸馏loss
### 5.添加蒸馏loss
在添加蒸馏loss的过程中,可能还会引入部分变量(Variable),为了避免命名重复这里可以使用
`with fluid.name_scope("distill"):`
为新引入的变量加一个命名作用域
在添加蒸馏loss的过程中,可能还会引入部分变量(Variable),为了避免命名重复这里可以使用
`with fluid.name_scope("distill"):`
为新引入的变量加一个命名作用域。
另外需要注意的是,merge过程为
`teacher_program`
的变量统一加了名称前缀,默认是
`"teacher_"`
, 这里在添加
`l2_loss`
时也要为teacher的变量加上这个前缀。
```
python
```
python
with
fluid
.
program_guard
(
student_program
,
student_startup
):
with
fluid
.
program_guard
(
student_program
,
student_startup
):
with
fluid
.
name_scope
(
"distill"
):
with
fluid
.
name_scope
(
"distill"
):
distill_loss
=
l2_loss
(
'teacher_bn5c_branch2b.output.1.tmp_3'
,
'depthwise_conv2d_11.tmp_0'
,
main
)
distill_loss
=
l2_loss
(
'teacher_bn5c_branch2b.output.1.tmp_3'
,
'depthwise_conv2d_11.tmp_0'
,
student_program
)
distill_weight
=
1
distill_weight
=
1
loss
=
avg_cost
+
distill_loss
*
distill_weight
loss
=
avg_cost
+
distill_loss
*
distill_weight
opt
=
create_optimizer
()
opt
=
create_optimizer
()
...
@@ -104,4 +107,4 @@ with fluid.program_guard(student_program, student_startup):
...
@@ -104,4 +107,4 @@ with fluid.program_guard(student_program, student_startup):
exe
.
run
(
student_startup
)
exe
.
run
(
student_startup
)
```
```
至此,我们就得到了用于蒸馏训练的
student_program,后面就可以使用一个普通program一样对其开始训练和评估
至此,我们就得到了用于蒸馏训练的
`student_program`
,后面就可以使用一个普通program一样对其开始训练和评估。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录