Deeplearning知识蒸馏

merge

paddleslim.dist.merge(teacher_programstudent_programdata_name_mapplacescope=fluid.global_scope()name_prefix='teacher_')

merge将teacher_program融合到student_program中。在融合的program中,可以为其中合适的teacher特征图和student特征图添加蒸馏损失函数,从而达到用teacher模型的暗知识(Dark Knowledge)指导student模型学习的目的。

参数:

  • teacher_program (Program)-定义了teacher模型的 paddle program
  • student_program (Program)-定义了student模型的 paddle program
  • data_name_map (dict)-teacher输入接口名与student输入接口名的映射,其中dict的 key 为teacher的输入名,value 为student的输入名
  • place (fluid.CPUPlace()|fluid.CUDAPlace(N))-该参数表示程序运行在何种设备上,这里的N为GPU对应的ID
  • scope (Scope)-该参数表示程序使用的变量作用域,如果不指定将使用默认的全局作用域。默认值: fluid.global_scope()
  • name_prefix (str)-merge操作将统一为teacher的 Variables 添加的名称前缀name_prefix。默认值:’teacher_

返回: 无

注解

data_name_map 是 teacher_var namestudent_var name的映射 ,如果写反可能无法正确进行merge

使用示例:

import paddle.fluid as fluid

import paddleslim.dist as dist

student_program = fluid.Program()

with fluid.program_guard(student_program):

x = fluid.layers.data(name='x', shape=[1, 28, 28])

conv = fluid.layers.conv2d(x, 32, 1)

out = fluid.layers.conv2d(conv, 64, 3, padding=1)

teacher_program = fluid.Program()

with fluid.program_guard(teacher_program):

y = fluid.layers.data(name='y', shape=[1, 28, 28])

conv = fluid.layers.conv2d(y, 32, 1)

conv = fluid.layers.conv2d(conv, 32, 3, padding=1)

out = fluid.layers.conv2d(conv, 64, 3, padding=1)

data_name_map = {'y':'x'}

USE_GPU = False

place = fluid.CUDAPlace(0) if USE_GPU else fluid.CPUPlace()

dist.merge(teacher_program, student_program,

data_name_map, place)

fsp_loss

paddleslim.dist.fsp_loss(teacher_var1_nameteacher_var2_namestudent_var1_namestudent_var2_nameprogram=fluid.default_main_program())

fsp_loss为program内的teacher var和student var添加fsp loss,出自论文 A Gift from Knowledge Distillation: Fast Optimization, Network Minimization and Transfer Learning

参数:

  • teacher_var1_name (str): teacher_var1的名称. 对应的variable是一个形为`[batch_size, x_channel, height, width]`的4-D特征图Tensor,数据类型为float32或float64
  • teacher_var2_name (str): teacher_var2的名称. 对应的variable是一个形为`[batch_size, y_channel, height, width]`的4-D特征图Tensor,数据类型为float32或float64。只有y_channel可以与teacher_var1的x_channel不同,其他维度必须与teacher_var1相同
  • student_var1_name (str): student_var1的名称. 对应的variable需与teacher_var1尺寸保持一致,是一个形为`[batch_size, x_channel, height, width]`的4-D特征图Tensor,数据类型为float32或float64
  • student_var2_name (str): student_var2的名称. 对应的variable需与teacher_var2尺寸保持一致,是一个形为`[batch_size, y_channel, height, width]`的4-D特征图Tensor,数据类型为float32或float64。只有y_channel可以与student_var1的x_channel不同,其他维度必须与student_var1相同
  • program (Program): 用于蒸馏训练的fluid program。默认值: fluid.default_main_program()

返回: 由teacher_var1, teacher_var2, student_var1, student_var2组合得到的fsp_loss

使用示例:

l2_loss

