Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
之江实验室 080cccd85a | 3 years ago | |
---|---|---|
.. | ||
examples | 3 years ago | |
src | 3 years ago | |
theseus | 3 years ago | |
README.md | 3 years ago | |
__init__.py | 3 years ago | |
glue_process.sh | 3 years ago | |
run_download_glue_data.sh | 3 years ago | |
run_eval_student_bert_pkd.sh | 3 years ago | |
run_eval_student_distilled_lstm.sh | 3 years ago | |
run_eval_student_kd.sh | 3 years ago | |
run_eval_student_tinybert.sh | 3 years ago | |
run_eval_teacher.sh | 3 years ago | |
run_eval_theseus.sh | 3 years ago | |
run_train_student_bert_pkd.sh | 3 years ago | |
run_train_student_distilled_lstm.sh | 3 years ago | |
run_train_student_kd.sh | 3 years ago | |
run_train_student_tinybert.sh | 3 years ago | |
run_train_teacher.sh | 3 years ago | |
run_train_theseus.sh | 3 years ago |
知识蒸馏:通过一些优化目标从大型、知识丰富的teacher模型学习一个小型的student模型
炼知技术平台提供了4个知识蒸馏相关算子,以及众多基于Oneflow算子复现的知识蒸馏模型和使用示例。
类型 | 知识蒸馏模型 | 主要算子 | 使用文档 |
---|---|---|---|
软标签蒸馏 | KD | 软标签蒸馏 | 链接 |
Distilled-BiLSTM | 软标签蒸馏,将BERT蒸馏到BiLSTM | 链接 | |
从其他知识蒸馏 | BERT-PKD | 软标签蒸馏+层与层蒸馏 | 链接 |
TinyBERT | 软标签蒸馏+层与层蒸馏+注意力蒸馏 | 链接 | |
模块替换 | BERT-Theseus | 依照概率替换原有的BERT模块和Theseus的模块组成新的模型来训练 | 链接 |
完整的环境可以通过以下命令安装:
conda create -n distil python=3.6
python3 -m pip install --find-links https://oneflow-inc.github.io/nightly oneflow_cu101 --user
知识蒸馏主要针对NLP相关的任务,炼知平台在GLUE任务的数据集上对不同算法进行了测试。
可以通过执行以下脚本下载GLUE任务的所有数据集,将会自动下载并解压到'--data_dir=data'目录下。
bash run_download_glue_data.sh
或者
python ../src/download_glue_data.py --data_dir data/glue_data --tasks all
TASKS = ["CoLA", "SST", "MRPC", "QQP", "STS", "MNLI", "SNLI", "QNLI", "RTE", "WNLI", "diagnostic"]
以上脚本将会默认下载所有GLUE任务数据集,也可以通过'--tasks=TASKS',指定下载某些数据集
也可以在这里下载GLUE任务数据集,并放置到相关目录(data/glue_data)下
链接: https://pan.baidu.com/s/1Im0uQM_V_zXkmQuHsFae0A 提取码: u64u
参考加载与准备OneFlow数据集,制作OFRecords数据集。或者执行以下命令,生成OFRecords数据集:
bash glue_process.sh
或者直接下载转换后的OFRecords GLUE数据集,并放置到相关目录(data/glue_ofrecord)下:
链接: https://pan.baidu.com/s/1CY2BfCGBZEeo1EgY5JQcuA 提取码: v2h4
预训练BERT模型下载地址:
链接: https://pan.baidu.com/s/1jfTUY7ygcZZOJzjfrgUL8Q 提取码: 6b87
下载后放置在./models/uncased_L-12_H-768_A-12_oneflow
执行以下脚本进行微调教师模型:
bash run_train_teacher.sh
我们微调过的教师模型可以在这里下载: 链接: https://pan.baidu.com/s/1jiOTSPBmmBoij0UwPO6UKw 提取码: 9xkp
并放置到"model_compress/distil/models/finetuned_teacher/"
。
在上述数据集的dev集上性能为SST-2: 92.2%, QQP: 91.1%, MRPC: 89.2%, RTE: 69.8%, CoLA: 58.5%
评价指标:
微调后,可以执行以下脚本对教师模型进行测试:
bash run_eval_teacher.sh
执行以下脚本将教师模型蒸馏到学生模型:
DATA_ROOT: GLUE数据集总路径
dataset: 任务名
FT_BERT_BASE_DIR: 在特定任务上微调过的教师模型路径
TMP_STUDENT_DIR: 临时学生模型路径(如果需要的话,不需要则设为TMP_STUDENT_DIR="")
STUDENT_DIR: 学生模型保存路径
RESULT_DIR: 测试结果json文件保存路径 (如果RESULT_DIR="",则默认保存到模型保存路径下,results_eval.json)
SERVE_FOR_ONLINE: 模型是否用于上线 (默认SERVE_FOR_ONLINE='False',如果SERVE_FOR_ONLINE='True',则删除清理模型保存路径中的无关变量,如教师模型参数和优化器参数等等)
不同知识蒸馏算法:
bash run_train_student_kd.sh
bash run_train_student_distilled_lstm.sh
bash run_train_student_bert_pkd.sh
注:BERT-PKD可以随机初始化,也可以选择根据教师BERT中间层进行初始化,详细步骤请查阅这里
临时学生模型下载链接(SST-2, RTE, MRPC, CoLA, QQP数据集) 链接: https://pan.baidu.com/s/17F8KVsLd_lMODLaVLc7yrQ 提取码: 95ir
下载并解压,将相应的模型放置到"./models/student_model/bert_pkd_3"
路径下
bash run_train_student_tinybert.sh
临时学生模型(通用TinyBERT)下载链接 链接: https://pan.baidu.com/s/1vZDILxXi-uxo2v3zFlWL3A 提取码: kpia
BERT类模型最大序列长度设为128; LSTM类模型最大序列长度设为32,词表大小为10000
执行以下脚本进行测试:
DATA_ROOT: GLUE数据集总路径
dataset: 任务名
STUDENT_DIR: 学生模型保存路径,蒸馏过的学生模型下载链接如下(SST-2数据集)
RESULT_DIR: 测试结果json文件保存路径 (如果RESULT_DIR="",则默认保存到模型保存路径下,results_eval.json)
不同知识蒸馏算法:
KD
下载链接: https://pan.baidu.com/s/1EgQyQgxAcFAG8Ch3-4VPaw 提取码: 5k9p
bash run_eval_student_kd.sh
Distilled-BiLSTM
下载链接: https://pan.baidu.com/s/1M4XzB2DnLikglxVFvhnYpw 提取码: hqhj
bash run_eval_student_distilled_lstm.sh
BERT-PKD
bash run_eval_student_bert_pkd.sh
TinyBERT
下载链接: https://pan.baidu.com/s/1nOAZHd3wLmyVw2vTJB7KfQ 提取码: ma65
bash run_eval_student_tinybert.sh
一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能
Java Vue Python Text JavaScript other