语言:中文
组件:
中国古代GPT2模型
模型描述
该模型由UER-py预训练,相关论文见此处。此外,模型也可通过腾讯预训练框架TencentPretrain进行预训练,该框架继承UER-py以支持十亿参数以上模型,并扩展为多模态预训练框架,论文详见此处。
本模型专用于生成文言文文本。您可通过以下途径获取模型:
使用方式
可直接通过文本生成管道调用模型:
>>> from transformers import BertTokenizer, GPT2LMHeadModel, TextGenerationPipeline
>>> tokenizer = BertTokenizer.from_pretrained("uer/gpt2-chinese-ancient")
>>> model = GPT2LMHeadModel.from_pretrained("uer/gpt2-chinese-ancient")
>>> text_generator = TextGenerationPipeline(model, tokenizer)
>>> text_generator("当是时", max_length=100, do_sample=True)
[{'generated_text': '[CLS]当是时 所 议 者 不 为 无 据 , 况 亦 在 之 列 乎 ? 然 则 今 日 之 事 , 所 当 思 者 在 何 ? 欲 求 国 是 于 天 下 , 莫 在 于 得 人 。 臣 以 为 求 人 之 法 , 不 在 多 用 官 一 途 。 诚 使 得 才 者 众 , 人 才 者 优 , 则 治 所 当 得 , 而 不 事 于 官 者 , 人 才 乃 其 常 也 。 所 当 讲 者'}]
训练数据
训练数据包含300万条文言文文本,源自daizhigev20语料库。由于部分古籍无标点,我们采用了BNU ICIP实验室开发的古籍标点系统进行处理。
训练流程
模型在腾讯云上通过UER-py预训练完成。训练参数如下:
- 总步数:50万步
- 序列长度:320
- 扩展词汇表处理低频词(出现次数≥100的古汉语单字纳入词表)
预处理命令:
python3 preprocess.py --corpus_path corpora/ancient_chinese.txt \
--vocab_path models/google_zh_ancient_vocab.txt \
--dataset_path ancient_chinese_dataset.pt --processes_num 16 \
--seq_length 320 --data_processor lm
训练命令:
python3 pretrain.py --dataset_path ancient_chinese_dataset.pt \
--vocab_path models/google_zh_ancient_vocab.txt \
--config_path models/bert_base_config.json \
--output_model_path models/ancient_chinese_gpt2_model.bin \
--world_size 8 --gpu_ranks 0 1 2 3 4 5 6 7 \
--total_steps 500000 --save_checkpoint_steps 100000 --report_steps 10000 \
--learning_rate 5e-4 --batch_size 32
格式转换命令(转为Huggingface格式):
python3 scripts/convert_gpt2_from_uer_to_huggingface.py --input_model_path models/ancient_chinese_gpt2_model.bin-500000 \
--output_model_path pytorch_model.bin \
--layers_num 12
引用文献
@article{radford2019language,
title={语言模型是无监督多任务学习器},
author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},
year={2019}
}
@article{zhao2019uer,
title={UER:开源预训练模型工具包},
author={赵哲、陈辉、张金斌、赵鑫、刘涛、卢伟、陈曦、邓浩堂、巨琦、杜晓勇},
journal={EMNLP-IJCNLP 2019},
pages={241},
year={2019}
}
@article{zhao2023tencentpretrain,
title={TencentPretrain:支持多模态的大规模预训练工具包},
author={赵哲、李雨东、侯成、赵静等},
journal={ACL 2023},
pages={217},
year={2023}
}