1
CS 589 Lecture 9
Monday 6:30-9:00
Kidde 228
All zoom links in Canvas
Most slides adapted from Stanford CS224
Seq2seq, Attention, Transformer
photo: https://www.scubedstudios.com/information-retrieval/
Review of language models
where xt+1 can be any word in the vocabulary
2
mary has a little
lamb
goat
bit
book
0.2
0.3
0.05
0.01
Training RNN method 1: Language modeling
3
mary
a
little
...
...
[0.01,
0.01,
0.85]
...
[0.79,
0.01,
0.10]
...
[0.09,
0.82,
0.10]
...
[0.09,
0.62,
0.10]
...
0.85]
0.62,
[0.79,
0.82,
mary
has
a
little
Training RNN method 2: Text classification
4
output distribution
[0.2,
0.3]
0.5,
neg
pos
neutral
mary
has
a
little
Today’s lecture
5
Machine translation
6
Training NN method 3: Machine Translation
7
man
is
born
free
is
born
free
...
...
[0.01,
0.01,
0.85]
...
[0.79,
0.01,
0.10]
...
[0.09,
0.82,
0.10]
...
[0.09,
0.10]
...
0.85,
0.62]
[0.79,
0.82,
0.01]
fers
le
dans
(only for the output sentences)
Encoder-decoder framework
… dans les fers <START>
8
Encoder-decoder framework
… dans les fers <START> man
9
man
Encoder-decoder framework
… dans les fers <START> man is
10
man is
Encoder-decoder framework
… dans les fers <START> man is born ….
11
man is born free
hN' encodes the info of input French sentence
encoder RNN
decoder RNN
Training NN task 3: Neural machine translation
12
train:
predict:
similar training objective as language modeling
Encoder-decoder framework: Other applications
What does SIT stand for?
Stevens Institute of Technology
Open the file file.txt and read it line by line
with open('file.txt', 'r') as fin:
lines = fin.readlines()
Can you show me the ticket from NY to LA?
OK, what are the dates of your flight?
machine translation
factoid question answering
natural language to code
conversational agent
Encoder (Input Network)
… dans les fers
14
LSTM encoder:
Other encoders:
GRU
LSTM/GRU + attention
Elmo
BERT
…
encoder RNN
hN' encodes the info of input French sentence
Decoder (Output network)
<START> man is born ….
15
man is born free
decoder RNN
Decoding (Prediction)
<START> man is born ….
16
man is born free
decoder RNN
Beam search decoding
17
<START>
he
I
…
Beam search decoding
18
<START>
he
I
hit
struck
was
got
Beam search decoding
19
<START>
he
I
hit
struck
was
got
Beam search decoding
20
<START>
he
I
hit
struck
was
got
Decoding: How do we know when to stop?
21
Beam search decoding: Selecting output hypothesis
22
Machine translation timeline
Statistical machine translation: IBM model 1-5 [Brown et al. 1993]
2014: first seq2seq paper was published
2016: Google Translate switched from SMT to NMT
The SMT system, built by hundreds of engineers over many years, outperformed by the NMT systems trained by a handful of engineers in a few months
23
Evaluating machine translation
24
25
#times t_i occurs in the hypothesis h
#times t_i occurs in the j-th reference for hypothesis h
n: length of hypothesis
L: length of reference
total #tokens in hypothesis h
Today’s lecture
26
Attention mechanism
27
?
Attention mechanism
28
… est dans les <START>
encoder
attention
dot product [Luong et al. 2015];
RNNSearch [Bahdanau et al. 2014]
Attention mechanism: Dot-product attention
29
attention
weight
encoder
[Luong et al. 2015]
Attention mechanism: Dot-product attention
30
weight
[Luong et al. 2015]
Attention mechanism: Dot-product attention
[Luong et al. 2015]
Step 1-2
Attention mechanism: Dot-product attention vs RNNSearch
32
For t = 1,…, T:
RNN search attention
For t = 1,…, T:
dot product attention
Attention mechanism: RNNSearch
33
attention
weight
encoder
GRU
[Bahdanau et al. 2014]
Attention mechanism: RNNSearch
34
weight
[Bahdanau et al. 2014]
Attention mechanism: RNNSearch
Step 1-2
GRU
[Bahdanau et al. 2014]
Attention mechanism: RNNSearch
Step 1-2
output
[Bahdanau et al. 2014]
Performance of attention [Luong et al. 2015]
37
Today’s lecture
38
Transformer [2017]
Encoder/decoder in transformer
Self attention
source: https://pretteyandnerdy.wordpress.com/2019/04/26/attention-is-all-you-need/
[Lin et al. 2017]
Self attention
… dans les fers <START> man
42
[Lin et al. 2017]
man
Self attention
source-source attention
source-target attention
target-target attention
+
+
43
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
tous les hommes naissent égaux
tous les hommes naissent égaux
tous les hommes naissent égaux
all men are born equal
all men are born equal
all men are born equal
Self attention
S to S
T to S
T to T
44
Attention vs. Self Attention
Position embedding
KV Cache
Summary
48
49
Pre-trained word embeddings
mary
has
a
little
From pretrained embedding to pretrained encoder
ELMo [Peters et al. 2018]
Competition of pretrained language models
source: https://www.microsoft.com/en-us/research/blog/turing-nlg-a-17-billion-parameter-language-model-by-microsoft/
BERT: Bidirectional Encoder Representations
Pre-training BERT
54
BERT: Bidirectional Encoder Representations
Pre-training BERT
55
BERT: Bidirectional Encoder Representations
Pre-training BERT
56
BERT: Bidirectional Encoder Representations
Pre-training BERT
57
BERT: Bidirectional Encoder Representations
Fine-tuning BERT
58
BERT experimental results
59
Effects of pretraining tasks
60
Effects of dimensionality and training time
61
Effects of dimensionality and training time
62
BERT: Open source release
One reason for BERT’s success was the open source release
63
from transformers import AutoTokenizer, AutoModelForMaskedLM
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForMaskedLM.from_pretrained("bert-base-uncased")
Homework 4
64