Skip to content

Latest commit

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

German T5 Tokenizer

This repo gives an overview of how to train a custom T5 tokenizer for our German model.

Training corpus for German T5 tokenizer

We experiment with different corpora from GC4. The corpus used for training a tokenizer has huge impact on the downstream task model performance, as it can be seen in the "How Good is Your Tokenizer?" paper.

Thus, we calculate the so called subword fertility rate (number of subtokens / number of total tokens) for three downstream tasks: GermEval 2018 (Classification), GermEval 2014 (NER) and Universal Dependencies (PoS Tagging, Parsing).

To get an overview, we calculated the subword fertility rate for various German (cased) models:

ModelVocab SizeApproachGermEval 2018GermEval 2014UD HDTAverage
GC4 ELECTRA64,000WordPiece1.47491.20631.23371.30
German BERT30,000WordPiece1.63261.31211.38521.44
DBMDZ German BERT31,102WordPiece1.57051.30041.351.41
GottBERT52,009BPE1.78061.39341.41721.53
mT5250,112SPM1.91491.72631.75451.80
Ours32,000SPM1.70791.38241.40971.50

We use the following packages from GC4 (filtered) to construct our vocabulary:

FilenameInstancesTokensSize
de_head_0000_2015-48_filtered.txt6,823,262230,285,0461.7G
de_head_0000_2016-44_filtered.txt1,305,75070,525,146509M
de_head_0004_2017-39_filtered.txt1,585,72551,027,153364M
de_head_0007_2018-30_filtered.txt1,321,01742,280,783302M
de_head_0007_2019-09_filtered.txt2,798,13291,992,353654M
de_head_0007_2020-10_filtered.txt1,204,55437,558,448270M
Total15,038,440523,668,9293.7G

De-Constructing original T5 Tokenizer

Before we can start training an own tokenizer, we need to de-construct the original T5 tokenizer.

The original tokenizer is sentencepiece-based as mentioned in the paper. So let's download the spiece.model from Hugging Face model hub via:

$ wget "https://huggingface.co/t5-base/resolve/main/spiece.model"

and inspect it:

importsentencepieceasspmvocab_file="./spiece.model"sp_model=spm.SentencePieceProcessor()
sp_model.Load(vocab_file)

This will load the original spm-model. Now let's have a look at the first ids and items in the vocab:

forindexinrange(0,10):
print(index, "->", sp_model.IdToPiece(index))

this outputs:

0 -><pad>
1 -></s>
2 -><unk>
3 -> ▁
4 -> X
5 ->.
6 -> ,
7 -> s
8 -> ▁the
9 -> a

The first three ids are some kind of special symbols: <pad>, and </unk> and used for padding or denoting an end of sentence. When constructing our own vocab, we need to make sure, that we use the same ids at the beginning.

SPM training

Now we can train our own spm model. We use the unigram approach, because this was also used for building the ALBERT vocab (slightly mentioned in the documentation). We did not experiment with our algorithms.

Here's the training command:

importsentencepieceasspmspm.SentencePieceTrainer.train(
input="vocab_first_attempt.txt",
model_prefix="spiece",
vocab_size=32000,
unk_id=2,
bos_id=-1,
eos_id=1,
pad_id=0,
model_type="unigram",
train_extremely_large_corpus=True,
)

We set the the ids <pad_id>, <eos_id> and <unk_id> according to the original T5 spm model and also use a vocab size of 32,000.

Notice: training can take ~2 hours. For our 3.7GB corpus it consumes ~170GB of RAM.

About

How to train German T5 Tokenizer

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors