SlideShare uma empresa Scribd logo
1 de 56
Baixar para ler offline
BERTology のススメ
BERT fine-tuning のすすめ
筑波⼤学⼤学院 ビジネス科学研究科 講師
Tomohiko HARADA
※ 本資料の著作権は,引⽤元の論⽂および記事に準じます
2019/9/9 BERTology のススメ 1
Goal
• BERT について,⼈より知ってる気分になる
• BERT の fine-tuning について, やればできる気分になる
2019/9/9 BERTology のススメ 2
Agenda
• はじめに
• BERT 解説
• BERT とは
• Transformer
• BERT fine-tuning
• BERT fine-tuning の実践
• テキスト分類 Classifier
• テキスト検索 Ranking
• タグ付け NER
• BERTology 論⽂
2019/9/9 BERTology のススメ 3
2018年10⽉: BERT の衝撃
• タスクに特化した構造を持たずに,⼈間のスコアを⼤きく超えた
2019/9/9 BERTology のススメ 4
⼈間
BERT
https://rajpurkar.github.io/SQuAD-explorer/
⽂献[1]
• 機械読解タスク(左)で,完全⼀致
と部分⼀致の両指標で最⾼精度
(2018/10/5)
• 様々な⾃然⾔語理解タスクで
SOTA (QA,含意,⾔い換え,NER等)
• タスク適応は,出⼒層をタスク毎
に 1層のみ追加してfine-tuning
GLUE benchmark Results
• 9つの⾃然⾔語理解タスクでSOTA (2018/10 時点)
2019/9/9 BERTology のススメ 5
Table 1: GLUE Test results, scored by the GLUE evaluation server (https://gluebenchmark.com/leaderboard).
MNLI: テキスト同⼠の関連性(含意・⽭盾・中⽴)を判定
QQP: 2つの質問が同じ意味かを判定
QNLI: ⽂章内に質問の回答が含まれているかを判定
SST-2: 映画のレビューを基に感情分析し,良し悪しを判定
CoLA: ⽂章が⽂法的に正しいかを判定
STS-B: 2つのニュース⾒出しの意味的類似性を判定
MRPC: 2つのニュース記事が意味的に等価かを判定
RTE: 2つの⽂章が含意関係かどうかを判定
⽂献[2][3]
⼿元のデータで簡易実験
• Scale Movie Review Dataset (rt-polarity): pos/neg 2値
2019/9/9 BERTology のススメ 6
77 77 - 5: 6 5: 6 - .5: 6 .5: 6 - 9 77 .8 2471 7473
7871
358 1
.19 16.100473
-
- 9- , 9 85-.
https://github.com/haradatm/nlp/tree/master/bert/classify
BERTとは
• 双⽅向 Transformer ブロックを24層重ねた⾔語モデル
• 事前学習モデルが公開
2019/9/9 BERTology のススメ 7
*1 https://github.com/google-research/bert
*2 http://nlp.ist.i.kyoto-u.ac.jp/index.php?BERT⽇本語Pretrainedモデル
• 英語
• 本家 Google の事前学習モデル *1
• Book Corpus 8億語 + 英語
Wikipedia 25億語 (語彙数 3万)
• ⽇本語
• ⿊橋研の事前学習モデル *2
• ⽇本語Wikipedia 約1,800万⽂ (語
彙数 3.2万)
⽂献[2][4]
⾔語モデルの Pre-train
• ⾃然⾔語処理の応⽤タスクは学習データが多くない(数⼗万件程度)
ため,⼤規模コーパスから学習したモデルを転移することが流⾏
• ELMo (2018/02に発表), OpenAI GPT (2018/06に発表)
• ⾔語モデルは LTR あるいは RTL の次単語を予測するのが普通
2019/9/9 BERTology のススメ 8
ELMo
left-to-right & right-to-left LSTMs
OpenAI GPT
left-to-right Transformer
BERT
双⽅向 Transformer
⽂献[1][2]
Transformer
• RNNやCNNを使わずアテンションのみ使⽤したTransformerが
ニューラル機械翻訳で圧倒的な SOTA を達成
• 従来,単語系列の⽂脈理解は主にLSTM →⻑期依存性の理解に限界
• 離れた単語の関係性も直接考慮できる Self-Attention が性能向上に
⼤きく寄与した (しかも省メモリで計算可)
2019/9/9 BERTology のススメ 9
LSTM
遠く離れた単語の関係性
を捕まえにくい
Self-Attention
遠く離れた単語も
直接関係性を考慮できる
好き
好き な 動物 は
な 動物 は ・・・
好きな動物は
Softmax
⽂献[1][5]
Self-Attention
• Self-Attention は,⽂構造や照応関係を獲得するという⾒⽅
2019/9/9 BERTology のススメ 10
← アテンションの重みに従って value から情報を引き出す
• Self-Attentionでは Query, Key Value ともに同じコン
テキストから⽣成されたベクトル
↓ “its” が “Law” を指す(照応関係)を獲得
⽂献[5]
Query
好き
Query
好き
Query
好き
Query
好き
Key1
好き
Key2
な
Key2
動物
Key3
は
0.1✖
0.1✖
0.7✖
0.1✖
Softmax
Key1
好き
Key2
な
Key2
動物
Key3
は
+
Attention OutputQuery好き
Transformer
• http://nlp.seas.harvard.edu/2018/04/03/attention.html
2019/9/9 BERTology のススメ 11
⽂献[6]
• 論⽂の注釈付きで,実装に沿って
Transformer を解説したサイト
• Jupyter および Google Colab
のノートブックも公開
• BERT や Transformer の初学者に
オススメ
Transformer
• 例: レイヤーN=6, ヘッドh=8, ⻑さ=512, 中間層=768
2019/9/9 BERTology のススメ 12
Multi-Head AttentionScaled Dot-Product Attention
⽂献[5]
BERT Task
• 隣接する⽂/ランダムな⽂をセパレータ([SEP])で繋げて,隣接する
⽂か否かの判定([→CLS])を⽳埋め予測(→[MASK])と同時に学習
2019/9/9 BERTology のススメ 13
[MASK] [MASK]
⽂献[2]
BERT Training
• Encoder 出⼒のすぐ上に分類レイヤーを追加→予測結果を学習
2019/9/9 BERTology のススメ 14
w1 w2 w3 [MASK] w5
w1 w2 w3 w4 w5
w’1 w’2 w’3 w’4 w’5
[CLS]
Token + Segment + Position Encoder
Encoder (Transformer の左側)
O1 O2 O3 O4 O5
全結合レイヤー (O0→2値, O4→語彙数)
Softmax (→確率値)
O0
IsNextTransformer Encoder
⽂献[2]
BERT Pre-train モデル
2019/9/9 BERTology のススメ 15
モデル
英語 モデル
⽇本語 モデル
Base Large
公開元 Google Research *1 京⼤ ⿊橋研 *2
コーパス
Book Corpus 8億ワード +
英語Wikipedia 25億 ワード
⽇本語Wikipedia 約1,800万⽂
(半⾓を全⾓に正規化)
語彙 (サブワード含む) 30K (WordPiece) 32K (Juman++ & BEP)
⼊⼒⻑ 最⼤ 512トークン *3 最⼤ 128トークン *3
パラメタ 12層, 各層768次元 24層, 各層1024次元 12層, 各層768次元 (Base と同じ)
学習時間
4 Cloud TPUs で
4⽇間(≒100時間)
16 Cloud TPUs で
4⽇間(≒100時間)
1GPU (GTX 1080 Ti) で
約30⽇間(≒750時間)
*1 https://github.com/google-research/bert
*2 http://nlp.ist.i.kyoto-u.ac.jp/index.php?BERT⽇本語Pretrainedモデル
*3 ⼊⼒できるシーケンスの⻑さに制限があることに注意
⽂献[2][4]
BERT fine-tuning
• 出⼒層を1層追加してfine-tuningするのみで各タスクに適応
2019/9/9 BERTology のススメ 16
w1 w2 [SEP] W4 [SEP][CLS]
Token + Segment + Position Encoder
Encoder (Transformer の左側)
O1 O2 O3 O4 O5O0
BERTModel
タスクにあわせて,1層追加する
(a)⽂ペアの分類 (b)単⽂の分類 (c)スパン抽出 (d)タグ付け
単⽂, ⽂ペア, QA 等
(a) ⽂ペアの分類 (b) 単⽂の分類
(c) スパン抽出 (例:読解) (d) タグ付け (例:NER)
⽂献[2]
BERT fine-tuning
• 出⼒層(ベクトル)は, BertModel のサービス関数で取り出せる
2019/9/9 BERTology のススメ 17
w1 w2 [SEP] W4 [SEP][CLS]
Token + Segment + Position Encoder
Encoder (Transformer の左側)
O1 O2 O3 O4 O5O0
タスクにあわせて,1層追加する
(a)⽂ペアの分類 (b)単⽂の分類 (c)スパン抽出 (d)タグ付け
単⽂, ⽂ペア, QA 等
class BertModel(chainer.Chain):
def __init__(self, config):
:
def __call__(self,
:
def get_pooled_output(self,
input_ids, input_mask=None, token_type_ids=None):
return self.__call__(input_ids, input_mask, token_type_ids)
def get_sequence_output(self,
input_ids, input_mask=None, token_type_ids=None):
return self.__call__(input_ids, input_mask, token_type_ids,
get_sequence_output=True)
def get_embedding_output(self,
input_ids, input_mask=None, token_type_ids=None):
return self.__call__(input_ids, input_mask, token_type_ids,
get_embedding_output=True)
学習済みの埋め込みを取り出す
各トークンのベクトルを取り出す
[CLS] のベクトルを取り出す
⽂献[2]
BERT fine-tuning: テキスト分類 (a)(b)
• ⽂ペアまたは単⽂の分類: 全結合層を1層追加してfine-tuning
• ⼊⼒: 先頭に [CLS], ⽂と⽂の境界には [SEP] トークンを追加
2019/9/9 BERTology のススメ 18
class BertClassifier(chainer.Chain):
def __init__(self, bert, num_labels):
:
def __call__(self, input_ids, input_mask, token_type_ids, labels):
output_layer = self.bert.get_pooled_output(input_ids, …)
output_layer = F.dropout(output_layer, 0.1)
logits = self.output(output_layer)
loss = F.softmax_cross_entropy(logits, labels)
return loss
(b) 単⽂の分類
Class label
Sentence 1 Sentence 2
(a) ⽂ペアの分類
(a) Sentence Pair Classification Tasks:
MNLI, QQP, QNLI, STS-B, BRPC, RTE,
SWAG
Class label
Single Sentence
(b) Single Sentence Classification Task:
MSST-2, CoLA
⽂献[2]
(b) 単⽂の分類
BERT fine-tuning: タグ付け (d)
• NER: 全結合層を1層追加してfine-tuning
• ⼊⼒: 先頭に [CLS], Qとパラグラフの境界には [SEP] トークンを追加
2019/9/9 BERTology のススメ 19
(d) Single Sentence Tagging Tasks: CoNLL-2003 NER
O B-PER ・・・ O
Single Sentence
class BertNER(chainer.Chain):
def __init__(self, bert):
:
def __call__(self, input_ids, input_mask, token_type_ids, labels):
final_hidden = self.bert.get_sequence_output(input_ids, …)
:
logits = self.output(final_hidden_matrix)
:
loss = F.softmax_cross_entropy(logits, labels, ignore_label=0)
return loss
(リサイズ等)
(リサイズ等)
⽂献[2]
BERT fine-tuning: スパン抽出 (c)
• SQuAD: 全結合層を1層追加してfine-tuning
• ⼊⼒: 先頭に [CLS], Qとパラグラフの境界には [SEP] トークンを追加
2019/9/9 BERTology のススメ 20
Start/End Span
Question Paragraph
(c) Question Answering Tasks: SQuAD v1.1
class BertSQuAD(chainer.Chain):
def __init__(self, bert):
:
def __call__(self, input_ids, input_mask, token_type_ids):
final_hidden = self.bert.get_sequence_output(input_ids, …)
:
logits = self.output(final_hidden_matrix)
:
unstacked_logits = F.separate(logits, axis=0)
(start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])
start_loss = F.softmax_cross_entropy(start_logits, start_positions)
end_loss = F.softmax_cross_entropy(end_logits, end_positions)
total_loss = (start_loss + end_loss) / 2.0
return total_loss
(リサイズ等)
(リサイズ等)
⽂献[2]
解説 BERT fine-tuning: スパン抽出 (c)
• タグ付け(d): T ∈ RK×H, W ∈ RK×H, H=768, K=2 の場合
• スパン抽出(c): T ∈ RK×H, Start ∈ RH, End ∈ RH, H=768, K=2 {Start,End}
2019/9/9 BERTology のススメ 21
tii w1,1 w1,2
ti2 w2,1 w2,2 y1 = ti1・w1,1 + ti2・w2,1 + ti3・w3,1 +・・・ ti768・w768,1
ti3 w3,1 w3,2 y2 = ti1・w1,2 + ti2・w2,2 + ti3・w3,2 +・・・ ti768・w768,2
:
:
ti768 w768,1 w768,2
T (H=768次元) WT (HxK) Y (K=2次元)
ti1 ti2 ti3
・・ ・・ ti768 dot w1,1 w1,2 = y1 y2
w2,1 w2,2 クラス1 クラス2
w3,1 w3,2
: :
: :
w768,1 w768,2
T (H=768次元) ST
, ET
(HxK) Y (K=2次元)
ti1 ti2 ti3
・・ ・・ ti768 dot w1,1 w1,2 = y1 y2
w2,1 w2,2 Start End
w3,1 w3,2
: :
: :
w768,1 wh2
softmax(S・Ti) ⇒ S・Ti
同様に
softmax(E・Ti) ⇒ E・Ti
ti : i番⽬のトークンの出⼒ベクトル
⽂献[2]
解説 BERT fine-tuning: スパン抽出 (c)
• タグ付け(d): T ∈ RK×H, W ∈ RK×H, H=768, K=2 の場合
• スパン抽出(c): T ∈ RK×H, Start ∈ RH, End ∈ RH, H=768, K=2 {Start,End}
2019/9/9 BERTology のススメ 22
トークン1 ・・
→
全結合層
→
Softmax
(列⽅向)
トークン2
トークン3
:
トークンN
H=768 次元 クラス数=K次元 クラス数=K次元
H=768 次元
Start/End
=2次元
トークン1 ・・
→
全結合層
→
Softmax
(⾏⽅向)
→
Softmax
(⾏⽅向)
トークン2
トークン3
:
トークンN
Startかどうか
=1次元
Endかどうか
=1次元
BERT まとめ
• 2018年に Google AI が発表し,話題を席巻した 汎⽤⾔語モデル
→ 双⽅向 Transformer ブロックを24層重ねたモデル
• ⾔語モデルを⼤規模コーパスで事前学習し,出⼒層をタスクに応
じた1層のみ追加して fine-tuning
• fine-tuning モデルは,タスクに特化した構造を持たずに,様々な
タスクで汎⽤的かつ良好な性能
• 発表以来,派⽣⼿法や応⽤タスクへの適⽤等の研究(BERTology)
が盛んになり, 引⽤数は1,276 に上る ※ 2019/09/08 時点
2019/9/9 BERTology のススメ 23
BERT fine-tuning の実践
以降では chainer を想定しています
2019/9/9 BERTology のススメ 24
fine-tuning 実装例と実験
• https://github.com/haradatm/nlp/tree/master/bert
2019/9/9 BERTology のススメ 25
テキスト分類: rt-polarity(英), 国交省不具合DB(⽇) での実験
テキスト検索: MedNLP 患者表現辞書(⽇) での実験
タグ付け: CoNLL-2013 (英) での追実験
fine-tuning 実装例と実験
•
2019/9/9 BERTology のススメ 26
bert-chainer https://github.com/soskek/bert-chainer
• BERT の chainer 実装が公開されている
2019/9/9 BERTology のススメ 27
bert-chainer https://github.com/soskek/bert-chainer
• Pre-train モデルの TensorFlow→chainer (.npz) 変換が必要
2019/9/9 BERTology のススメ 28
Python コードの配置
• BERT 実装の中から,fine-tuningに必要な 3ファイルを集める
2019/9/9 BERTology のススメ 29
右: https://github.com/haradatm/nlp/tree/master/bert/classify左: https://github.com/soskek/bert-chainer
← Pre-train モデルはここ
テキスト分類: 実装
• train_bert.py : input_ids, token_type_ids, input_mask は以下の通り
2019/9/9 BERTology のススメ 30
詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify
• input_ids
1. FullTokenizer.tokenize() で
⼊⼒⽂をトークン化
2. FullTokenizer.convert_
tokens_to_ids() で ID に変換
してセット
• token_type_ids
すべて同じ=0 をセット
• input_mask
すべて同じ=1 をセット
※ FullTokenizer は,
tokenization.py が提供
分類結果
全結合 + Softmax
O[CLS]
Encoder (Transformer の左側)
get_pooled_output()
input_ids
w1 w2 → sub21+ sub22[CLS] [SEP]
101 102 1030
0 0 00
1 1 11
1
0
1
104 105
0 0
1 1
w3 w4
token_type_ids
input_mask
タイプ(b): 単⽂の分類
→ [CLS] のベクトルを取り出す
→ get_pooled_output()
テキスト分類: 実装
• train_bert.py : BERT 実装の run_classifier.py とほぼ同じ
2019/9/9 BERTology のススメ 31
https://github.com/haradatm/nlp/tree/master/bert/classify
← Pre-train モデルはここ
class BertClassifier(chainer.Chain):
def __init__(self, bert, num_labels):
:
def __call__(self, input_ids, input_mask, token_type_ids, labels):
output_layer = self.bert.get_pooled_output(input_ids, …)
output_layer = F.dropout(output_layer, 0.1)
logits = self.output(output_layer)
loss = F.softmax_cross_entropy(logits, labels)
return loss
def main():
:
bert_config = BertConfig.from_json_file(bert_config_file)
tokenizer = FullTokenizer(vocab_file=vocab_file, do_lower_case=True)
:
bert = BertModel(config=bert_config)
model = BertClassifier(bert, num_labels=len(labels))
chainer.serializers.load_npz(
init_checkpoint, model, ignore_names=['output/W', 'output/b'])
詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify
タイプ(b): 単⽂の分類
→ [CLS] のベクトルを取り出す
→ get_pooled_output()
テキスト分類: 実験データ
• Datasets
• 英語: Scale Movie Review Dataset (rt-polarity) →Pos/Neg 2クラス
• https://www.cs.cornell.edu/people/pabo/movie-review-data/
• 全 51,940 件 → 学習 46,743, 評価 5,197 に分割
• ⽇本語: 国交省「⾃動⾞の不具合情報」(mlit)→不具合分類 16クラス
• http://carinf.mlit.go.jp/jidosha/carinf/opn/index.html
• クロール&スクレイピングした 全10,662 件 → 学習 9,596, 評価 1,066 に分割
• テキストは,あらかじめ Juman++ で分かち書き
• BERT モデル
• 英語: Google AI, uncased_L-12_H-768_A-12
• ⽇本語: ⿊橋研, Japanese_L-12_H-768_A-12_E-30_BPE
2019/9/9 BERTology のススメ 32
詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify
テキスト分類: 実験結果
• 英(rt-polarity)⽇(mlit)の両⽅で精度向上, 特に英語が効果⼤
2019/9/9 BERTology のススメ 33
66
66
-::
49:5
49:5
-::
.49:
5
.49:
5
-::
8 66 .7
1360
: 636
2
6760
247 0
.08:
-
- 8- , 8: 74-.
.55
.55
,99
3894
3894
,99
-389
4
-389
4
,99
7.55 -6
0 5
9:5 5
1
565
136
- 79
,..
,..:7,. 43 9 .63,-
詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify
テキスト検索: 実装
• train_patient-triplet.py : input_ids, token_type_ids, input_mask は以下の通り
2019/9/9 BERTology のススメ 34
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking
• input_ids
1. FullTokenizer.tokenize() で
⼊⼒⽂をトークン化
2. FullTokenizer.convert_
tokens_to_ids() で ID に変換
してセット
• token_type_ids
すべて同じ=0 をセット
• input_mask
すべて同じ=1 をセット
※ FullTokenizer は,
tokenization.py が提供
クエリ, 正例, 負例 の 3つの Triplet loss で学習
タイプ(b): 単⽂の分類
→ [CLS] のベクトルを取り出す
→ get_pooled_output()
Encoder (Transformer の左側)
get_pooled_output()
input_ids 101 102 1030
0 0 00
1 1 11
1
0
1
104 105
0 0
1 1
token_type_ids
input_mask
w1 w2 → sub21+ sub22[CLS] [SEP]w3 w4
クエリ
正例
負例
O[CLS]
全結合 (次元数の変更)
テキスト検索: 実装
• train_patient-triplet.py : クエリ-正例-負例 の距離を学習
2019/9/9 BERTology のススメ 35
https://github.com/haradatm/nlp/tree/master/bert/ranking class BertRanking(chainer.Chain):
def __init__(self, bert, num_labels):
:
def forward(self, input_ids, input_mask, token_type_ids, labels):
output_layer = self.bert.get_pooled_output(input_ids, …)
output_layer = F.dropout(output_layer, 0.1)
output_layer = self.output(output_layer)
return output_layer
def __call__(self, q_x1, q_x2, q_x3, d_x1, d_x2, d_x3, n_x1, n_x2, n_x3):
y_0 = self.forward(q_x1, q_x2, q_x3)
y_1 = self.forward(d_x1, d_x2, d_x3)
y_2 = self.forward(n_x1, n_x2, n_x3)
loss = F.triplet(y_0, y_1, y_2)
return loss
← Pre-train モデルはここ
def main():
:
bert_config = BertConfig.from_json_file(bert_config_file) …
(テキスト分類と同じ)
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking
タイプ(b): 単⽂の分類
→ [CLS] のベクトルを取り出す
→ get_pooled_output()
テキスト検索: 実験データ
• Datasets
• ⽇本語: MedNLP「患者表現辞書」→不具合分類 16クラス
患者が実際に⽤いる病名表現を網羅した辞書
• http://sociocom.jp/~data/2019-pde/
• 患者表現-標準病名ペア 8,483件 → 学習 7,483, 評価 1,000 に分割 (重複なし)
• ペアを正例, 負例はペアでない組み合わせをランダムに選択
• テキストは,あらかじめ Juman++ で分かち書き
• BERT モデル
• ⽇本語: ⿊橋研, Japanese_L-12_H-768_A-12_E-30_BPE
2019/9/9 BERTology のススメ 36
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking
テキスト検索: 実験結果
• 表層が⼀致しない検索も, fine-tuning で「ある程度」検索可能
2019/9/9 BERTology のススメ 37
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking
+(
-(
) -
)+
)
(
)
+
1 ) 024
9 6988 @
6 ) 69B
9 6988 @
69B @9 E@9
B 9
3 5B 395B7 29 E
5 . @ 78.
% %%
% %%
% %%
(% %%
)% %%
% %%
+% %%
% %%
-% %%
03
03
(
03
)
03
03
+
03
03
-
03
.
03
%
03
8 03 11 1
07 03 37032 03 4583 83 5963
テキスト検索: 実験結果
2019/9/9 BERTology のススメ 38
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking
1 3 279784 7 65 3 279784 7 65 3 279784 7 65 3 279784 7 65
1 , - , - , - , -
1 C c Ci, - , - , - , 0.
1 , - , - , - , -
1 T i OD, 0. , - , - , -
1 D S i, 0. , - , - , -
1 a , - , - , 0. , -
1 N a S, 0. , p rt y - , or - , -
1 T , 0. , - , - , -
1 i , - , - , 0. , -
1 G e , - , 0. , - , -
1 , - , - , - , -
1 K , - , - , - , -
1 Ni , 0. , - , - , G -
1 , - , - , - , 0.
1 m, 0. , - , - , -
1 , 0. , - , - , -
1 , 0. , - , - , S -
1 , - , - , - , 0.
1 OK , 0. , - , - , -
1 , - , - , - , G -
1 Q li, 0. , - , - , -
1 , - , - , - , -
1 a S, or - , p rt y - , 0. , -
1 O K m O K Ni, - , - , 0. , -
1 i O, - , 0. , G - , -
1 O N G, 0. , - , - , -
タグ付け-NER: 実装
• train_ner.py : input_ids, token_type_ids, input_mask は以下の通り
2019/9/9 BERTology のススメ 39
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner
Encoder (Transformer の左側)
get_sequence_output
input_ids 101 102 1030
0 0 00
1 1 11
1
0
1
104 105
0 0
1 1
token_type_ids
input_mask
タイプ(c): タグ付け
→ 各トークンのベクトルを取り出す
→ get_sequence_output()
全結合 + Softmax
O[CLS] O1 O[SEP]O3 O4O21 O22
タグ1 タグ3 タグ4タグ21
w1 w2 → sub21+ sub22[CLS] [SEP]w3 w4
• input_ids
1. FullTokenizer.tokenize() で
⼊⼒⽂をトークン化
2. FullTokenizer.convert_
tokens_to_ids() で ID に変換
してセット
• token_type_ids
すべて同じ=0 をセット
• input_mask
すべて同じ=1 をセット
※ FullTokenizer は,
tokenization.py が提供
タグ22
タグ付け-NER: 実装
• train_ner.py : BERT 実装にソースがないため,実装した
2019/9/9 BERTology のススメ 40
https://github.com/haradatm/nlp/tree/master/bert/ner
← Pre-train モデルはここ
def main(): (テキスト分類と同じ)
class BertNER(chainer.Chain):
def __init__(self, bert, num_labels):
:
def forward(self, input_ids, input_mask, token_type_ids, labels):
final_hidden = self.bert.get_sequence_output(input_ids, …)
batch_size, seq_length, hidden_size = final_hidden.shape
final_hidden_matrix = F.reshape(final_hidden, [batch_size * seq_length, hidden_size])
output_layer = self.output(final_hidden_matrix)
output_layer = F.reshape(output_layer, [batch_size, seq_length, self.n_label])
return output_layer
def __call__(self, input_ids, input_mask, token_type_ids, label_ids):
logits = self.forward(input_ids, input_mask, token_type_ids)
batch_size, seq_length, class_size = logits.shape
y = F.reshape(logits, [batch_size * seq_length, class_size])
t = F.reshape(label_ids, [batch_size * seq_length])
loss = F.softmax_cross_entropy(y, t, ignore_label=0)
return loss, logits
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner
タイプ(c): タグ付け
→ 各トークンのベクトルを取り出す
→ get_sequence_output()
タグ付け-NER: 実験データ
• Datasets
• 英語: CoNLL-2013 NER datasets →固有表現 4ラベル
• https://github.com/glample/tagger/tree/master/dataset
• PER (⼈名), LOC(場所), ORG(組織), MISC(その他) → IOBタグは11種
• 全 8,483件 → 学習 14,041, 評価 3,453 に分割
• Tokenizer で分割されたサブワードには,論⽂に従いラベル’X’ を付与
• BERT モデル
• 英語: Google AI, uncased_L-12_H-768_A-12
2019/9/9 BERTology のススメ 41
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner
タグ付け-NER: 実験結果
• 論⽂中の 92.4 には届かず
2019/9/9 BERTology のススメ 42
詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner
==== Classification report (early-stopped model) ====
precision recall f1-score support
PER 0.96 0.96 0.96 1617
LOC 0.92 0.93 0.92 1668
ORG 0.88 0.88 0.88 1661
MISC 0.80 0.79 0.79 702
micro avg 0.90 0.91 0.91 5648
macro avg 0.90 0.91 0.91 5648
BERTology 論⽂
Knowledge Graph 界隈と BERT 分析
2019/9/9 BERTology のススメ 43
Knowledge Graph (1)
• Relation Extraction のための Simple な拡張
2019/9/9 BERTology のススメ 44
⽂献[7]
Simple BERT Models for Relation
Extraction and Semantic Role Labeling
• ⼊⼒は
[[CLS]⽂[SEP] subject [SEP] object [SEP]]
• 過適合を防ぐため,⽂中の実体を
マスクで置き換える
(Subj-PERやObj-LOC)
• BERTの出⼒のうち [SEP]以降の
シーケンスを破棄
• 位置埋め込みと連結して,⼀層の
BILSTM と MLPで予測
Figure 1: Architecture of our relation extraction model. (a) denotes the
concatenation of BERT contextual embedding and position embedding. The final
prediction is based on the concatenation of the final hidden state in each direction
from the BiLSTM, fed through an MLP.
Position embedding:
Table 1: Results on the TACRED test set.
Knowledge Graph (2)
• テキストとKGの両⽅を活⽤した拡張⾔語表現モデル
2019/9/9 BERTology のススメ 45
⽂献[8]
ERNIE: Enhanced Language
Representation with Informative Entities
Figure 2: The left part is the architecture of ERNIE.
← Figure 3: Modifying the
input sequence for the
specific tasks.
• T-Encoderは, BERTの実装と同⼀
で,トークンを埋め込む
• エンティティは, TransE などの効
果的なKG埋め込みで事前学習
• K-Encoderは, 知識情報を⾔語表
現に注⼊し,トークンとエンティ
ティの異種情報を統⼀空間で表現
• 同⼀構造で Relation Extraction と
Entity Typing の両タスクに対応
Table 5: Results of various models on FewRel
and TACRED (%).
BERT⾃体の分析 Revealing the Dark Secrets of BERT [EMNLP’19]
• BERT の Self-Attention を分析した論⽂
2019/9/9 BERTology のススメ 46
⽂献[8]
図9: レイヤー(全12ヘッド)を1つ無効にしたときの精度
→すべて使う(オレンジ)より良い場合がある
図8: ヘッドを1つ無効にしたときの精度 →すべて使う(オレンジ)より良い場合がある
図5: fine-tuning 前後でアテンションの変化 →出⼒に近いレイヤーで変化が⼤きい
(濃いほど類似度が低い)
• 異なるヘッドでタスクに関係なく同
じパターンが⼀貫して繰り返された
• モデルの枝刈りとデータの繰り返し
を減らす最適なアーキテクチャの発
⾒を⽰唆図1: アテンションを5パターンに分類. 最初の3つは事前学習に関連, 最後の2つは意味
や構⽂情報を捉える. 異なるヘッドで同じパターンの繰り返しがあった
BERT fine-tuning まとめ
• 事前学習モデルとタスクに応じた fine-tuning で,容易に良好な
性能が得られることを確認
• BertModel クラスにある 2つの Function の使い⽅をマスター
するだけで, 応⽤範囲は広い
• [CLS] のベクトルを取り出す get_pooled_output()
• 各トークンのベクトルを取り出す get_sequence_output()
• 様々な応⽤タスクへの適⽤等, 楽しい BERTology 活動を!
2019/9/9 BERTology のススメ 47
⽂献
[1] ⻄⽥ 京介. “機械読解の現状と展望." ⾔語処理学会第25回年次⼤会チュートリアル資料,
2019 (2019).
[2] Jacob Devlin, et al. "Bert: Pre-training of deep bidirectional transformers for
language understanding." (2018)
[3] Wang, Alex, et al. "Glue: A multi-task benchmark and analysis platform for
natural language understanding." arXiv preprint arXiv:1804.07461 (2018).
[4] 柴⽥ 知秀, 河原 ⼤輔, ⿊橋 禎夫. “ BERTによる⽇本語構⽂解析の精度向上." ⾔語処理学
会 第25回年次⼤会, pp.205-208, 名古屋, (2019.3).
[5] Vaswani, Ashish, et al. "Attention is all you need." Advances in neural
information processing systems. 2017.
[6] Klein, Guillaume, et al. "Opennmt: Open-source toolkit for neural machine
translation." arXiv preprint arXiv:1701.02810 (2017).
2019/9/9 BERTology のススメ 48
⽂献
[7] Shi, Peng, and Jimmy Lin. "Simple BERT Models for Relation Extraction and
Semantic Role Labeling." arXiv preprint arXiv:1904.05255 (2019).
[8] Zhang, Zhengyan, et al. "ERNIE: Enhanced Language Representation with
Informative Entities." arXiv preprint arXiv:1905.07129 (2019).
[9] Kovaleva, Olga, et al. "Revealing the Dark Secrets of BERT." arXiv preprint
arXiv:1908.08593 (2019).
2019/9/9 BERTology のススメ 49
付録: その他の汎⽤⾔語モデル
2019/9/9 BERTology のススメ 50
2019年5⽉: XLNet
• BERT の弱点を修正し, 20以上のタスクで BERT を超えた
2019/9/9 BERTology のススメ 51
⼈間
BERTXLNet
BERT
https://rajpurkar.github.io/SQuAD-explorer/
• BERT のマスク単語予測の
問題(通常発⽣しないため
ノイズにもなる)を克服
• 20タスクで BERT を超えた
(2018/5/21)
• 機械読解タスク(左)では,
single モデルで BERT の
ensemble を超える
⽂献[10]
2019年2⽉: GPT-2
• 超⼤規模コーパス(800万⽂書)で学習した超巨⼤サイズの GPT
2019/9/9 BERTology のススメ 52
• コーパスは Common Crawl から⼈間がcurate/filter および Reddit で絞り込み
• 1600次元, 1024トークン(BPE)の修正版GPT (パラメタ数は元のGPTの10倍)
• zero-shot で 7/8 の⾔語モデルのタスクで Transformer-XL を上回りSOTA (上表)
• zero-shot で 読解,要約,翻訳にも「ある程度」適応可 (タスクに有⽤な表現が多い場合)
Table 3. Zero-shot results on many datasets.
⽂献[11]
2019年7⽉: RoBERTa
• ⾔語理解タスクの事前学習のBERTを改善し,多くのタスクで
SoTAを更新, SuperGLUEタスクでも⼈の成績に近く
2019/9/9 BERTology のススメ 53
BERTの改善:
(1)動的なマスク
(2)次⽂の予測を削除
(3)⼤きなバッチサイズ
(4) ⾛査回数を増やす
(5)⽂字ではなくバイト
単位のBPEを使⽤
⽂献[12]
2019年7⽉: RoBERTa
• ⾔語理解タスクの事前学習のBERTを改善し,多くのタスクで
SoTAを更新, SuperGLUEタスクでも⼈の成績に近く
2019/9/9 BERTology のススメ 54
BERTの改善:
(1)動的なマスク
(2)次⽂の予測を削除
(3)⼤きなバッチサイズ
(4) ⾛査回数を増やす
(5)⽂字ではなくバイト
単位のBPEを使⽤
⽂献[12][13]
SuperGULE
• GLUEを新しいタスクで作り直したベンチマーク
• 分類/⽂関係(NLI)が減り,⽂書理解系(共参照/意味識別)が増えている
2019/9/9 BERTology のススメ 55
https://w4ngatang.github.io/static/papers/superglue.pdf
⽂献[13]
⽂献
[10] Yang, Zhilin, et al. "XLNet: Generalized Autoregressive Pretraining for Language
Understanding." arXiv preprint arXiv:1906.08237 (2019).
[11] Radford, Alec, et al. "Language models are unsupervised multitask learners."
OpenAI Blog 1.8 (2019).
[12] Liu, Yinhan, et al. "RoBERTa: A Robustly Optimized BERT Pretraining Approach."
arXiv preprint arXiv:1907.11692 (2019).
[13] Wang, Alex, et al. "Superglue: A stickier benchmark for general-purpose
language understanding systems." arXiv preprint arXiv:1905.00537 (2019).
2019/9/9 BERTology のススメ 56

