Comments (4)
这个做不到,需要修改bert模型。
可以参考以下步骤:
- 修改bert的接口,使之返回一个value_layer list,list中的元素是各个层的value_layer
- 给这个特征随便取个名字,比如叫"value_layer",添加为features:
textbrewer.presets.FEATURES.append("value_layer")
- 仿照textbrewer.losses中的损失函数(比如仿照hid_mse_loss),实现你自己的中间层损失函数,这个函数接收教师和学生的value_layer,蒸馏温度和mask为参数(嫌麻烦的话可不实现mask相关逻辑)。并在textbrewer.presets. MATCH_LOSS_MAP中注册:
textbrewer.presets. MATCH_LOSS_MAP['custom_loss'] = your_custom_loss_function
- 之后可以像使用其他损失函数一样使用你的自定义损失函数了,比中间层匹配的配置可写为:
intermediate_matches = [{"layer_T":1, "layer_S":1, "feature":"value_layer", "loss":"custom_loss", "weight":1}, ... ]
最后别忘了,使用新的loss时 adaptor返回的字典里要提供loss需要的value_layer:
def adaptor(batch, model_outputs):
return {'logits' : ...,
'value_layer': ..., #BERT返回的value_layer list,
... }
Hope it helps.
from textbrewer.
补充一点:
如loss形式本身无变化(还是mse或cross-entropy等计算方式)修改的只是输入,也可跳过步骤3,直接利用现有损失函数如hid_mse_loss,att_mse_loss计算value_layer相关的损失,只要把feature改成"value_layer"即可
from textbrewer.
感谢你的耐心解答!
from textbrewer.
工具非常棒!
@airaria 您前面说明了如何自定义特征,并添加到中间层匹配里面去。我们使用过程中遇到一个小问题,如果新特征是不分层的,就像logits那样,该如何添加映射呢?使用CustomMatch吗,但是我们暂时没有发现比较简明的示例。目前我们的做法是使用新特征来作为logits(在满足size要求的情况下),相当于复用了你们预设的logits字段,但是使用的是我们自己定义的新特征。请问有更好的处理方式吗?
from textbrewer.
Related Issues (20)
- pre-trained student weights HOT 3
- Where to find gs4210.pkl file or how to generate it ? thanks HOT 2
- interpreting intermediate matches HOT 5
- Show the progress bar when training. HOT 3
- Picking right layers HOT 3
- How about the distillation effect of gpt2 ? HOT 2
- Does it support translation model? HOT 2
- 在VisionTransformer HOT 7
- 关于ner数据的处理 HOT 2
- notebook_examples/msra_ner.ipynb 运行报错 HOT 12
- 不同维度蒸馏有对应的例子吗,从768降到256 HOT 4
- msra_ner.ipynb最后的trainer.evaluate()显示CUDA out of memory,请问训练的显存要求是多大?十分感谢! HOT 2
- 老师,您好,请问有多任务多教师的蒸馏的demo吗? HOT 4
- 老师您好,我想问一下,比如roberta蒸馏到tinybert,中间的hidden是通过线性层拉到同样的维度去算mse,那在推理的时候岂不是这些经过梯度更新的线性层毫无作用?那请问这些线性层仅仅就是为了调整维度? HOT 2
- 蒸馏后的模型进行evaluate,报错AxisError: axis 2 is out of bounds for array of dimension 1 HOT 5
- 可以使用chatgpt蒸馏到bert或者T5吗? HOT 2
- 麻烦问下,目前支持llama模型吗 HOT 2
- 请问支持BERT-of-Theseus的蒸馏方式吗 HOT 3
- TextBrewer/examples/notebook_examples/msra_ner.ipynb have bug? HOT 1
- TextBrewer/src/textbrewer/distiller_utils.py get_outputs_from_batch fails tocheck dicts properly for maskedLM HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from textbrewer.