yasuokaの日記: JGLUEのJSQuADをtransformers 4.19.2のDebertaV2TokenizerFastでムリヤリ動かすには
一昨日の日記で、私(安岡孝一)は以下のように書いた。
でも、JGLUEのタスクのうち、JSQuADはDebertaV2TokenizerFastを受け付けてくれないようだ。
この問題を解決すべく、JSQuADのtrain-v1.0.jsonを眺めていたのだが、妙な点があって悩ましい。たとえばid:a3097844p53q2は、contextが"バラク・オバマ [SEP] 2014年12月17日にオバマとカストロは国交正常化交渉の開始を電撃発表した。数か月以内に大使館を開設し、銀行や通商関係の正常化を話し合うことでも合意した。『』はこの雪解けを「オバマ政権の最大の外交成果」だと位置づけている。"、questionが"アメリカとキューバの国交正常化交渉の開始が発表されたのはいつ?7"となっているのだが、この[SEP]はsep_tokenだとみなしていいのだろうか。あるいは末尾の7は、どう扱うべきなのだろうか。
このあたりがイマイチわからないので、自信がないまま、transformers 4.19.2のrun_qa.py向けに変換してみた。Google Colaboratory (GPU)だと、以下のような感じ。
!test -d transformers-4.19.2 || git clone -b v4.19.2 --depth=1 https://github.com/huggingface/transformers transformers-4.19.2
!test -d JGLUE || ( git clone --depth=1 https://github.com/yahoojapan/JGLUE && cat JGLUE/fine-tuning/patch/transformers-4.9.2_jglue-1.0.0.patch | ( cd transformers-4.19.2 && patch -p1 ) )
!cd transformers-4.19.2 && pip install .
!pip install -r transformers-4.19.2/examples/pytorch/text-classification/requirements.txt
!pip install protobuf==3.19.1 tensorboard
import json
for f in ["train-v1.0.json","valid-v1.0.json"]:
with open("JGLUE/datasets/jsquad-v1.0/"+f,"r",encoding="utf-8") as r:
j=json.load(r)
u=[]
for d in j["data"]:
for p in d["paragraphs"]:
for q in p["qas"]:
u.append({"id":q["id"],"title":d["title"],"context":p["context"],"question":q["question"],"answers":{"text":[x["text"] for x in q["answers"]],"answer_start":[x["answer_start"] for x in q["answers"]]}})
with open(f,"w",encoding="utf-8") as w:
json.dump({"data":u},w,ensure_ascii=False,indent=2)
!python transformers-4.19.2/examples/pytorch/question-answering/run_qa.py --model_name_or_path KoichiYasuoka/deberta-base-japanese-aozora --do_train --do_eval --max_seq_length 384 --learning_rate 5e-05 --num_train_epochs 3 --per_device_train_batch_size 16 --per_device_eval_batch_size 16 --output_dir ./output_jsquad2 --overwrite_output_dir --train_file train-v1.0.json --validation_file valid-v1.0.json --save_steps 5000 --warmup_ratio 0.1
このプログラムでdeberta-base-japanese-aozoraをファインチューニングしてみたところ、GPUでも4時間ほど要した上、私の手元では以下の「eval metrics」が出力された。
***** eval metrics *****
epoch = 3.0
eval_exact_match = 74.9887
eval_f1 = 75.3574
eval_samples = 4493
JSQuADが、EM/F1=0.7499/0.7536なので、昨日の日記のBertJapaneseTokenizerより、DebertaV2TokenizerFastの方が性能がいいということになる。ただ、こういう対応の仕方がベンチマークとして正しいのかどうか、私自身よく分からない。うーん、このあたり全部を、ちゃんとJGLUEでサポートしてほしいなあ。
JGLUEのJSQuADをtransformers 4.19.2のDebertaV2TokenizerFastでムリヤリ動かすには More ログイン