tossy diary

日常の記録を残す

BERTで固有表現抽出を行う際の特殊トークン'X'について

BERTで固有表現抽出を行う際に、BERTでの特殊トークン'X'を学習時に使う場合があった。

固有表現抽出を行う際に特殊トークン'X'を学習に利用していた

固有表現抽出では、単語ごとにラベリングをして、ラベルに基づきモデルが学習を行う。 BERTへ文章を入力する時に、一般的にサブワード化が行われるが、このサブワード化により、単語がトークンの単位になると、単語ごとに付与したラベルがずれてしまうケースが発生する。 上記の理由から、About 'X' label#1のissueやBERTのv1の論文にも記載されている通り、最初は学習時に'X'は必要という話だった。 例えば、以下のようにラベル付けをしたとする。

Jim Henson was a puppeteer
B-PER I-PER  O O O 

上記の文をサブワード化すると、ラベリングした単語が分割されてしまう。

以下の例では、サブワード化した際の##のトークンには、元のラベルをそのまま使用している。

Jim Hen ##son was a puppet ##eer
B-PER I-PER I-PER O O O O

この場合、当初には意図していない「##son」と「I-PER」というラベルや「##eer」と「O」というラベルが学習に使われてしまう。

そこで、単語の最初のトークンにはオリジナルのラベルを残し、その単語のサブワードされた##のトークンには'X'というラベルを使うことがBERTの原著論文v1の中でも挙げられている。 具体的には、4.3 NERの章に記載されている。

sub-token as input to the classifier. For example:
Jim Hen ##son was a puppet ##eer
I-PER I-PER X O O O X
Where no prediction is made for X. Since
the WordPiece tokenization boundaries are a
known part of the input, this is done for both
training and test. A visual representation is also
given in Figure 3 (d). A cased WordPiece model
is used for NER, whereas an uncased model is
used for all other tasks.

これを読むと、WordPieceによりtokenizeされた先頭以外、'X'は予測しないということが書かれている。

現在は特殊トークン'X'は利用されていない

一方、現在最新であるBERTの原著論文v2にはこの記載は消えているように思える。

v2のBERT論文を見ると、'X'を使わずに、「文書情報を使う」ことで'X'を使う場合と同等の性能を得た、と書かれている。

BERTの原著論文v2には以下のような記載がある。

 5.3 Feature-based Approach with BERT
 We use the representation of the first sub-token as the input to the token-level classifier over the NER label set.

5.3章の中段あたりに、サブワードの最初のトークンにラベルをセットする、と書かれている。'X'との記載もないため、現在は'X'を使っていない。

また、さきほどのissueにも、'X'を使わずにサブワードの最初の単語のみをfine-tuningで学習することでモデルがパターンを学習できるとしている。

For example After extracting features for the sentence below Jim Hen ##son was a puppet ##eer You're giving only [Jim , Hen , was , a, puppet] hidden states to linear classification layer ?

Yes. Because I think the fine tune bert could learn this pattern.

参考:https://github.com/kamalkraj/BERT-NER/issues/1#issuecomment-474266192

また、以下の記事にも'X'の言及がされている。

BERT uses WordPiece tokenization rather than whole-word tokenization (although there are whole words in its vocabulary). So if you have a word like “personally,” it may be broken up into “person” “##al” “##ly.” You now have a list of WordPieces which is much longer than, and misaligned with, your list of labels. You as the practitioner need to figure out how to handle cases where you have one tag per word, but some words get broken up into several chunks. I wrote a function called tokenize_and_preserve_labels to propagate a word’s original label to all of its pieces during tokenization. Other folks who implemented this have also used the following heuristic: leave the original label on the first token of the word, then use the label “X” for subwords of that word. Either method seems to result in equally good model performance.

まとめ

BERTで固有表現抽出を行う際の特殊トークン'X'は、モデルのfine-tuning時に過去使われていた。 現在は使われていないようだ。