Unigramトークナイザの最大トークン長と最大語彙数は係り受け解析に影響するのか
Unigramトークナイザにおける最大トークン長Mと最大語彙数Vが、UPOS/LAS/MLASにどう影響するか調査した。DeBERTa(V2)モデルの製作には、ja_gsd-ud-train.conlluの各文だけを用いている。なお、参考として、トークナイザをBertJapaneseTokenizerに入れ替えた場合のUPOS/LAS/MLASも示した。
ja_gsd-ud-dev.conlluで評価
| V=4000 | V=8000 | V=16000 | V=32000 |
M=1 |
66.30/48.83/32.40 |
68.08/51.42/34.57 |
67.42/50.10/33.60 |
67.98/51.43/34.38 |
---|
M=2 |
73.40/59.47/42.18 |
73.86/60.10/43.20 |
74.01/59.55/42.81 |
72.98/59.03/41.69 |
---|
M=4 |
71.95/57.65/40.45 |
73.59/59.81/42.61 |
74.05/61.20/43.58 |
74.40/61.10/44.11 |
---|
M=8 |
73.97/59.86/43.29 |
73.70/60.30/42.72 |
73.67/60.61/43.14 |
73.90/60.16/43.31 |
---|
M=16 |
73.45/58.92/42.48 |
73.79/60.51/43.07 |
73.54/59.66/42.64 |
74.14/60.60/43.57 |
---|
BJT |
82.52/85.05/63.36 |
90.34/89.31/74.78 |
89.66/86.87/72.71 |
89.53/86.97/72.73 |
---|
ja_gsd-ud-test.conlluでテスト
| V=4000 | V=8000 | V=16000 | V=32000 |
M=1 |
64.58/46.52/28.95 |
66.60/49.41/31.88 |
65.63/48.10/30.69 |
66.56/49.01/31.42 |
---|
M=2 |
72.31/57.61/38.87 |
72.71/58.24/39.99 |
73.06/57.86/40.16 |
71.74/57.30/38.41 |
---|
M=4 |
71.77/57.86/38.89 |
72.67/58.42/40.29 |
73.40/59.56/41.43 |
73.61/59.52/41.81 |
---|
M=8 |
73.39/58.97/41.40 |
73.07/59.15/41.06 |
73.52/59.61/41.39 |
73.30/59.21/41.21 |
---|
M=16 |
73.49/59.12/41.80 |
73.24/59.13/41.20 |
73.05/59.26/41.07 |
74.00/60.39/42.45 |
---|
BJT |
80.54/83.31/58.93 |
89.28/87.86/71.71 |
88.88/86.00/69.96 |
89.03/86.35/70.39 |
---|
作業環境
mdx 1GPU (NVIDIA A100-SXM4-40GB)
- tokenizers 0.12.1
- transformers 4.19.1
- esupar 1.2.7
- torch 1.11.0+cu113
- Universal Dependencies 2.10
/bin/shスクリプト
#! /bin/sh
URL=https://github.com/UniversalDependencies/UD_Japanese-GSD
D=`basename $URL`
test -d $D || git clone --depth=1 $URL
for F in train dev test
do nawk -F'\t' '{OFS=FS;if(NF==10)$6="_";print}' $D/*-$F*.conllu > $F.conllu
sed -n 's/^# text = //p' $F.conllu > $F.txt
done
S='{if(NF==10&&$1~/^[1-9][0-9]*$/)printf($1>1?" %s":"%s",$2);if(NF==0)print}'
nawk -F'\t' "$S" $D/*-train.conllu > token.txt
U=http://universaldependencies.org/conll18/conll18_ud_eval.py
C=`basename $U`
test -f $C || curl -LO $U
for M in 1 2 4 8 16
do for V in 4000 8000 16000 32000
do test -d deberta$M-$V || python3 -c m,v=$M,$V'
from transformers import (DataCollatorForLanguageModeling,TrainingArguments,
DebertaV2TokenizerFast,DebertaV2Config,DebertaV2ForMaskedLM,Trainer)
from tokenizers import (Tokenizer,models,pre_tokenizers,normalizers,processors,
decoders,trainers)
s=["[CLS]","[PAD]","[SEP]","[UNK]","[MASK]"]
spt=Tokenizer(models.Unigram())
spt.pre_tokenizer=pre_tokenizers.Sequence([pre_tokenizers.Whitespace(),
pre_tokenizers.Punctuation()])
spt.normalizer=normalizers.Sequence([normalizers.Nmt(),normalizers.NFKC()])
spt.post_processor=processors.TemplateProcessing(single="[CLS] $A [SEP]",
pair="[CLS] $A [SEP] $B:1 [SEP]:1",special_tokens=[("[CLS]",0),("[SEP]",2)])
spt.decoder=decoders.WordPiece(prefix="",cleanup=True)
spt.train(trainer=trainers.UnigramTrainer(vocab_size=v,max_piece_length=m,
special_tokens=s,unk_token="[UNK]",n_sub_iterations=2),files=["token.txt"])
spt.save("tokenizer.json")
tkz=DebertaV2TokenizerFast(tokenizer_file="tokenizer.json",
do_lower_case=False,keep_accents=True,bos_token="[CLS]",cls_token="[CLS]",
pad_token="[PAD]",sep_token="[SEP]",unk_token="[UNK]",mask_token="[MASK]",
vocab_file="/dev/null",model_max_length=512,split_by_punct=True)
t=tkz.convert_tokens_to_ids(s)
cfg=DebertaV2Config(hidden_size=768,num_hidden_layers=12,num_attention_heads=12,
intermediate_size=3072,max_position_embeddings=tkz.model_max_length,
vocab_size=len(tkz),tokenizer_class=type(tkz).__name__,
bos_token_id=t[0],pad_token_id=t[1],eos_token_id=t[2])
arg=TrainingArguments(num_train_epochs=8,per_device_train_batch_size=64,
output_dir="/tmp",overwrite_output_dir=True,save_total_limit=2)
class ReadLineDS(object):
def __init__(self,file,tokenizer):
self.tokenizer=tokenizer
with open(file,"r",encoding="utf-8") as r:
self.lines=[s.strip() for s in r if s.strip()!=""]
__len__=lambda self:len(self.lines)
__getitem__=lambda self,i:self.tokenizer(self.lines[i],truncation=True,
add_special_tokens=True,max_length=self.tokenizer.model_max_length-2)
trn=Trainer(args=arg,data_collator=DataCollatorForLanguageModeling(tkz),
model=DebertaV2ForMaskedLM(cfg),train_dataset=ReadLineDS("train.txt",tkz))
trn.train()
trn.save_model("deberta{}-{}".format(m,v))
tkz.save_pretrained("deberta{}-{}".format(m,v))'
test -d upos$M-$V || python3 -m esupar.train deberta$M-$V upos$M-$V .
test -f result$M-$V/result && continue
mkdir -p result$M-$V
for F in dev test
do cat $F.txt | python3 -c 'mdl,f="upos'$M-$V'","result'$M-$V/$F'.conllu"
import esupar
nlp=esupar.load(mdl)
with open(f,"w",encoding="utf-8") as w:
while True:
try:
doc=nlp(input().strip())
except:
quit()
print(doc,file=w)'
done
( echo '***' upos$M-$V dev
python3 $C -v dev.conllu result$M-$V/dev.conllu
echo '***' upos$M-$V test
python3 $C -v test.conllu result$M-$V/test.conllu
) | tee result$M-$V/result
done
done