yasuokaの日記: Re: roberta-base-japanese-with-auto-jumanppのトークナイザはsentencepieceを必要としないのか 1
昨日の日記の続きだが、現時点のroberta-base-japanese-with-auto-jumanppは、どうもvocab.txtが壊れているようだ。とりあえず、そのあたりを報告しつつ、AlbertTokenizerの力を借りてvocab.txtを直す方法を考えてみた。Google Colaboratoryで動かしてみよう。
!test -d jumanpp-2.0.0-rc3 || curl -L https://github.com/ku-nlp/jumanpp/releases/download/v2.0.0-rc3/jumanpp-2.0.0-rc3.tar.xz | tar xJf -
!test -x /usr/local/bin/jumanpp || ( mkdir jumanpp-2.0.0-rc3/build && cd jumanpp-2.0.0-rc3/build && cmake .. -DCMAKE_BUILD_TYPE=Release && make install )
!pip install transformers pyknp sentencepiece
from transformers import AlbertTokenizer,BertJapaneseTokenizer,AutoModelForMaskedLM,FillMaskPipeline
from transformers.utils import cached_file
tkz=AlbertTokenizer(cached_file("nlp-waseda/roberta-base-japanese-with-auto-jumanpp","spiece.model"))
with open("vocab.txt","w",encoding="utf-8") as w:
print("\n##".join(tkz.convert_ids_to_tokens(range(len(tkz)))).replace("\n##[","\n[").replace("\n##<","\n<").replace("\n##\u2581","\n"),file=w)
tokenizer=BertJapaneseTokenizer.from_pretrained("nlp-waseda/roberta-base-japanese-with-auto-jumanpp",vocab_file="vocab.txt")
model=AutoModelForMaskedLM.from_pretrained("nlp-waseda/roberta-base-japanese-with-auto-jumanpp")
fmp=FillMaskPipeline(model=model,tokenizer=tokenizer)
print(fmp("国境の[MASK]トンネルを抜けると雪国であった。"))
vocab.txtを直して、「国境の[MASK]トンネルを抜けると雪国であった。」の[MASK]を穴埋めさせてみたところ、私(安岡孝一)の手元では以下の結果になった。
[{'score': 0.16705404222011566, 'token': 2244, 'token_str': '地 下', 'sequence': '国境 の 地下 トンネル を 抜ける と 雪国 であった 。'}, {'score': 0.14420612156391144, 'token': 2309, 'token_str': '長 い', 'sequence': '国境 の 長い トンネル を 抜ける と 雪国 であった 。'}, {'score': 0.02641996741294861, 'token': 509, 'token_str': '北', 'sequence': '国境 の 北 トンネル を 抜ける と 雪国 であった 。'}, {'score': 0.023077642545104027, 'token': 526, 'token_str': '南', 'sequence': '国境 の 南 トンネル を 抜ける と 雪国 であった 。'}, {'score': 0.0190455112606287, 'token': 577, 'token_str': '山', 'sequence': '国境 の 山 トンネル を 抜ける と 雪国 であった 。'}]
2番目に「長い」が出てくる。これなら大丈夫だと思う。でも、本当にsentencepieceじゃなくて、WordpieceTokenizerでいいのかしら。
vocab.txtが無事に修正 (スコア:2)
された [huggingface.co]ようです。これで、以下のGoogle Colaboratory向けプログラムが「正しく」動くようになりました。