Mais conteúdo relacionado

Mais procurados

全力解説!Transformer
全力解説!Transformer全力解説!Transformer
全力解説!TransformerArithmer Inc.
 
強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)
強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)
強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)Shota Imai
 
【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい
【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい
【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたいTakuji Tahara
 
深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで
深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで
深層学習による自然言語処理入門: word2vecからBERT, GPT-3までYahoo!デベロッパーネットワーク
 
データに内在する構造をみるための埋め込み手法
データに内在する構造をみるための埋め込み手法データに内在する構造をみるための埋め込み手法
データに内在する構造をみるための埋め込み手法Tatsuya Shirakawa
 
勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)
勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)
勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)RyuichiKanoh
 
Transformerを雰囲気で理解する
Transformerを雰囲気で理解するTransformerを雰囲気で理解する
Transformerを雰囲気で理解するAtsukiYamaguchi1
 
SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法
SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法
SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法SSII
 
NLPにおけるAttention~Seq2Seq から BERTまで~
NLPにおけるAttention~Seq2Seq から BERTまで~NLPにおけるAttention~Seq2Seq から BERTまで~
NLPにおけるAttention~Seq2Seq から BERTまで~Takuya Ono
 
[研究室論文紹介用スライド] Adversarial Contrastive Estimation
[研究室論文紹介用スライド] Adversarial Contrastive Estimation[研究室論文紹介用スライド] Adversarial Contrastive Estimation
[研究室論文紹介用スライド] Adversarial Contrastive EstimationMakoto Takenaka
 
NIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision Tree
NIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision TreeNIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision Tree
NIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision TreeTakami Sato
 
最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向ohken
 
adversarial training.pptx
adversarial training.pptxadversarial training.pptx
adversarial training.pptxssuserc45ddf
 
[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision
[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision
[DL輪読会]Learning Transferable Visual Models From Natural Language SupervisionDeep Learning JP
 
近年のHierarchical Vision Transformer
近年のHierarchical Vision Transformer近年のHierarchical Vision Transformer
近年のHierarchical Vision TransformerYusuke Uchida
 
[DL輪読会]相互情報量最大化による表現学習
[DL輪読会]相互情報量最大化による表現学習[DL輪読会]相互情報量最大化による表現学習
[DL輪読会]相互情報量最大化による表現学習Deep Learning JP
 
機械学習モデルの判断根拠の説明(Ver.2)
機械学習モデルの判断根拠の説明(Ver.2)機械学習モデルの判断根拠の説明(Ver.2)
機械学習モデルの判断根拠の説明(Ver.2)Satoshi Hara
 
2019年度チュートリアルBPE
2019年度チュートリアルBPE2019年度チュートリアルBPE
2019年度チュートリアルBPE広樹 本間
 
[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets
[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets
[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic DatasetsDeep Learning JP
 

Mais procurados (20)

全力解説!Transformer
全力解説!Transformer全力解説!Transformer
全力解説!Transformer
 
強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)
強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)
強化学習エージェントの内発的動機付けによる探索とその応用(第4回 統計・機械学習若手シンポジウム 招待公演)
 
【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい
【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい
【LT資料】 Neural Network 素人なんだけど何とかご機嫌取りをしたい
 
深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで
深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで
深層学習による自然言語処理入門: word2vecからBERT, GPT-3まで
 
データに内在する構造をみるための埋め込み手法
データに内在する構造をみるための埋め込み手法データに内在する構造をみるための埋め込み手法
データに内在する構造をみるための埋め込み手法
 
勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)
勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)
勾配ブースティングの基礎と最新の動向 (MIRU2020 Tutorial)
 
Transformerを雰囲気で理解する
Transformerを雰囲気で理解するTransformerを雰囲気で理解する
Transformerを雰囲気で理解する
 
SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法
SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法
SSII2021 [OS2-01] 転移学習の基礎:異なるタスクの知識を利用するための機械学習の方法
 
NLPにおけるAttention~Seq2Seq から BERTまで~
NLPにおけるAttention~Seq2Seq から BERTまで~NLPにおけるAttention~Seq2Seq から BERTまで~
NLPにおけるAttention~Seq2Seq から BERTまで~
 
[研究室論文紹介用スライド] Adversarial Contrastive Estimation
[研究室論文紹介用スライド] Adversarial Contrastive Estimation[研究室論文紹介用スライド] Adversarial Contrastive Estimation
[研究室論文紹介用スライド] Adversarial Contrastive Estimation
 
NIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision Tree
NIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision TreeNIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision Tree
NIPS2017読み会 LightGBM: A Highly Efficient Gradient Boosting Decision Tree
 
最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向最適輸送の計算アルゴリズムの研究動向
最適輸送の計算アルゴリズムの研究動向
 
TabNetの論文紹介
TabNetの論文紹介TabNetの論文紹介
TabNetの論文紹介
 
adversarial training.pptx
adversarial training.pptxadversarial training.pptx
adversarial training.pptx
 
[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision
[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision
[DL輪読会]Learning Transferable Visual Models From Natural Language Supervision
 
近年のHierarchical Vision Transformer
近年のHierarchical Vision Transformer近年のHierarchical Vision Transformer
近年のHierarchical Vision Transformer
 
[DL輪読会]相互情報量最大化による表現学習
[DL輪読会]相互情報量最大化による表現学習[DL輪読会]相互情報量最大化による表現学習
[DL輪読会]相互情報量最大化による表現学習
 
機械学習モデルの判断根拠の説明(Ver.2)
機械学習モデルの判断根拠の説明(Ver.2)機械学習モデルの判断根拠の説明(Ver.2)
機械学習モデルの判断根拠の説明(Ver.2)
 
2019年度チュートリアルBPE
2019年度チュートリアルBPE2019年度チュートリアルBPE
2019年度チュートリアルBPE
 
[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets
[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets
[DL輪読会]Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets
 

Semelhante a BERTology のススメ

200514material minami
200514material minami200514material minami
200514material minamiRCCSRENKEI
 
STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強
STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強
STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強Kiyoshi Ogawa
 
LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)
LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)
LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)Takeshi Yamamuro
 
OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)
OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)
OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)NTT DATA Technology & Innovation
 
DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)
DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)
DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)Takuji Tahara
 
最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)
最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)
最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)NTT DATA Technology & Innovation
 
充足可能性問題のいろいろ
充足可能性問題のいろいろ充足可能性問題のいろいろ
充足可能性問題のいろいろHiroshi Yamashita
 
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)takesako
 
DTrace for biginners part(2)
DTrace for biginners part(2)DTrace for biginners part(2)
DTrace for biginners part(2)Shoji Haraguchi
 
[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...
[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...
[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...Deep Learning JP
 
Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)
Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)
Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)MITSUNARI Shigeo
 
Or seminar2011final
Or seminar2011finalOr seminar2011final
Or seminar2011finalMikio Kubo
 
速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)
速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)
速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)NTT DATA Technology & Innovation
 
「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部
「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部
「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部Kiyoshi Ogawa
 
Intro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみたIntro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみたMITSUNARI Shigeo
 

Semelhante a BERTology のススメ (20)

200514material minami
200514material minami200514material minami
200514material minami
 
STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強
STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強
STARC RTL設計スタイルガイドによるVerilog HDL並列記述の補強
 
LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)
LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)
LLVMで遊ぶ(整数圧縮とか、x86向けの自動ベクトル化とか)
 
OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)
OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)
OSS-DB Gold 合格体験記(第29回PostgreSQLアンカンファレンス@オンライン 発表資料)
 
