language: zh
datasets: CLUECorpusSmall
widget:
中文GPT2模型系列
模型描述
本系列GPT2模型中,除GPT2-xlarge外,均由UER-py预训练实现(技术细节详见论文)。GPT2-xlarge模型则通过TencentPretrain框架预训练(技术细节见论文),该框架继承UER-py支持十亿参数以上模型预训练的能力,并扩展为多模态预训练框架。其余模型也可通过TencentPretrain进行预训练。
这些模型专用于中文文本生成任务。您可以通过以下两种方式获取:
- UER-py模型库
- HuggingFace平台(链接如下):
注:6层模型因采用distilgpt2架构且训练过程未使用大模型监督,故命名为GPT2-distil。
使用示例
以下以GPT2-distil为例展示文本生成功能:
>>> from transformers import BertTokenizer, GPT2LMHeadModel, TextGenerationPipeline
>>> tokenizer = BertTokenizer.from_pretrained("uer/gpt2-distil-chinese-cluecorpussmall")
>>> model = GPT2LMHeadModel.from_pretrained("uer/gpt2-distil-chinese-cluecorpussmall")
>>> text_generator = TextGenerationPipeline(model, tokenizer)
>>> text_generator("这是很久之前的事情了", max_length=100, do_sample=True)
[{'generated_text': '这是很久之前的事情了 。 我 现 在 想 起 来 就 让 自 己 很 伤 心 , 很 失 望 。 我 现 在 想 到 , 我 觉 得 大 多 数 人 的 生 活 比 我 的 生 命 还 要 重 要 , 对 一 些 事 情 的 看 法 , 对 一 些 人 的 看 法 , 都 是 在 发 泄 。 但 是 , 我 们 的 生 活 是 需 要 一 个 信 用 体 系 的 。 我 不 知'}]
训练数据
使用CLUECorpusSmall作为训练语料。
训练流程
GPT2-xlarge模型通过TencentPretrain预训练,其余模型通过UER-py在腾讯云完成。训练分为两个阶段:
- 序列长度128,训练1,000,000步
- 序列长度1024,追加训练250,000步
以UER-py训练的GPT2-distil为例:
第一阶段:
预处理与训练命令(详见原文)...
第二阶段:
长序列预处理与训练命令(详见原文)...
最终转换为Huggingface格式:
python3 scripts/convert_gpt2_from_uer_to_huggingface.py --input_model_path models/cluecorpussmall_gpt2_distil_seq1024_model.bin-250000 \
--output_model_path pytorch_model.bin \
--layers_num 6
GPT2-xlarge使用TencentPretrain训练:
深度加速训练与格式转换命令(详见原文)...
文献引用
@article{radford2019language,
title={Language Models are Unsupervised Multitask Learners},
author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},
year={2019}
}
@article{zhao2019uer,
title={UER: An Open-Source Toolkit for Pre-training Models},
author={Zhao, Zhe and Chen, Hui and Zhang, Jinbin and Zhao, Xin and Liu, Tao and Lu, Wei and Chen, Xi and Deng, Haotang and Ju, Qi and Du, Xiaoyong},
journal={EMNLP-IJCNLP 2019},
pages={241},
year={2019}
}
@article{zhao2023tencentpretrain,
title={TencentPretrain: A Scalable and Flexible Toolkit for Pre-training Models of Different Modalities},
author={Zhao, Zhe and Li, Yudong and Hou, Cheng and Zhao, Jing and others},
journal={ACL 2023},
pages={217},
year={2023}