Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
88f849b3
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
大约 1 年 前同步成功
通知
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看板
未验证
提交
88f849b3
编写于
2月 11, 2020
作者:
B
Bai Yifan
提交者:
GitHub
2月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Format KD english API (#102)
上级
4670a793
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
58 addition
and
32 deletion
+58
-32
paddleslim/dist/single_distiller.py
paddleslim/dist/single_distiller.py
+58
-32
未找到文件。
paddleslim/dist/single_distiller.py
浏览文件 @
88f849b3
...
...
@@ -20,21 +20,31 @@ def merge(teacher_program,
student_program
,
data_name_map
,
place
,
scope
=
fluid
.
global_scope
()
,
scope
=
None
,
name_prefix
=
'teacher_'
):
"""
Merge teacher program into student program and add a uniform prefix to the
"""Merge teacher program into student program and add a uniform prefix to the
names of all vars in teacher program
Args:
teacher_program(Program): The input teacher model paddle program
student_program(Program): The input student model paddle program
data_map_map(dict): Describe the mapping between the teacher var name
and the student var name
data_map_map(dict): Mapping of teacher input interface name and student
input interface name, where key of dict is the
input name of teacher_program, and value is the
input name of student_program.
place(fluid.CPUPlace()|fluid.CUDAPlace(N)): This parameter represents
paddle run on which device.
scope(Scope): The input scope
scope(Scope): This parameter indicates the variable scope used by
the program. If not specified, the default global scope
will be used. Default: None
name_prefix(str): Name prefix added for all vars of the teacher program.
Default: 'teacher_'
Returns:
None
"""
if
scope
==
None
:
scope
=
fluid
.
global_scope
()
teacher_program
=
teacher_program
.
clone
(
for_test
=
True
)
for
teacher_var
in
teacher_program
.
list_vars
():
skip_rename
=
False
...
...
@@ -89,9 +99,9 @@ def fsp_loss(teacher_var1_name,
teacher_var2_name
,
student_var1_name
,
student_var2_name
,
program
=
fluid
.
default_main_program
()
):
"""
Combine variables from student model and teacher model by fsp-loss.
program
=
None
):
"""
Combine variables from student model and teacher model by fsp-loss.
Args:
teacher_var1_name(str): The name of teacher_var1.
teacher_var2_name(str): The name of teacher_var2. Except for the
...
...
@@ -101,10 +111,14 @@ def fsp_loss(teacher_var1_name,
student_var2_name(str): The name of student_var2. Except for the
second dimension, all other dimensions should
be consistent with student_var1.
program(Program): The input distiller program.
default: fluid.default_main_program()
Return(Variable): fsp distiller loss.
program(Program): The input distiller program. If not specified,
the default program will be used. Default: None
Returns:
Variable: fsp distiller loss.
"""
if
program
==
None
:
program
=
fluid
.
default_main_program
()
teacher_var1
=
program
.
global_block
().
var
(
teacher_var1_name
)
teacher_var2
=
program
.
global_block
().
var
(
teacher_var2_name
)
student_var1
=
program
.
global_block
().
var
(
student_var1_name
)
...
...
@@ -118,16 +132,20 @@ def fsp_loss(teacher_var1_name,
def
l2_loss
(
teacher_var_name
,
student_var_name
,
program
=
fluid
.
default_main_program
()
):
"""
Combine variables from student model and teacher model by l2-loss.
program
=
None
):
"""
Combine variables from student model and teacher model by l2-loss.
Args:
teacher_var_name(str): The name of teacher_var.
student_var_name(str): The name of student_var.
program(Program): The input distiller program.
default: fluid.default_main_program()
Return(Variable): l2 distiller loss.
program(Program): The input distiller program. If not specified,
the default program will be used. Default: None
Returns:
Variable: l2 distiller loss.
"""
if
program
==
None
:
program
=
fluid
.
default_main_program
()
student_var
=
program
.
global_block
().
var
(
student_var_name
)
teacher_var
=
program
.
global_block
().
var
(
teacher_var_name
)
l2_loss
=
fluid
.
layers
.
reduce_mean
(
...
...
@@ -137,22 +155,26 @@ def l2_loss(teacher_var_name,
def
soft_label_loss
(
teacher_var_name
,
student_var_name
,
program
=
fluid
.
default_main_program
()
,
program
=
None
,
teacher_temperature
=
1.
,
student_temperature
=
1.
):
"""
Combine variables from student model and teacher model by soft-label-loss.
"""
Combine variables from student model and teacher model by soft-label-loss.
Args:
teacher_var_name(str): The name of teacher_var.
student_var_name(str): The name of student_var.
program(Program): The input distiller program.
default: fluid.default_main_program()
program(Program): The input distiller program.
If not specified,
the default program will be used. Default: None
teacher_temperature(float): Temperature used to divide
teacher_feature_map before softmax.
d
efault: 1.0
teacher_feature_map before softmax.
D
efault: 1.0
student_temperature(float): Temperature used to divide
student_feature_map before softmax. default: 1.0
Return(Variable): l2 distiller loss.
student_feature_map before softmax. Default: 1.0
Returns:
Variable: l2 distiller loss.
"""
if
program
==
None
:
program
=
fluid
.
default_main_program
()
student_var
=
program
.
global_block
().
var
(
student_var_name
)
teacher_var
=
program
.
global_block
().
var
(
teacher_var_name
)
student_var
=
fluid
.
layers
.
softmax
(
student_var
/
student_temperature
)
...
...
@@ -164,15 +186,19 @@ def soft_label_loss(teacher_var_name,
return
soft_label_loss
def
loss
(
loss_func
,
program
=
fluid
.
default_main_program
()
,
**
kwargs
):
"""
Combine variables from student model and teacher model by self defined loss.
def
loss
(
loss_func
,
program
=
None
,
**
kwargs
):
"""
Combine variables from student model and teacher model by self defined loss.
Args:
program(Program): The input distiller program.
default: fluid.default_main_program()
program(Program): The input distiller program.
If not specified,
the default program will be used. Default: None
loss_func(function): The user self defined loss function.
Return(Variable): self defined distiller loss.
Returns:
Variable: self defined distiller loss.
"""
if
program
==
None
:
program
=
fluid
.
default_main_program
()
func_parameters
=
{}
for
item
in
kwargs
.
items
():
if
isinstance
(
item
[
1
],
str
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录