前排感谢这个开源项目:https://github.com/Morizeyao/GPT2-Chinese
原文链接:https://moluuser.com/archives/74/
记录下运行以上项目的流程。
运行环境
导入语料
由于使用单一文本进行训练,只需要把数据集放置在./data/train.json即可。
如果需要合并txt文本,使用批处理type *.txt>>all.txt即可。
模型训练
python train_single.py --raw
文本生成
python ./generate.py --length=400 --nsamples=4 --prefix=xxx --fast_pattern --save_samples --save_samples_path=/mnt/xx
参数说明
-length:参数为生成文本的长度。
--prefix:生成文章的开头。
--fast_pattern:如果生成的length参数比较小,速度基本无差别,默认不采用fast_pattern方式。
--save_samples:默认将输出样本直接打印到控制台,传递此参数,将保存在根目录下的samples.txt。
--save_samples_path:可自行指定保存的目录,默认可递归创建多级目录,不可以传递文件名称,文件名称默认为samples.txt。
结果
这里model使用的是hughqiu训练生成模型,感谢分享。
Others
可能需要修改的地方
在train.py中:
#lines = json.load(f)
lines = f.readlines()
#full_tokenizer.max_len = 999999
其他类似开源项目
https://github.com/hughqiu/GPT2-Chinese
https://github.com/jianyq/Tong-Music
https://github.com/GaoPeng97/transformer-xl-chinese
https://github.com/yangjianxin1/GPT2-chitchat