Unigramトークナイザの最大トークン長と最大語彙数は係り受け解析に影響するのか
Unigramトークナイザにおける最大トークン長Mと最大語彙数Vが、UPOS/LAS/MLASにどう影響するか調査した。RoBERTaモデルの製作には、zh_gsdsimp-ud-train.conlluの各文だけを用いている。
zh_gsdsimp-ud-dev.conlluで評価
| V=4000 | V=8000 | V=16000 | V=32000 |
M=1 |
52.71/27.83/17.01 |
52.57/27.46/17.03 |
52.88/27.44/17.01 |
52.89/28.01/17.45 |
---|
M=2 |
53.15/28.05/17.28 |
52.50/27.64/16.87 |
52.99/27.29/16.98 |
52.78/27.61/17.32 |
---|
M=4 |
52.94/27.68/17.10 |
53.05/27.57/16.91 |
53.19/27.87/17.35 |
52.54/27.45/16.55 |
---|
M=8 |
52.69/27.20/16.63 |
52.71/27.48/16.71 |
52.78/27.72/16.94 |
53.00/27.99/16.78 |
---|
M=16 |
53.00/27.77/17.12 |
52.96/27.87/17.53 |
52.98/27.71/17.07 |
53.09/27.19/16.93 |
---|
zh_gsdsimp-ud-test.conlluでテスト
| V=4000 | V=8000 | V=16000 | V=32000 |
M=1 |
53.95/30.44/19.13 |
53.71/30.57/18.99 |
53.27/29.84/18.44 |
53.96/30.17/19.11 |
---|
M=2 |
53.98/29.96/18.98 |
53.89/30.37/19.59 |
54.51/30.71/19.73 |
53.99/30.10/19.52 |
---|
M=4 |
54.25/30.12/19.26 |
54.02/30.17/19.06 |
54.50/30.41/19.41 |
54.26/30.48/19.48 |
---|
M=8 |
53.95/29.79/19.32 |
53.77/30.23/19.06 |
54.12/29.88/19.00 |
54.77/30.91/19.99 |
---|
M=16 |
53.92/30.15/19.43 |
54.15/30.54/19.34 |
54.31/30.10/18.99 |
54.35/30.59/19.41 |
---|
作業環境
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_Chinese-GSDSimp
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