Transformer
Prof. Seungchul Lee
1
From RNN to Attention
2
From Sequential Modeling to Context Modeling
3
What We Already Know
4
Language Is Not Just a Time Series
�
5
The researcher asked the assistant to run the experiment again because he was not satisfied with the results.
Compress the Past into a State
�
6
I
go
to
school
LSTM
FC
나는
FC
학교에
FC
간다
<s>
나는
LSTM
학교에
LSTM
간다
LSTM
<eos>
FC
LSTM
LSTM
LSTM
LSTM
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Compress the Past into a State
�
7
LSTM
I
LSTM
go
LSTM
to
LSTM
school
LSTM
Context�Vector
FC
나는
FC
학교에
FC
간다
<s>
나는
LSTM
학교에
LSTM
간다
LSTM
<eos>
FC
Bottleneck
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Compress the Past into a State
8
LSTM
I
LSTM
go
LSTM
to
LSTM
school
LSTM
Context�Vector
FC
나는
FC
학교에
FC
간다
<s>
나는
LSTM
학교에
LSTM
간다
LSTM
<eos>
FC
Bottleneck
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
Embedding
I
go
to
school
나는
학교에
간다
English
Korean
Improving Sequential Learning
9
Attention!
Attention
10
Two Different Sentences
11
“I swam across the river to get to the other bank.”
“I walked across the road to get cash from the bank.”
Key Concepts in Attention (1)
12
Key Concepts in Attention (2)
13
“I swam across the river to get to the other bank.”
“I walked across the road to get cash from the bank.”
Attention as Weighted Aggregation
14
Analogy: Attention and Class Activation Maps (CAM)
15
Class Activation Map
Attention Mechanism (?)
16
Query
(question)
Key
(index)
Value
(content)
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
He
opened
the
door
and
smiled
He
opened
the
door
and
smiled
The Dictionary Analogy
17
The Dictionary Analogy
18
key
The Dictionary Analogy
19
key
value
The Dictionary Analogy
20
key
value
query
The Dictionary Analogy
21
The Dictionary Analogy
22
Word Tokenization
23
power
of
industrial
AI
power of industrial AI
0.1 | 1.2 | | 0.3 |
0.8 | 0.2 | | 0.1 |
0.3 | 1.8 | | 1.3 |
0.5 | 0.7 | | 0.2 |
Word Embedding
24
https://arize.com/blog-course/embeddings-meaning-examples-and-how-to-compute/
The
cat
sat
Material type
Original text
Embedding
Embedding
0.1 | 1.2 | 0.4 | 0.3 |
0.3 | 0.6 | 0.2 | 0.8 |
0.9 | 0.7 | 0.5 | 0.2 |
“Steel”
“Aluminum”
“Rubber”
0.6 | 0.9 | 0.0 | 0.1 |
0.5 | 0.4 | 0.3 | 0.9 |
0.2 | 0.7 | 0.8 | 0.7 |
Word Embedding
25
https://arize.com/blog-course/embeddings-meaning-examples-and-how-to-compute/
Encoding Positions
26
“the dog bit the man”
“the man bit the dog”
1
2
3
5
4
1
2
3
5
4
Encoding Positions (Intuitive Insights)
27
Encoding Positions (Intuitive Insights)
28
Positional Embedding
29
The
cat
sat
0.1 | 1.2 | 0.4 | 0.3 |
0.3 | 0.6 | 0.2 | 0.8 |
0.9 | 0.7 | 0.5 | 0.2 |
Input embedding
Encoded position
Final input
+
=
Element-wise addition
0.0 | 1.0 | 0.0 | 1.0 |
0.8 | 0.5 | 0.0 | 0.9 |
0.9 | -0.4 | 0.0 | 0.9 |
0.1 | 2.2 | 0.4 | 1.3 |
1.1 | 1.1 | 0.2 | 1.7 |
1.8 | 0.3 | 0.5 | 1.1 |
How to Compute Similarity Between Two Vectors
30
How Attention is Computed (w/ Single Query)
31
Softmax
Y
Query
Key
Value
Compute attention scores
Extract features with high attention
How Attention is Computed
32
Softmax
Query
Key
Value
Compute attention scores
Extract features with high attention
Y
From Encoded Inputs to Attention
33
Encoded Input
0.1 | 2.2 | 0.4 | 1.3 |
1.1 | 1.1 | 0.2 | 1.7 |
1.8 | 0.3 | 0.5 | 1.1 |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
Query
Key
Value
Learnable parts
Learning Self-Attention with Neural Networks
34
Mat mul
Scaling
Softmax
Mat mul
Transformer
35
Transformer
36
Multi-Head Attention
37
Multi-Head Attention
38
Mat mul
Scaling
Softmax
Mat mul
Self-attention
Self-attention
Self-attention
Concatenate
Linear
Transformer Layers
39
Multi-head
self-attention
Add & norm
MLP
Add & norm
Why Are Transformers Effective for Sequential Learning?
40
Local receptive field
Sequential processing
RNN
CNN
Transformer
Global attention
Encoder and Decoder Transformers
41
Encoder-decoder Transformers
42
Cross-attention transformer layer
Cross-attention transformer layer
Linear+Softmax
…
Self-attention transformer layer
Self-attention transformer layer
…
Encoder
Decoder
Note: Encoder-decoder Structure for Natural Language
43
Encoder Transformers
44
“I <mask> across the river to get to the <mask> bank.”
Transformer layer
Transformer layer
<class>
Linear+
Softmax
Linear+
Softmax
Linear+
Softmax
…
…
…
…
…
…
…
Decoder Transformers
45
| -inf | -inf | -inf | -inf |
| | -inf | -inf | -inf |
| | | -inf | -inf |
| | | | -inf |
| | | | |
I
swam
across
the
river
I
swam
across
the
<start>
Inputs
Outputs
Masked transformer layer
Masked transformer layer
<start>
Linear+
Softmax
Linear+
Softmax
Linear+
Softmax
…
…
…
…
…
…
…
“I <mask> across the river to get to the other bank.”
“I swam <mask> the river to get to the other bank.”
“I swam across <mask> river to get to the other bank.”
…
Transformer Summary
46
Vision Transformers (ViT)
47
Lab: Transformer Implementation in Python