Unigramトークナイザの最大トークン長と最大語彙数は係り受け解析に影響するのか
Unigramトークナイザにおける最大トークン長Mと最大語彙数Vが、UPOS/LAS/MLASにどう影響するか調査した。RoBERTaモデルの製作には、ja_gsdluw-ud-train.conlluの各文だけを用いている。
ja_gsdluw-ud-dev.conlluで評価
| V=4000 | V=8000 | V=16000 | V=32000 |
M=1 |
71.84/60.35/37.66 |
65.39/48.36/26.67 |
65.63/49.26/26.59 |
64.78/46.99/24.86 |
---|
M=2 |
75.61/63.24/40.35 |
75.15/62.03/39.72 |
76.96/65.79/43.60 |
72.70/58.64/35.72 |
---|
M=4 |
77.77/66.64/43.78 |
80.68/72.14/49.98 |
79.28/69.40/47.41 |
81.29/73.51/51.16 |
---|
M=8 |
78.80/68.19/46.37 |
79.19/69.54/47.43 |
80.15/70.56/49.09 |
81.95/73.51/52.03 |
---|
M=16 |
79.75/69.57/48.14 |
78.82/68.63/45.73 |
81.85/73.27/51.84 |
78.82/68.39/46.23 |
---|
ja_gsdluw-ud-test.conlluでテスト
| V=4000 | V=8000 | V=16000 | V=32000 |
M=1 |
69.43/58.10/34.31 |
62.70/45.39/23.18 |
62.93/45.87/23.87 |
62.62/44.87/22.93 |
---|
M=2 |
73.45/60.67/37.26 |
72.70/59.27/36.03 |
74.42/61.86/38.63 |
70.65/55.98/32.93 |
---|
M=4 |
76.01/64.43/41.46 |
79.09/68.90/46.54 |
76.91/66.30/42.58 |
78.68/68.95/46.83 |
---|
M=8 |
76.72/66.03/43.17 |
77.34/66.78/43.59 |
77.99/66.59/44.51 |
79.74/69.54/48.30 |
---|
M=16 |
77.72/67.85/44.96 |
77.40/66.59/43.83 |
79.92/69.48/48.46 |
76.85/64.32/41.98 |
---|
作業環境
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-GSDLUW
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 roberta$M-$V || python3 -c m,v=$M,$V'
from transformers import (RemBertTokenizerFast,RobertaConfig,RobertaForMaskedLM,
DataCollatorForLanguageModeling,TrainingArguments,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.Whitespace()
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=RemBertTokenizerFast(tokenizer_file="tokenizer.json",vocab_file="/dev/null",
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]",
model_max_length=512)
t=tkz.convert_tokens_to_ids(s)
cfg=RobertaConfig(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 ReadLineDataset(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=RobertaForMaskedLM(cfg),train_dataset=ReadLineDataset("train.txt",tkz))
trn.train()
trn.save_model("roberta{}-{}".format(m,v))
tkz.save_pretrained("roberta{}-{}".format(m,v))'
test -d upos$M-$V || python3 -m esupar.train roberta$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