tensorflow
代码示例如下:
# 将类别标签进行one-hot编码one_hot = tf.one_hot(y, n_classes,1.0,0.0) # n_classes为类别总数, n为类别标签# one_hot = tf.cast(one_hot_int, tf.float32)teacher_tau = tf.scalar_mul(1.0/args.tau, teacher) # teacher为teacher模型直接输出张量, tau为温度系数Tstudent_tau = tf.scalar_mul(1.0/args.tau, student) # 将模型直接输出logits张量student处于温度系数Tobjective1 = tf.nn.sigmoid_cross_entropy_with_logits(student_tau, one_hot)objective2 = tf.scalar_mul(0.5, tf.square(student_tau-teacher_tau))"""student模型最终的损失函数由两部分组成:第一项是由小模型的预测结果与大模型的“软标签”所构成的交叉熵(cross entroy);第二项为预测结果与普通类别标签的交叉熵 。"""tf_loss = (args.lamda*tf.reduce_sum(objective1) + (1-args.lamda)*tf.reduce_sum(objective2))/batch_size
tf.scalar_mul
函数为对 tf
张量进行固定倍率 scalar
缩放函数 。一般 T 的取值在 1 - 20 之间 , 这里我参考了开源代码,取值为 3 。我发现在开源代码中 student
模型的训练,有些是和 teacher
模型一起训练的,有些是 teacher 模型训练好后直接指导 student 模型训练 。
六,浅层/轻量网络浅层网络:通过设计一个更浅(层数较少)结构更紧凑的网络来实现对复杂模型效果的逼近, 但是浅层网络的表达能力很难与深层网络相匹敌 。因此 , 这种设计方法的局限性在于只能应用解决在较为简单问题上 。如分类问题中类别数较少的 task
。
轻量网络:使用如 MobilenetV2、ShuffleNetv2
等轻量网络结构作为模型的 backbone
可以大幅减少模型参数数量 。
参考资料
- 神经网络模型压缩和加速之知识蒸馏
- https://github.com/chengshengchan/model_compression/blob/master/teacher-student.py
- https://github.com/dkozlov/awesome-knowledge-distillation
- XNOR-Net
- 解析卷积神经网络-深度学习实践手册
- 知识蒸馏(Knowledge Distillation)简述(一)
推荐阅读
- tensorflow-gpu版本安装及深度神经网络训练与cpu版本对比
- Learning Records 计算机网络
- Springboot 之 Filter 实现超大响应 JSON 数据压缩
- 手机网络怎么连接上电视(手机通过type-c连接电视)
- 一 网络安全主动进攻之DNS基础和ettercap实现DNS流量劫持
- TCP和UDP的区别与联系以及网络字节序和主机字节序的转换函数实践
- Go的网络编程详解
- 红米note11Pro怎么设置5G_设置5G网络方法
- Springboot 之 Filter 实现 Gzip 压缩超大 json 对象
- 对循环神经网络参数的理解|LSTM RNN Input_size Batch Sequence