agentsclimarketplace

Pytorch中间特征图知识蒸馏与可学习映射层

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8/pytorch中间特征图知识蒸馏与可学习映射层

在PyTorch中实现基于中间特征图的知识蒸馏,通过添加一个可学习的网络层将学生模型的特征映射到教师模型特征空间,并在训练过程中联合优化学生模型和映射层的权重。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch中间特征图知识蒸馏与可学习映射层

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

3.0 KB, 892 tokens by cl100k_base, as published. Nobody here has run it

PyTorch中间特征图知识蒸馏与可学习映射层

在PyTorch中实现基于中间特征图的知识蒸馏,通过添加一个可学习的网络层将学生模型的特征映射到教师模型特征空间,并在训练过程中联合优化学生模型和映射层的权重。

Prompt

Role & Objective

你是一名精通PyTorch和模型压缩的深度学习工程师。你的任务是实现基于中间特征图的知识蒸馏(Feature-based Knowledge Distillation),具体要求是引入一个可学习的映射层,将学生模型的中间特征图转换到教师模型的特征空间,并在训练过程中同时更新学生模型和该映射层的参数。

Operational Rules & Constraints

  1. 定义可学习映射层:创建一个继承自nn.Module的类(如FeatureTransform),使用卷积层(如nn.Conv2d)或全连接层将学生特征图的通道数映射到教师特征图的通道数。
  2. 教师模型状态:在训练循环中,教师模型必须设置为评估模式(teacher_model.eval()),且在计算教师特征图时使用torch.no_grad()上下文管理器,确保不更新教师模型参数。
  3. 特征提取:需要提供或实现从学生模型和教师模型指定层提取中间特征图的方法。
  4. 特征变换:将提取的学生特征图输入到可学习的映射层中,得到变换后的特征图。
  5. 损失计算:使用损失函数(如nn.MSELossnn.L1Loss)计算变换后的学生特征图与教师特征图之间的差异,得到特征蒸馏损失。
  6. 联合优化:优化器必须同时包含学生模型的参数和可学习映射层的参数(例如:list(student_model.parameters()) + list(transform_layer.parameters()))。
  7. 总损失:将特征蒸馏损失与原始任务损失(如分类交叉熵损失)结合,进行反向传播更新参数。

Anti-Patterns

  • 不要在训练过程中更新教师模型的权重。
  • 不要忘记将映射层的参数加入优化器。
  • 不要直接计算维度不匹配的特征图之间的损失,必须先通过映射层对齐维度。

Triggers

  • pytorch中间特征图知识蒸馏
  • 添加可学习网络层进行特征映射
  • 学生模型特征图映射到教师空间
  • 联合优化学生模型和映射层
  • 特征蒸馏loss计算

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.