Prosym2012
Prosym2012Prosym2012
Prosym2012
 
Tokyo r27
Tokyo r27Tokyo r27
Tokyo r27
 
DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)
DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)
DSB2019振り返り会:あのにっくき QWK を閾値調整なしで攻略した(かった)
 
CMSI計算科学技術特論B(10) 大規模MD並列化の技術1
CMSI計算科学技術特論B(10) 大規模MD並列化の技術1CMSI計算科学技術特論B(10) 大規模MD並列化の技術1
CMSI計算科学技術特論B(10) 大規模MD並列化の技術1
 
最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)
最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)
最新機能までを総ざらい!PostgreSQLの注目機能を振り返る(第32回 中国地方DB勉強会 in 岡山 発表資料)
 
充足可能性問題のいろいろ
充足可能性問題のいろいろ充足可能性問題のいろいろ
充足可能性問題のいろいろ
 
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
 
ggplot2 110129
ggplot2 110129ggplot2 110129
ggplot2 110129
 
DTrace for biginners part(2)
DTrace for biginners part(2)DTrace for biginners part(2)
DTrace for biginners part(2)
 
[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...
[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...
[DL輪読会]Pervasive Attention: 2D Convolutional Neural Networks for Sequence-to-...
 
Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)
Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)
Xeon PhiとN体計算コーディング x86/x64最適化勉強会6(@k_nitadoriさんの代理アップ)
 
Or seminar2011final
Or seminar2011finalOr seminar2011final
Or seminar2011final
 
速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)
速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)
速習!論理レプリケーション ~基礎から最新動向まで~(PostgreSQL Conference Japan 2022 発表資料)
 
「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部
「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部
「C言語規格&MISRA-C:みんなで楽しいCプログラミング」NGK2013B名古屋合同懇親会2013忘年会昼の部
 
Intro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみたIntro to SVE 富岳のA64FXを触ってみた
Intro to SVE 富岳のA64FXを触ってみた
 
ADVENTURE_Thermalの概要
ADVENTURE_Thermalの概要ADVENTURE_Thermalの概要
ADVENTURE_Thermalの概要
 

Último

論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Danieldanielhu54
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 

Último (9)

論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 

