Professional Documents
Culture Documents
BERT (v3)
BERT (v3)
李宏毅
Hung-yi Lee
1-of-N Encoding Word Embedding
apple = [ 1 0 0 0 0]
dog
bag = [ 0 1 0 0 0] rabbit
run
cat = [ 0 0 1 0 0] jump cat
dog = [ 0 0 0 1 0] tree
flower
elephant = [ 0 0 0 0 1]
Word Class
class 1 Class 2 Class 3
dog ran flower
cat bird jumped
walk tree apple
https://arxiv.org/abs/1902.06006
這是
加賀號護衛艦
他是尼祿
這也是加賀
她也是尼祿 號護衛艦
Contextualized Word Embedding
Contextualized
Word Embedding
潮水 退了 就 潮水 退了 就
<BOS> 潮水 退了 退了 就 知道
Each layer in deep LSTM can generate a
ELMO
… latent representation.
Which one should we use???
…
RNN RNN RNN … … RNN RNN RNN …
h2
RNN RNN RNN … … RNN RNN RNN …
<BOS> 潮水 退了 退了 就 知道
ELMO = 𝛼 1 + 𝛼 2
ELMO
small large
潮水 退了 就 知道 ……
Bidirectional Encoder Representations
from Transformers (BERT)
• BERT = Encoder of Transformer
Learned from a large amount of text
without annotation
Encoder
……
BERT
潮水 退了 就 知道 ……
Training of BERT
vocabulary size
Predicting the
• Approach 1: masked word
Masked LM Linear Multi-class
Classifier
……
BERT
潮水 [MASK]
退了 就 知道 ……
Training of BERT
Approach 2: Next Sentence Prediction
yes [CLS]: the position that outputs
classification results
Linear Binary [SEP]: the boundary of two sentences
Classifier Approaches 1 and 2 are used at the same time.
BERT
[CLS] 醒醒 吧 [SEP] 你 沒有 妹妹
Training of BERT
Approach 2: Next Sentence Prediction
No [CLS]: the position that outputs
classification results
Linear Binary [SEP]: the boundary of two sentences
Classifier Approaches 1 and 2 are used at the same time.
BERT
[CLS] 醒醒 吧 [SEP] 眼睛 業障 重
How to use BERT – Case 1
class
[CLS] w1 w2 w3
sentence
How to use BERT – Case 2
class class class
BERT
[CLS] w1 w2 w3
sentence
How to use BERT – Case 3
Class Input: two sentences, output: class
Example: Natural Language Inference
Linear Given a “premise”, determining whether
Classifier a “hypothesis” is T/F/ unknown.
BERT
[CLS] w1 w2 [SEP] w3 w4 w5
Sentence 1 Sentence 2
How to use BERT – Case 4
• Extraction-based Question
Answering (QA) (E.g. SQuAD) 17
Document: 𝐷= { 𝑑 1 , 𝑑 2 , ⋯ , 𝑑 𝑁 }
Query: 𝑄= {𝑞 1 , 𝑞 2 , ⋯ , 𝑞 𝑁 } 77 79
𝐷 QA
𝑠
𝑄 𝑒
𝑠=
17 , 𝑒=17
Model
output: two integers (, )
Answer: 𝐴= {𝑞 𝑠 , ⋯ , 𝑞 𝑒 } 𝑠=
77 , 𝑒=79
How to use BERT – Case 4
Learned 0.3 0.5 0.2
from scratch Softmax
s = 2, e = 3
The answer is “d2 d3”. dot product
BERT
[CLS] q1 q2 [SEP] d1 d2 d3
question document
How to use BERT – Case 4
Learned 0.1 0.2 0.7
from scratch Softmax
s = 2, e = 3
The answer is “d2 d3”. dot product
BERT
[CLS] q1 q2 [SEP] d1 d2 d3
question document
BERT 屠榜 ……
SQuAD 2.0
Enhanced Representation through
Knowledge Integration (ERNIE)
• Designed for Chinese
BERT
ERNIE
Source of image:
https://zhuanlan.zhihu.com/p/59436589 https://arxiv.org/abs/1904.09223
What does BERT
learn?
https://arxiv.org/abs/1905.05950
https://openreview.net/pdf?id=SJzSgnRcKX
https://arxiv.org/abs/1904.09077
Multilingual BERT
Trained on 104 languages
En Class 1 Zh ? Zh ?
En Class 2 Zh ?
En Class 3
Zh ? Zh ?
https://d4mucfpksywv.cloudfront.net/better-language-models/langua
ge_models_are_unsupervised_multitask_learners.pdf
BERT
(340M)
ELMO
(94M)
GPT-2
(1542M)
Source of image: https://huaban.com/pins/1714071707/
Generative Pre-Training (GPT)
退了
Many Layers …
𝑏2
𝛼 ^ 2,1 𝛼 ^ 2,2
𝑞 1 𝑘 1 𝑣 1
𝑞 2 𝑘 2 𝑣 2 𝑞 3 𝑘 3 𝑣 3
𝑞 𝑘
4
𝑣 4 4
𝑎1 𝑎2 𝑎3
𝑎4
<BOS> 潮水
Generative Pre-Training (GPT)
就
Many Layers …
𝑏3
𝛼 ^ 3 ,1 𝛼 ^ 3 ,2 𝛼 ^ 3 ,3
𝑞 1 𝑘 1 𝑣 1
𝑞 2 𝑘 2 𝑣 2
𝑞 3 𝑘 3 𝑣 3
𝑞 𝑘
4
𝑣 4 4
<BOS> 潮水 退了 就
Zero-shot Learning?
• Reading Comprehension
,
”Q:”, , “A:”
CoQA
• Summarization ,”TL;DR:”
English sentence 3 =
https://arxiv.org/abs/1904.02679
(The results below are from GPT-2)
Visualization
https://talktotransformer.com/
GPT-2