文章目录
- [下载 GPT-2 模型](#下载 GPT-2 模型)
- [快速开始 GPT-2](#快速开始 GPT-2)
下载 GPT-2 模型
bash
git clone https://huggingface.co/uer/gpt2-chinese-cluecorpussmall
# 或单独下载 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/uer/gpt2-chinese-cluecorpussmall
快速开始 GPT-2
- 安装依赖
bash
pip3 install torch torchvision torchaudio
pip3 install transformers
- python脚本
python
from transformers import BertTokenizer, GPT2LMHeadModel, TextGenerationPipeline
tokenizer = BertTokenizer.from_pretrained("./gpt2-chinese-cluecorpussmall")
model = GPT2LMHeadModel.from_pretrained("./gpt2-chinese-cluecorpussmall")
text_generator = TextGenerationPipeline(model, tokenizer)
result = text_generator("这是很久之前的事情了", max_length=100, do_sample=True)
print(result)
- 输出结果