BERTology のススメ

  • 1. BERTology のススメ BERT fine-tuning のすすめ 筑波⼤学⼤学院 ビジネス科学研究科 講師 Tomohiko HARADA ※ 本資料の著作権は,引⽤元の論⽂および記事に準じます 2019/9/9 BERTology のススメ 1
  • 2. Goal • BERT について,⼈より知ってる気分になる • BERT の fine-tuning について, やればできる気分になる 2019/9/9 BERTology のススメ 2
  • 3. Agenda • はじめに • BERT 解説 • BERT とは • Transformer • BERT fine-tuning • BERT fine-tuning の実践 • テキスト分類 Classifier • テキスト検索 Ranking • タグ付け NER • BERTology 論⽂ 2019/9/9 BERTology のススメ 3
  • 4. 2018年10⽉: BERT の衝撃 • タスクに特化した構造を持たずに,⼈間のスコアを⼤きく超えた 2019/9/9 BERTology のススメ 4 ⼈間 BERT https://rajpurkar.github.io/SQuAD-explorer/ ⽂献[1] • 機械読解タスク(左)で,完全⼀致 と部分⼀致の両指標で最⾼精度 (2018/10/5) • 様々な⾃然⾔語理解タスクで SOTA (QA,含意,⾔い換え,NER等) • タスク適応は,出⼒層をタスク毎 に 1層のみ追加してfine-tuning
  • 5. GLUE benchmark Results • 9つの⾃然⾔語理解タスクでSOTA (2018/10 時点) 2019/9/9 BERTology のススメ 5 Table 1: GLUE Test results, scored by the GLUE evaluation server (https://gluebenchmark.com/leaderboard). MNLI: テキスト同⼠の関連性(含意・⽭盾・中⽴)を判定 QQP: 2つの質問が同じ意味かを判定 QNLI: ⽂章内に質問の回答が含まれているかを判定 SST-2: 映画のレビューを基に感情分析し,良し悪しを判定 CoLA: ⽂章が⽂法的に正しいかを判定 STS-B: 2つのニュース⾒出しの意味的類似性を判定 MRPC: 2つのニュース記事が意味的に等価かを判定 RTE: 2つの⽂章が含意関係かどうかを判定 ⽂献[2][3]
  • 6. ⼿元のデータで簡易実験 • Scale Movie Review Dataset (rt-polarity): pos/neg 2値 2019/9/9 BERTology のススメ 6 77 77 - 5: 6 5: 6 - .5: 6 .5: 6 - 9 77 .8 2471 7473 7871 358 1 .19 16.100473 - - 9- , 9 85-. https://github.com/haradatm/nlp/tree/master/bert/classify
  • 7. BERTとは • 双⽅向 Transformer ブロックを24層重ねた⾔語モデル • 事前学習モデルが公開 2019/9/9 BERTology のススメ 7 *1 https://github.com/google-research/bert *2 http://nlp.ist.i.kyoto-u.ac.jp/index.php?BERT⽇本語Pretrainedモデル • 英語 • 本家 Google の事前学習モデル *1 • Book Corpus 8億語 + 英語 Wikipedia 25億語 (語彙数 3万) • ⽇本語 • ⿊橋研の事前学習モデル *2 • ⽇本語Wikipedia 約1,800万⽂ (語 彙数 3.2万) ⽂献[2][4]
  • 8. ⾔語モデルの Pre-train • ⾃然⾔語処理の応⽤タスクは学習データが多くない(数⼗万件程度) ため,⼤規模コーパスから学習したモデルを転移することが流⾏ • ELMo (2018/02に発表), OpenAI GPT (2018/06に発表) • ⾔語モデルは LTR あるいは RTL の次単語を予測するのが普通 2019/9/9 BERTology のススメ 8 ELMo left-to-right & right-to-left LSTMs OpenAI GPT left-to-right Transformer BERT 双⽅向 Transformer ⽂献[1][2]
  • 9. Transformer • RNNやCNNを使わずアテンションのみ使⽤したTransformerが ニューラル機械翻訳で圧倒的な SOTA を達成 • 従来,単語系列の⽂脈理解は主にLSTM →⻑期依存性の理解に限界 • 離れた単語の関係性も直接考慮できる Self-Attention が性能向上に ⼤きく寄与した (しかも省メモリで計算可) 2019/9/9 BERTology のススメ 9 LSTM 遠く離れた単語の関係性 を捕まえにくい Self-Attention 遠く離れた単語も 直接関係性を考慮できる 好き 好き な 動物 は な 動物 は ・・・ 好きな動物は Softmax ⽂献[1][5]
  • 10. Self-Attention • Self-Attention は,⽂構造や照応関係を獲得するという⾒⽅ 2019/9/9 BERTology のススメ 10 ← アテンションの重みに従って value から情報を引き出す • Self-Attentionでは Query, Key Value ともに同じコン テキストから⽣成されたベクトル ↓ “its” が “Law” を指す(照応関係)を獲得 ⽂献[5] Query 好き Query 好き Query 好き Query 好き Key1 好き Key2 な Key2 動物 Key3 は 0.1✖ 0.1✖ 0.7✖ 0.1✖ Softmax Key1 好き Key2 な Key2 動物 Key3 は + Attention OutputQuery好き
  • 11. Transformer • http://nlp.seas.harvard.edu/2018/04/03/attention.html 2019/9/9 BERTology のススメ 11 ⽂献[6] • 論⽂の注釈付きで,実装に沿って Transformer を解説したサイト • Jupyter および Google Colab のノートブックも公開 • BERT や Transformer の初学者に オススメ
  • 12. Transformer • 例: レイヤーN=6, ヘッドh=8, ⻑さ=512, 中間層=768 2019/9/9 BERTology のススメ 12 Multi-Head AttentionScaled Dot-Product Attention ⽂献[5]
  • 14. BERT Training • Encoder 出⼒のすぐ上に分類レイヤーを追加→予測結果を学習 2019/9/9 BERTology のススメ 14 w1 w2 w3 [MASK] w5 w1 w2 w3 w4 w5 w’1 w’2 w’3 w’4 w’5 [CLS] Token + Segment + Position Encoder Encoder (Transformer の左側) O1 O2 O3 O4 O5 全結合レイヤー (O0→2値, O4→語彙数) Softmax (→確率値) O0 IsNextTransformer Encoder ⽂献[2]
  • 15. BERT Pre-train モデル 2019/9/9 BERTology のススメ 15 モデル 英語 モデル ⽇本語 モデル Base Large 公開元 Google Research *1 京⼤ ⿊橋研 *2 コーパス Book Corpus 8億ワード + 英語Wikipedia 25億 ワード ⽇本語Wikipedia 約1,800万⽂ (半⾓を全⾓に正規化) 語彙 (サブワード含む) 30K (WordPiece) 32K (Juman++ & BEP) ⼊⼒⻑ 最⼤ 512トークン *3 最⼤ 128トークン *3 パラメタ 12層, 各層768次元 24層, 各層1024次元 12層, 各層768次元 (Base と同じ) 学習時間 4 Cloud TPUs で 4⽇間(≒100時間) 16 Cloud TPUs で 4⽇間(≒100時間) 1GPU (GTX 1080 Ti) で 約30⽇間(≒750時間) *1 https://github.com/google-research/bert *2 http://nlp.ist.i.kyoto-u.ac.jp/index.php?BERT⽇本語Pretrainedモデル *3 ⼊⼒できるシーケンスの⻑さに制限があることに注意 ⽂献[2][4]
  • 16. BERT fine-tuning • 出⼒層を1層追加してfine-tuningするのみで各タスクに適応 2019/9/9 BERTology のススメ 16 w1 w2 [SEP] W4 [SEP][CLS] Token + Segment + Position Encoder Encoder (Transformer の左側) O1 O2 O3 O4 O5O0 BERTModel タスクにあわせて,1層追加する (a)⽂ペアの分類 (b)単⽂の分類 (c)スパン抽出 (d)タグ付け 単⽂, ⽂ペア, QA 等 (a) ⽂ペアの分類 (b) 単⽂の分類 (c) スパン抽出 (例:読解) (d) タグ付け (例:NER) ⽂献[2]
  • 17. BERT fine-tuning • 出⼒層(ベクトル)は, BertModel のサービス関数で取り出せる 2019/9/9 BERTology のススメ 17 w1 w2 [SEP] W4 [SEP][CLS] Token + Segment + Position Encoder Encoder (Transformer の左側) O1 O2 O3 O4 O5O0 タスクにあわせて,1層追加する (a)⽂ペアの分類 (b)単⽂の分類 (c)スパン抽出 (d)タグ付け 単⽂, ⽂ペア, QA 等 class BertModel(chainer.Chain): def __init__(self, config): : def __call__(self, : def get_pooled_output(self, input_ids, input_mask=None, token_type_ids=None): return self.__call__(input_ids, input_mask, token_type_ids) def get_sequence_output(self, input_ids, input_mask=None, token_type_ids=None): return self.__call__(input_ids, input_mask, token_type_ids, get_sequence_output=True) def get_embedding_output(self, input_ids, input_mask=None, token_type_ids=None): return self.__call__(input_ids, input_mask, token_type_ids, get_embedding_output=True) 学習済みの埋め込みを取り出す 各トークンのベクトルを取り出す [CLS] のベクトルを取り出す ⽂献[2]
  • 18. BERT fine-tuning: テキスト分類 (a)(b) • ⽂ペアまたは単⽂の分類: 全結合層を1層追加してfine-tuning • ⼊⼒: 先頭に [CLS], ⽂と⽂の境界には [SEP] トークンを追加 2019/9/9 BERTology のススメ 18 class BertClassifier(chainer.Chain): def __init__(self, bert, num_labels): : def __call__(self, input_ids, input_mask, token_type_ids, labels): output_layer = self.bert.get_pooled_output(input_ids, …) output_layer = F.dropout(output_layer, 0.1) logits = self.output(output_layer) loss = F.softmax_cross_entropy(logits, labels) return loss (b) 単⽂の分類 Class label Sentence 1 Sentence 2 (a) ⽂ペアの分類 (a) Sentence Pair Classification Tasks: MNLI, QQP, QNLI, STS-B, BRPC, RTE, SWAG Class label Single Sentence (b) Single Sentence Classification Task: MSST-2, CoLA ⽂献[2] (b) 単⽂の分類
  • 19. BERT fine-tuning: タグ付け (d) • NER: 全結合層を1層追加してfine-tuning • ⼊⼒: 先頭に [CLS], Qとパラグラフの境界には [SEP] トークンを追加 2019/9/9 BERTology のススメ 19 (d) Single Sentence Tagging Tasks: CoNLL-2003 NER O B-PER ・・・ O Single Sentence class BertNER(chainer.Chain): def __init__(self, bert): : def __call__(self, input_ids, input_mask, token_type_ids, labels): final_hidden = self.bert.get_sequence_output(input_ids, …) : logits = self.output(final_hidden_matrix) : loss = F.softmax_cross_entropy(logits, labels, ignore_label=0) return loss (リサイズ等) (リサイズ等) ⽂献[2]
  • 20. BERT fine-tuning: スパン抽出 (c) • SQuAD: 全結合層を1層追加してfine-tuning • ⼊⼒: 先頭に [CLS], Qとパラグラフの境界には [SEP] トークンを追加 2019/9/9 BERTology のススメ 20 Start/End Span Question Paragraph (c) Question Answering Tasks: SQuAD v1.1 class BertSQuAD(chainer.Chain): def __init__(self, bert): : def __call__(self, input_ids, input_mask, token_type_ids): final_hidden = self.bert.get_sequence_output(input_ids, …) : logits = self.output(final_hidden_matrix) : unstacked_logits = F.separate(logits, axis=0) (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1]) start_loss = F.softmax_cross_entropy(start_logits, start_positions) end_loss = F.softmax_cross_entropy(end_logits, end_positions) total_loss = (start_loss + end_loss) / 2.0 return total_loss (リサイズ等) (リサイズ等) ⽂献[2]
  • 21. 解説 BERT fine-tuning: スパン抽出 (c) • タグ付け(d): T ∈ RK×H, W ∈ RK×H, H=768, K=2 の場合 • スパン抽出(c): T ∈ RK×H, Start ∈ RH, End ∈ RH, H=768, K=2 {Start,End} 2019/9/9 BERTology のススメ 21 tii w1,1 w1,2 ti2 w2,1 w2,2 y1 = ti1・w1,1 + ti2・w2,1 + ti3・w3,1 +・・・ ti768・w768,1 ti3 w3,1 w3,2 y2 = ti1・w1,2 + ti2・w2,2 + ti3・w3,2 +・・・ ti768・w768,2 : : ti768 w768,1 w768,2 T (H=768次元) WT (HxK) Y (K=2次元) ti1 ti2 ti3 ・・ ・・ ti768 dot w1,1 w1,2 = y1 y2 w2,1 w2,2 クラス1 クラス2 w3,1 w3,2 : : : : w768,1 w768,2 T (H=768次元) ST , ET (HxK) Y (K=2次元) ti1 ti2 ti3 ・・ ・・ ti768 dot w1,1 w1,2 = y1 y2 w2,1 w2,2 Start End w3,1 w3,2 : : : : w768,1 wh2 softmax(S・Ti) ⇒ S・Ti 同様に softmax(E・Ti) ⇒ E・Ti ti : i番⽬のトークンの出⼒ベクトル ⽂献[2]
  • 22. 解説 BERT fine-tuning: スパン抽出 (c) • タグ付け(d): T ∈ RK×H, W ∈ RK×H, H=768, K=2 の場合 • スパン抽出(c): T ∈ RK×H, Start ∈ RH, End ∈ RH, H=768, K=2 {Start,End} 2019/9/9 BERTology のススメ 22 トークン1 ・・ → 全結合層 → Softmax (列⽅向) トークン2 トークン3 : トークンN H=768 次元 クラス数=K次元 クラス数=K次元 H=768 次元 Start/End =2次元 トークン1 ・・ → 全結合層 → Softmax (⾏⽅向) → Softmax (⾏⽅向) トークン2 トークン3 : トークンN Startかどうか =1次元 Endかどうか =1次元
  • 23. BERT まとめ • 2018年に Google AI が発表し,話題を席巻した 汎⽤⾔語モデル → 双⽅向 Transformer ブロックを24層重ねたモデル • ⾔語モデルを⼤規模コーパスで事前学習し,出⼒層をタスクに応 じた1層のみ追加して fine-tuning • fine-tuning モデルは,タスクに特化した構造を持たずに,様々な タスクで汎⽤的かつ良好な性能 • 発表以来,派⽣⼿法や応⽤タスクへの適⽤等の研究(BERTology) が盛んになり, 引⽤数は1,276 に上る ※ 2019/09/08 時点 2019/9/9 BERTology のススメ 23
  • 24. BERT fine-tuning の実践 以降では chainer を想定しています 2019/9/9 BERTology のススメ 24
  • 25. fine-tuning 実装例と実験 • https://github.com/haradatm/nlp/tree/master/bert 2019/9/9 BERTology のススメ 25 テキスト分類: rt-polarity(英), 国交省不具合DB(⽇) での実験 テキスト検索: MedNLP 患者表現辞書(⽇) での実験 タグ付け: CoNLL-2013 (英) での追実験
  • 27. bert-chainer https://github.com/soskek/bert-chainer • BERT の chainer 実装が公開されている 2019/9/9 BERTology のススメ 27
  • 28. bert-chainer https://github.com/soskek/bert-chainer • Pre-train モデルの TensorFlow→chainer (.npz) 変換が必要 2019/9/9 BERTology のススメ 28
  • 29. Python コードの配置 • BERT 実装の中から,fine-tuningに必要な 3ファイルを集める 2019/9/9 BERTology のススメ 29 右: https://github.com/haradatm/nlp/tree/master/bert/classify左: https://github.com/soskek/bert-chainer ← Pre-train モデルはここ
  • 30. テキスト分類: 実装 • train_bert.py : input_ids, token_type_ids, input_mask は以下の通り 2019/9/9 BERTology のススメ 30 詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify • input_ids 1. FullTokenizer.tokenize() で ⼊⼒⽂をトークン化 2. FullTokenizer.convert_ tokens_to_ids() で ID に変換 してセット • token_type_ids すべて同じ=0 をセット • input_mask すべて同じ=1 をセット ※ FullTokenizer は, tokenization.py が提供 分類結果 全結合 + Softmax O[CLS] Encoder (Transformer の左側) get_pooled_output() input_ids w1 w2 → sub21+ sub22[CLS] [SEP] 101 102 1030 0 0 00 1 1 11 1 0 1 104 105 0 0 1 1 w3 w4 token_type_ids input_mask タイプ(b): 単⽂の分類 → [CLS] のベクトルを取り出す → get_pooled_output()
  • 31. テキスト分類: 実装 • train_bert.py : BERT 実装の run_classifier.py とほぼ同じ 2019/9/9 BERTology のススメ 31 https://github.com/haradatm/nlp/tree/master/bert/classify ← Pre-train モデルはここ class BertClassifier(chainer.Chain): def __init__(self, bert, num_labels): : def __call__(self, input_ids, input_mask, token_type_ids, labels): output_layer = self.bert.get_pooled_output(input_ids, …) output_layer = F.dropout(output_layer, 0.1) logits = self.output(output_layer) loss = F.softmax_cross_entropy(logits, labels) return loss def main(): : bert_config = BertConfig.from_json_file(bert_config_file) tokenizer = FullTokenizer(vocab_file=vocab_file, do_lower_case=True) : bert = BertModel(config=bert_config) model = BertClassifier(bert, num_labels=len(labels)) chainer.serializers.load_npz( init_checkpoint, model, ignore_names=['output/W', 'output/b']) 詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify タイプ(b): 単⽂の分類 → [CLS] のベクトルを取り出す → get_pooled_output()
  • 32. テキスト分類: 実験データ • Datasets • 英語: Scale Movie Review Dataset (rt-polarity) →Pos/Neg 2クラス • https://www.cs.cornell.edu/people/pabo/movie-review-data/ • 全 51,940 件 → 学習 46,743, 評価 5,197 に分割 • ⽇本語: 国交省「⾃動⾞の不具合情報」(mlit)→不具合分類 16クラス • http://carinf.mlit.go.jp/jidosha/carinf/opn/index.html • クロール&スクレイピングした 全10,662 件 → 学習 9,596, 評価 1,066 に分割 • テキストは,あらかじめ Juman++ で分かち書き • BERT モデル • 英語: Google AI, uncased_L-12_H-768_A-12 • ⽇本語: ⿊橋研, Japanese_L-12_H-768_A-12_E-30_BPE 2019/9/9 BERTology のススメ 32 詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify
  • 33. テキスト分類: 実験結果 • 英(rt-polarity)⽇(mlit)の両⽅で精度向上, 特に英語が効果⼤ 2019/9/9 BERTology のススメ 33 66 66 -:: 49:5 49:5 -:: .49: 5 .49: 5 -:: 8 66 .7 1360 : 636 2 6760 247 0 .08: - - 8- , 8: 74-. .55 .55 ,99 3894 3894 ,99 -389 4 -389 4 ,99 7.55 -6 0 5 9:5 5 1 565 136 - 79 ,.. ,..:7,. 43 9 .63,- 詳細→ https://github.com/haradatm/nlp/tree/master/bert/classify
  • 34. テキスト検索: 実装 • train_patient-triplet.py : input_ids, token_type_ids, input_mask は以下の通り 2019/9/9 BERTology のススメ 34 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking • input_ids 1. FullTokenizer.tokenize() で ⼊⼒⽂をトークン化 2. FullTokenizer.convert_ tokens_to_ids() で ID に変換 してセット • token_type_ids すべて同じ=0 をセット • input_mask すべて同じ=1 をセット ※ FullTokenizer は, tokenization.py が提供 クエリ, 正例, 負例 の 3つの Triplet loss で学習 タイプ(b): 単⽂の分類 → [CLS] のベクトルを取り出す → get_pooled_output() Encoder (Transformer の左側) get_pooled_output() input_ids 101 102 1030 0 0 00 1 1 11 1 0 1 104 105 0 0 1 1 token_type_ids input_mask w1 w2 → sub21+ sub22[CLS] [SEP]w3 w4 クエリ 正例 負例 O[CLS] 全結合 (次元数の変更)
  • 35. テキスト検索: 実装 • train_patient-triplet.py : クエリ-正例-負例 の距離を学習 2019/9/9 BERTology のススメ 35 https://github.com/haradatm/nlp/tree/master/bert/ranking class BertRanking(chainer.Chain): def __init__(self, bert, num_labels): : def forward(self, input_ids, input_mask, token_type_ids, labels): output_layer = self.bert.get_pooled_output(input_ids, …) output_layer = F.dropout(output_layer, 0.1) output_layer = self.output(output_layer) return output_layer def __call__(self, q_x1, q_x2, q_x3, d_x1, d_x2, d_x3, n_x1, n_x2, n_x3): y_0 = self.forward(q_x1, q_x2, q_x3) y_1 = self.forward(d_x1, d_x2, d_x3) y_2 = self.forward(n_x1, n_x2, n_x3) loss = F.triplet(y_0, y_1, y_2) return loss ← Pre-train モデルはここ def main(): : bert_config = BertConfig.from_json_file(bert_config_file) … (テキスト分類と同じ) 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking タイプ(b): 単⽂の分類 → [CLS] のベクトルを取り出す → get_pooled_output()
  • 36. テキスト検索: 実験データ • Datasets • ⽇本語: MedNLP「患者表現辞書」→不具合分類 16クラス 患者が実際に⽤いる病名表現を網羅した辞書 • http://sociocom.jp/~data/2019-pde/ • 患者表現-標準病名ペア 8,483件 → 学習 7,483, 評価 1,000 に分割 (重複なし) • ペアを正例, 負例はペアでない組み合わせをランダムに選択 • テキストは,あらかじめ Juman++ で分かち書き • BERT モデル • ⽇本語: ⿊橋研, Japanese_L-12_H-768_A-12_E-30_BPE 2019/9/9 BERTology のススメ 36 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking
  • 37. テキスト検索: 実験結果 • 表層が⼀致しない検索も, fine-tuning で「ある程度」検索可能 2019/9/9 BERTology のススメ 37 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking +( -( ) - )+ ) ( ) + 1 ) 024 9 6988 @ 6 ) 69B 9 6988 @ 69B @9 E@9 B 9 3 5B 395B7 29 E 5 . @ 78. % %% % %% % %% (% %% )% %% % %% +% %% % %% -% %% 03 03 ( 03 ) 03 03 + 03 03 - 03 . 03 % 03 8 03 11 1 07 03 37032 03 4583 83 5963
  • 38. テキスト検索: 実験結果 2019/9/9 BERTology のススメ 38 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ranking 1 3 279784 7 65 3 279784 7 65 3 279784 7 65 3 279784 7 65 1 , - , - , - , - 1 C c Ci, - , - , - , 0. 1 , - , - , - , - 1 T i OD, 0. , - , - , - 1 D S i, 0. , - , - , - 1 a , - , - , 0. , - 1 N a S, 0. , p rt y - , or - , - 1 T , 0. , - , - , - 1 i , - , - , 0. , - 1 G e , - , 0. , - , - 1 , - , - , - , - 1 K , - , - , - , - 1 Ni , 0. , - , - , G - 1 , - , - , - , 0. 1 m, 0. , - , - , - 1 , 0. , - , - , - 1 , 0. , - , - , S - 1 , - , - , - , 0. 1 OK , 0. , - , - , - 1 , - , - , - , G - 1 Q li, 0. , - , - , - 1 , - , - , - , - 1 a S, or - , p rt y - , 0. , - 1 O K m O K Ni, - , - , 0. , - 1 i O, - , 0. , G - , - 1 O N G, 0. , - , - , -
  • 39. タグ付け-NER: 実装 • train_ner.py : input_ids, token_type_ids, input_mask は以下の通り 2019/9/9 BERTology のススメ 39 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner Encoder (Transformer の左側) get_sequence_output input_ids 101 102 1030 0 0 00 1 1 11 1 0 1 104 105 0 0 1 1 token_type_ids input_mask タイプ(c): タグ付け → 各トークンのベクトルを取り出す → get_sequence_output() 全結合 + Softmax O[CLS] O1 O[SEP]O3 O4O21 O22 タグ1 タグ3 タグ4タグ21 w1 w2 → sub21+ sub22[CLS] [SEP]w3 w4 • input_ids 1. FullTokenizer.tokenize() で ⼊⼒⽂をトークン化 2. FullTokenizer.convert_ tokens_to_ids() で ID に変換 してセット • token_type_ids すべて同じ=0 をセット • input_mask すべて同じ=1 をセット ※ FullTokenizer は, tokenization.py が提供 タグ22
  • 40. タグ付け-NER: 実装 • train_ner.py : BERT 実装にソースがないため,実装した 2019/9/9 BERTology のススメ 40 https://github.com/haradatm/nlp/tree/master/bert/ner ← Pre-train モデルはここ def main(): (テキスト分類と同じ) class BertNER(chainer.Chain): def __init__(self, bert, num_labels): : def forward(self, input_ids, input_mask, token_type_ids, labels): final_hidden = self.bert.get_sequence_output(input_ids, …) batch_size, seq_length, hidden_size = final_hidden.shape final_hidden_matrix = F.reshape(final_hidden, [batch_size * seq_length, hidden_size]) output_layer = self.output(final_hidden_matrix) output_layer = F.reshape(output_layer, [batch_size, seq_length, self.n_label]) return output_layer def __call__(self, input_ids, input_mask, token_type_ids, label_ids): logits = self.forward(input_ids, input_mask, token_type_ids) batch_size, seq_length, class_size = logits.shape y = F.reshape(logits, [batch_size * seq_length, class_size]) t = F.reshape(label_ids, [batch_size * seq_length]) loss = F.softmax_cross_entropy(y, t, ignore_label=0) return loss, logits 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner タイプ(c): タグ付け → 各トークンのベクトルを取り出す → get_sequence_output()
  • 41. タグ付け-NER: 実験データ • Datasets • 英語: CoNLL-2013 NER datasets →固有表現 4ラベル • https://github.com/glample/tagger/tree/master/dataset • PER (⼈名), LOC(場所), ORG(組織), MISC(その他) → IOBタグは11種 • 全 8,483件 → 学習 14,041, 評価 3,453 に分割 • Tokenizer で分割されたサブワードには,論⽂に従いラベル’X’ を付与 • BERT モデル • 英語: Google AI, uncased_L-12_H-768_A-12 2019/9/9 BERTology のススメ 41 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner
  • 42. タグ付け-NER: 実験結果 • 論⽂中の 92.4 には届かず 2019/9/9 BERTology のススメ 42 詳細→ https://github.com/haradatm/nlp/tree/master/bert/ner ==== Classification report (early-stopped model) ==== precision recall f1-score support PER 0.96 0.96 0.96 1617 LOC 0.92 0.93 0.92 1668 ORG 0.88 0.88 0.88 1661 MISC 0.80 0.79 0.79 702 micro avg 0.90 0.91 0.91 5648 macro avg 0.90 0.91 0.91 5648
  • 43. BERTology 論⽂ Knowledge Graph 界隈と BERT 分析 2019/9/9 BERTology のススメ 43
  • 44. Knowledge Graph (1) • Relation Extraction のための Simple な拡張 2019/9/9 BERTology のススメ 44 ⽂献[7] Simple BERT Models for Relation Extraction and Semantic Role Labeling • ⼊⼒は [[CLS]⽂[SEP] subject [SEP] object [SEP]] • 過適合を防ぐため,⽂中の実体を マスクで置き換える (Subj-PERやObj-LOC) • BERTの出⼒のうち [SEP]以降の シーケンスを破棄 • 位置埋め込みと連結して,⼀層の BILSTM と MLPで予測 Figure 1: Architecture of our relation extraction model. (a) denotes the concatenation of BERT contextual embedding and position embedding. The final prediction is based on the concatenation of the final hidden state in each direction from the BiLSTM, fed through an MLP. Position embedding: Table 1: Results on the TACRED test set.
  • 45. Knowledge Graph (2) • テキストとKGの両⽅を活⽤した拡張⾔語表現モデル 2019/9/9 BERTology のススメ 45 ⽂献[8] ERNIE: Enhanced Language Representation with Informative Entities Figure 2: The left part is the architecture of ERNIE. ← Figure 3: Modifying the input sequence for the specific tasks. • T-Encoderは, BERTの実装と同⼀ で,トークンを埋め込む • エンティティは, TransE などの効 果的なKG埋め込みで事前学習 • K-Encoderは, 知識情報を⾔語表 現に注⼊し,トークンとエンティ ティの異種情報を統⼀空間で表現 • 同⼀構造で Relation Extraction と Entity Typing の両タスクに対応 Table 5: Results of various models on FewRel and TACRED (%).
  • 46. BERT⾃体の分析 Revealing the Dark Secrets of BERT [EMNLP’19] • BERT の Self-Attention を分析した論⽂ 2019/9/9 BERTology のススメ 46 ⽂献[8] 図9: レイヤー(全12ヘッド)を1つ無効にしたときの精度 →すべて使う(オレンジ)より良い場合がある 図8: ヘッドを1つ無効にしたときの精度 →すべて使う(オレンジ)より良い場合がある 図5: fine-tuning 前後でアテンションの変化 →出⼒に近いレイヤーで変化が⼤きい (濃いほど類似度が低い) • 異なるヘッドでタスクに関係なく同 じパターンが⼀貫して繰り返された • モデルの枝刈りとデータの繰り返し を減らす最適なアーキテクチャの発 ⾒を⽰唆図1: アテンションを5パターンに分類. 最初の3つは事前学習に関連, 最後の2つは意味 や構⽂情報を捉える. 異なるヘッドで同じパターンの繰り返しがあった
  • 47. BERT fine-tuning まとめ • 事前学習モデルとタスクに応じた fine-tuning で,容易に良好な 性能が得られることを確認 • BertModel クラスにある 2つの Function の使い⽅をマスター するだけで, 応⽤範囲は広い • [CLS] のベクトルを取り出す get_pooled_output() • 各トークンのベクトルを取り出す get_sequence_output() • 様々な応⽤タスクへの適⽤等, 楽しい BERTology 活動を! 2019/9/9 BERTology のススメ 47
  • 48. ⽂献 [1] ⻄⽥ 京介. “機械読解の現状と展望." ⾔語処理学会第25回年次⼤会チュートリアル資料, 2019 (2019). [2] Jacob Devlin, et al. "Bert: Pre-training of deep bidirectional transformers for language understanding." (2018) [3] Wang, Alex, et al. "Glue: A multi-task benchmark and analysis platform for natural language understanding." arXiv preprint arXiv:1804.07461 (2018). [4] 柴⽥ 知秀, 河原 ⼤輔, ⿊橋 禎夫. “ BERTによる⽇本語構⽂解析の精度向上." ⾔語処理学 会 第25回年次⼤会, pp.205-208, 名古屋, (2019.3). [5] Vaswani, Ashish, et al. "Attention is all you need." Advances in neural information processing systems. 2017. [6] Klein, Guillaume, et al. "Opennmt: Open-source toolkit for neural machine translation." arXiv preprint arXiv:1701.02810 (2017). 2019/9/9 BERTology のススメ 48
  • 49. ⽂献 [7] Shi, Peng, and Jimmy Lin. "Simple BERT Models for Relation Extraction and Semantic Role Labeling." arXiv preprint arXiv:1904.05255 (2019). [8] Zhang, Zhengyan, et al. "ERNIE: Enhanced Language Representation with Informative Entities." arXiv preprint arXiv:1905.07129 (2019). [9] Kovaleva, Olga, et al. "Revealing the Dark Secrets of BERT." arXiv preprint arXiv:1908.08593 (2019). 2019/9/9 BERTology のススメ 49
  • 51. 2019年5⽉: XLNet • BERT の弱点を修正し, 20以上のタスクで BERT を超えた 2019/9/9 BERTology のススメ 51 ⼈間 BERTXLNet BERT https://rajpurkar.github.io/SQuAD-explorer/ • BERT のマスク単語予測の 問題(通常発⽣しないため ノイズにもなる)を克服 • 20タスクで BERT を超えた (2018/5/21) • 機械読解タスク(左)では, single モデルで BERT の ensemble を超える ⽂献[10]
  • 52. 2019年2⽉: GPT-2 • 超⼤規模コーパス(800万⽂書)で学習した超巨⼤サイズの GPT 2019/9/9 BERTology のススメ 52 • コーパスは Common Crawl から⼈間がcurate/filter および Reddit で絞り込み • 1600次元, 1024トークン(BPE)の修正版GPT (パラメタ数は元のGPTの10倍) • zero-shot で 7/8 の⾔語モデルのタスクで Transformer-XL を上回りSOTA (上表) • zero-shot で 読解,要約,翻訳にも「ある程度」適応可 (タスクに有⽤な表現が多い場合) Table 3. Zero-shot results on many datasets. ⽂献[11]
  • 53. 2019年7⽉: RoBERTa • ⾔語理解タスクの事前学習のBERTを改善し,多くのタスクで SoTAを更新, SuperGLUEタスクでも⼈の成績に近く 2019/9/9 BERTology のススメ 53 BERTの改善: (1)動的なマスク (2)次⽂の予測を削除 (3)⼤きなバッチサイズ (4) ⾛査回数を増やす (5)⽂字ではなくバイト 単位のBPEを使⽤ ⽂献[12]
  • 54. 2019年7⽉: RoBERTa • ⾔語理解タスクの事前学習のBERTを改善し,多くのタスクで SoTAを更新, SuperGLUEタスクでも⼈の成績に近く 2019/9/9 BERTology のススメ 54 BERTの改善: (1)動的なマスク (2)次⽂の予測を削除 (3)⼤きなバッチサイズ (4) ⾛査回数を増やす (5)⽂字ではなくバイト 単位のBPEを使⽤ ⽂献[12][13]
  • 56. ⽂献 [10] Yang, Zhilin, et al. "XLNet: Generalized Autoregressive Pretraining for Language Understanding." arXiv preprint arXiv:1906.08237 (2019). [11] Radford, Alec, et al. "Language models are unsupervised multitask learners." OpenAI Blog 1.8 (2019). [12] Liu, Yinhan, et al. "RoBERTa: A Robustly Optimized BERT Pretraining Approach." arXiv preprint arXiv:1907.11692 (2019). [13] Wang, Alex, et al. "Superglue: A stickier benchmark for general-purpose language understanding systems." arXiv preprint arXiv:1905.00537 (2019). 2019/9/9 BERTology のススメ 56