paddleslim.dist.l2_loss(teacher_var_namestudent_var_nameprogram=fluid.default_main_program())[[]](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/dist/single_distiller.py#L118)

: l2_loss为program内的teacher var和student var添加l2 loss

参数:

  • teacher_var_name (str): teacher_var的名称.
  • student_var_name (str): student_var的名称.
  • program (Program): 用于蒸馏训练的fluid program。默认值: fluid.default_main_program()

返回: 由teacher_var, student_var组合得到的l2_loss

使用示例:

soft_label_loss

paddleslim.dist.soft_label_loss(teacher_var_namestudent_var_nameprogram=fluid.default_main_program()teacher_temperature=1.student_temperature=1.)[[]](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/dist/single_distiller.py#L136)

soft_label_loss为program内的teacher var和student var添加soft label loss,出自论文 Distilling the Knowledge in a Neural Network

参数:

  • teacher_var_name (str): teacher_var的名称.
  • student_var_name (str): student_var的名称.
  • program (Program): 用于蒸馏训练的fluid program。默认值: fluid.default_main_program()
  • teacher_temperature (float): 对teacher_var进行soft操作的温度值,温度值越大得到的特征图越平滑
  • student_temperature (float): 对student_var进行soft操作的温度值,温度值越大得到的特征图越平滑

返回: 由teacher_var, student_var组合得到的soft_label_loss

使用示例:

loss

paddleslim.dist.loss(loss_funcprogram=fluid.default_main_program()**kwargs) [[]](https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/dist/single_distiller.py#L165)

: loss函数支持对任意多对teacher_var和student_var使用自定义损失函数

参数:

  • **loss_func**( python function): 自定义的损失函数,输入为teacher var和student var,输出为自定义的loss
  • program (Program): 用于蒸馏训练的fluid program。默认值: fluid.default_main_program()
  • **kwargs : loss_func输入名与对应variable名称

返回 :自定义的损失函数loss

使用示例:

注解

在添加蒸馏loss时会引入新的variable,需要注意新引入的variable不要与student variables命名冲突。这里建议两种用法(两种方法任选其一即可):

  1. 建议与student_program使用同一个命名空间,以避免一些未指定名称的variables(例如tmp_0, tmp_1...)多次定义为同一名称出现命名冲突
  2. 建议在添加蒸馏loss时指定一个命名空间前缀

最新文章

  1. 移动端rem布局实践
  2. 3D坦克大战游戏iOS源码
  3. C# 使用IEnumerable,yield 返回结果,同时使用foreach时,在循环内修改变量的值无效(二)
  4. mysql 字符集设置方法
  5. jetty服务器的安装和部署、新增到开机启动服务
  6. c/c++细节知识整理
  7. 《AppletButtonEvent.java》
  8. coco2d-x 纹理研究
  9. JavaScript “\”替换成 “\\”
  10. SQL server数据类型int、bigint、smallint、tinyint
  11. windows7中的“mklink命令” 转
  12. openGL绘制正方形
  13. Vue.js组件间通信方式总结
  14. [LeetCode] Ambiguous Coordinates 模糊的坐标
  15. Ehcache 3.7文档—基础篇—GettingStarted
  16. Maven(五)Eclipse配置Maven插件
  17. 函数isNaN() parseFloat() parseInt() Math对象
  18. Unity5天空盒小黑点问题
  19. sparkuser is not in the sudoers file. This incident will be reported.
  20. 回归JavaScript基础(六)

热门文章

  1. node-redis基本操作
  2. [CTF]Brainfuck/Ook!编码
  3. LeetCode---84. 柱状图中最大的矩形(hard)
  4. 6 JDBC
  5. 联想R720Y空间问题
  6. Java反射机制以及动态代理
  7. 虚拟机快速下载安装配置aarch64-linux-gnu-gcc工具链
  8. 15.Git
  9. [bug] Python AttributeError: module 'web' has no attribute 'application'
  10. Linux命令nohup实现命令后台运行并输出到或记录到日志文件