🚀 中文对联GPT2模型
本模型用于生成中文对联,借助预训练技术,能够根据上联生成合适的下联,为对联创作提供便利。
🚀 快速开始
你可以使用文本生成管道直接调用该模型:
当参数 skip_special_tokens 为 True 时:
>>> from transformers import BertTokenizer, GPT2LMHeadModel, TextGenerationPipeline
>>> tokenizer = BertTokenizer.from_pretrained("uer/gpt2-chinese-couplet")
>>> model = GPT2LMHeadModel.from_pretrained("uer/gpt2-chinese-couplet")
>>> text_generator = TextGenerationPipeline(model, tokenizer)
>>> text_generator("[CLS]丹 枫 江 冷 人 初 去 -", max_length=25, do_sample=True)
[{'generated_text': '[CLS]丹 枫 江 冷 人 初 去 - 黄 叶 声 从 天 外 来 阅 旗'}]
当参数 skip_special_tokens 为 False 时:
>>> from transformers import BertTokenizer, GPT2LMHeadModel, TextGenerationPipeline
>>> tokenizer = BertTokenizer.from_pretrained("uer/gpt2-chinese-couplet")
>>> model = GPT2LMHeadModel.from_pretrained("uer/gpt2-chinese-couplet")
>>> text_generator = TextGenerationPipeline(model, tokenizer)
>>> text_generator("[CLS]丹 枫 江 冷 人 初 去 -", max_length=25, do_sample=True)
[{'generated_text': '[CLS]丹 枫 江 冷 人 初 去 - 黄 叶 声 我 酒 不 辞 [SEP] [SEP] [SEP] [SEP] [SEP] [SEP] [SEP] [SEP] [SEP]'}]
✨ 主要特性
- 本模型由 UER-py 进行预训练,该工具在 此论文 中被介绍。此外,模型也可以通过 TencentPretrain 进行预训练,相关内容见 此论文。TencentPretrain 继承了 UER-py,支持参数超过十亿的模型,并将其扩展为多模态预训练框架。
- 由于在
pipelines.py
中使用了参数 skip_special_tokens
,像 [SEP]
、[UNK]
这样的特殊标记会被删除,托管推理 API(右侧)的输出结果可能无法正确显示。
📦 安装指南
模型下载
你可以从以下途径下载模型:
训练步骤
1. 数据预处理
python3 preprocess.py --corpus_path corpora/couplet.txt \
--vocab_path models/google_zh_vocab.txt \
--dataset_path couplet_dataset.pt --processes_num 16 \
--seq_length 64 --data_processor lm
2. 预训练模型
python3 pretrain.py --dataset_path couplet_dataset.pt \
--vocab_path models/google_zh_vocab.txt \
--config_path models/gpt2/config.json \
--output_model_path models/couplet_gpt2_model.bin \
--world_size 8 --gpu_ranks 0 1 2 3 4 5 6 7 \
--total_steps 25000 --save_checkpoint_steps 5000 --report_steps 1000 \
--learning_rate 5e-4 --batch_size 64
3. 转换为 Huggingface 格式
python3 scripts/convert_gpt2_from_uer_to_huggingface.py --input_model_path models/couplet_gpt2_model.bin-25000 \
--output_model_path pytorch_model.bin \
--layers_num 12
📚 详细文档
训练数据
训练数据包含 700,000 条中文对联,这些对联由 couplet-clean-dataset 收集。
引用信息
@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}
}
🔧 技术细节
本模型基于预训练技术,使用特定的工具(UER-py 或 TencentPretrain)在腾讯云平台上进行训练。通过对大量中文对联数据的学习,模型能够掌握对联的语言规律和对仗规则,从而实现对联的生成。在训练过程中,设置了合适的序列长度、训练步数、学习率等参数,以保证模型的性能和效果。同时,在使用模型时,参数 skip_special_tokens
的设置会影响输出结果中特殊标记的显示情况。