NexteraBERT-Mezzoforte-220M-enがNexteraBERTForTokenClassificationをサポートしてくれたので、2025年12月7日の記事の手法を適用して、UD_English-EWTのUPOS品詞付与を試してみた。Google Colaboratory (GPU版)だと、こんな感じ。
!pip install transformers accelerate datasets evaluate vcs-versioning
!pip install seqeval --no-build-isolation
import json
s='$1=="transformers"{printf("-b v%s",$2)}'
!test -d transformers || git clone `pip list | awk '{s}'` https://github.com/huggingface/transformers
!test -d UD_English-EWT || git clone --depth=1 https://github.com/UniversalDependencies/UD_English-EWT
def makejson(conllu_file,json_file):
with open(conllu_file,"r",encoding="utf-8") as r, open(json_file,"w",encoding="utf-8") as w:
d,f={"tokens":["[CLS]"],"tags":["SYM"]},False
for s in r:
if s.strip()=="":
if len(d["tokens"])>1:
print(json.dumps(d),file=w)
d,f={"tokens":["[CLS]"],"tags":["SYM"]},False
else:
t=s.split("\t")
if len(t)==10 and t[0].isdecimal():
d["tokens"].append(" "+t[1] if f else t[1])
d["tags"].append(t[3])
f=t[9].find("SpaceAfter=No")<0
makejson("UD_English-EWT/en_ewt-ud-train.conllu","train.json")
makejson("UD_English-EWT/en_ewt-ud-dev.conllu","dev.json")
makejson("UD_English-EWT/en_ewt-ud-test.conllu","test.json")
!env WANDB_DISABLED=true python transformers/examples/pytorch/token-classification/run_ner.py --task_name pos --model_name_or_path RikkaBotan/NexteraBERT-Mezzoforte-220M-en --trust_remote_code --train_file train.json --validation_file dev.json --test_file test.json --output_dir ./NexteraBERT-Mezzoforte-220M-english-upos --do_train --do_eval --do_predict
私(安岡孝一)の手元では、35分ほどで以下のmetricsが出力されて、NexteraBERT-Mezzoforte-220M-english-uposが出来上がった。
***** train metrics *****
epoch = 3.0
total_flos = 1502568GF
train_loss = 0.0358
train_runtime = 0:36:07.03
train_samples = 12544
train_samples_per_second = 17.366
train_steps_per_second = 2.171
***** eval metrics *****
epoch = 3.0
eval_accuracy = 0.9822
eval_f1 = 0.9786
eval_loss = 0.0839
eval_precision = 0.9781
eval_recall = 0.9792
eval_runtime = 0:01:04.04
eval_samples = 2001
eval_samples_per_second = 31.242
eval_steps_per_second = 3.919
***** predict metrics *****
predict_accuracy = 0.9829
predict_f1 = 0.9797
predict_loss = 0.0833
predict_precision = 0.9785
predict_recall = 0.9809
predict_runtime = 0:00:37.06
predict_samples_per_second = 56.032
predict_steps_per_second = 7.014
eval・predictともにF1値が0.98弱で、まあまあいいセンである。ちょっと動かしてみよう。
from transformers import pipeline
nlp=pipeline("token-classification","NexteraBERT-Mezzoforte-220M-english-upos",trust_remote_code=True)
print(nlp("It don't mean a thing if it ain't got that swing"))
出来立てのNexteraBERT-Mezzoforte-220M-english-uposで「It don't mean a thing if it ain't got that swing」に品詞付与してみたところ、私の手元では以下の結果が得られた。
[{'entity': 'PRON', 'score': np.float32(0.99997663), 'index': 1, 'word': 'It', 'start': 0, 'end': 2}, {'entity': 'AUX', 'score': np.float32(0.9991898), 'index': 2, 'word': 'Ġdon', 'start': 2, 'end': 6}, {'entity': 'PART', 'score': np.float32(0.99990916), 'index': 3, 'word': "'t", 'start': 6, 'end': 8}, {'entity': 'VERB', 'score': np.float32(0.99993515), 'index': 4, 'word': 'Ġmean', 'start': 8, 'end': 13}, {'entity': 'DET', 'score': np.float32(0.999966), 'index': 5, 'word': 'Ġa', 'start': 13, 'end': 15}, {'entity': 'NOUN', 'score': np.float32(0.9999517), 'index': 6, 'word': 'Ġthing', 'start': 15, 'end': 21}, {'entity': 'SCONJ', 'score': np.float32(0.99994886), 'index': 7, 'word': 'Ġif', 'start': 21, 'end': 24}, {'entity': 'PRON', 'score': np.float32(0.9999789), 'index': 8, 'word': 'Ġit', 'start': 24, 'end': 27}, {'entity': 'AUX', 'score': np.float32(0.99514455), 'index': 9, 'word': 'Ġain', 'start': 27, 'end': 31}, {'entity': 'PART', 'score': np.float32(0.99987876), 'index': 10, 'word': "'t", 'start': 31, 'end': 33}, {'entity': 'VERB', 'score': np.float32(0.99993), 'index': 11, 'word': 'Ġgot', 'start': 33, 'end': 37}, {'entity': 'DET', 'score': np.float32(0.9999672), 'index': 12, 'word': 'Ġthat', 'start': 37, 'end': 42}, {'entity': 'NOUN', 'score': np.float32(0.99998355), 'index': 13, 'word': 'Ġswing', 'start': 42, 'end': 48}]
うーん、空白が「Ġ」に化けてしまっているのは、まあ何とかするとしても、「don't」と「ain't」がイマイチだ。UD_English-EWTでは「don't」は「do」「n't」に、「ain't」は「ai」「n't」に切って、AUXとPARTを品詞付与するのが流儀なのだが、上の結果では、単語の切れ目が1文字ズレてしまっている。このあたり、ModernBERTForTokenClassificationではトークナイザの改造に踏み切ったのだけど、NexteraBERTForTokenClassificationではどうしようかなあ。