模型:
德国版的 GPT2 有两个版本:大型和小型:
有关限制和偏见的考虑,请参见 GPT2 model card 。有关 GPT2 的详细信息,请参见 GPT2 documentation 。
我在 CC-100 数据集和德文维基百科上评估了 GerPT2-large 和另一个德文 GPT2 - dbmdz/german-gpt2 :
| CC-100 (PPL) | Wikipedia (PPL) | |
|---|---|---|
| dbmdz/german-gpt2 | 49.47 | 62.92 |
| GerPT2 | 24.78 | 35.33 |
| GerPT2-large | 16.08 | 23.26 |
有关代码,请参见 GerPT2 Github repository 中的 evaluate.py 脚本。
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
tokenizer = AutoTokenizer.from_pretrained("benjamin/gerpt2-large")
model = AutoModelForCausalLM.from_pretrained("benjamin/gerpt2-large")
prompt = "<your prompt>"
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
print(pipe(prompt)[0]["generated_text"])
此外,两个技巧可能会改善生成的文本:
output = model.generate(
# during training an EOS token was used to mark the beginning of each text
# so it can help to insert it at the start
torch.tensor(
[tokenizer.eos_token_id] + tokenizer.encode(prompt)
).unsqueeze(0),
do_sample=True,
# try setting bad_words_ids=[[0]] to disallow generating an EOS token, without this the model is
# prone to ending generation early because a significant number of texts from the training corpus
# is quite short
bad_words_ids=[[0]],
max_length=max_length,
)[0]
print(tokenizer.decode(output))
GerPT2-large 是使用来自 CC-100 Corpus 的整个德文数据进行训练的,并且权重是从 English GPT2 model 初始化的。GerPT2-large 训练了:
训练大约耗时 12 天,使用了 8 个 TPUv3 核心。
要训练 GerPT2-large,请按照以下步骤进行。脚本位于 Github repository 中:
ĠMinde -> Ġleast Ġjed -> Ġwhatsoever flughafen -> Air vermittlung -> employment teilung -> ignment ĠInterpretation -> Ġinterpretation Ġimport -> Ġimported hansa -> irl genehmigungen -> exempt ĠAuflist -> Ġlists Ġverschwunden -> Ġdisappeared ĠFlyers -> ĠFlyers Kanal -> Channel Ġlehr -> Ġteachers Ġnahelie -> Ġconvenient gener -> Generally mitarbeiter -> staff
这对我进行的试验有很大帮助,尽管由于预算和时间限制,我无法进行全面比较。要使用此 WTE 矩阵,可以通过 wte_path 参数传递给训练脚本。感谢 this blogpost 提出了从英文权重初始化 GPT2 的思路。
GerPT2 基于 MIT 许可证。
请按以下方式引用 GerPT2:
@misc{Minixhofer_GerPT2_German_large_2020,
author = {Minixhofer, Benjamin},
doi = {10.5281/zenodo.5509984},
month = {12},
title = {{GerPT2: German large and small versions of GPT2}},
url = {https://github.com/bminixhofer/gerpt2},
year = {2020}
}
感谢 Hugging Face 提供的出色工具和基础设施。特别感谢 Artus Krohn-Grimberghe 和 LYTiQ 为培训提供资源支持。