Transformer 모델 (1): Self-Attention
수업 목표
이번 수업의 핵심:
핵심 개념
Transformer 개요
Attention is all you need
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
3
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
I go to school
입력
나는 학교에 간다
출력
RNN을 통한 인코딩 방식
4
입력
출력
Forward
RNN
Backward
RNN
Query, Key, Value in Hash Table
5
Key (동물) | Value (다리 개수) |
강아지 | 4 |
닭 | 2 |
문어 | 8 |
오징어 | 10 |
고양이 | 4 |
Self-Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
6
Softmax
나는
학교에
간다
3.8
-0.2
5.9
0.7
0.1
0.2
Dot-Product Attention
7
Self-Attention의 선형 변환 예시
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
8
Softmax
나는
학교에
간다
3.8
-0.2
5.9
0.7
0.1
0.2
Embedding
입력
학교에
간다
Queries
Keys
Values
Dot-Product Attention for Query Batch
9
Row-wise
Softmax
Scaled Dot-Product Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
10
Scaled Dot-Product Attention
11
입력
학교에
간다
Embedding
Queries
Keys
Values
Attention score
Softmax output
가중합
Multi-Head Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
12
Multi-Head Attention
13
Attention Head #0
Attention Head #1
Attention Head #7
…
Multi-Head Attention
14
Concatenation
Multi-Head Attention
15
1) 문장 입력
학교에
간다
2) 각 단어를Embedding
🡪 이전 Encoder 층의 출력 사용
…
…
…
Self-Attention 시각화
Attention distribution 시각화
16
색깔 별로 다른 Head를 의미
Self-Attention 시각화
Attention distribution 시각화
17
Self-Attention 비교
Vaswani et al. “Attention Is All You Need”. NeurIPS. 2017.
18
요약
19
Transformer 모델 (2): 기타 구성 요소
수업 목표
이번 수업의 핵심:
핵심 개념
Transformer 개요
Attention is all you need
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
22
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
I go to school
입력
나는 학교에 간다
출력
Transformer의 Block
저자의 성 et al. “논문 이름”. 학회/저널 별칭. 년도. 링크.
23
Layer Normalization
Ba et al. “Layer Normalization”. ArXiv. 2016. https://arxiv.org/abs/1607.06450.
Wu et al. “Group Normalization”. ECCV. 2018.
24
Layer Normalization
Ba et al. “Layer Normalization”. ArXiv. 2016. https://arxiv.org/abs/1607.06450.
25
4
2
3
-3
2
1
5
2
학교에 갑니다
학교에 갑니다
학교에 갑니다
0.65
0.7
-1.35
-0.3
-0.45
-1.5
1.25
1.1
2.95
3.1
1.35
0.3
0.55
-0.5
-0.5
-0.2
Positional Encoding
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
26
Sinusoidal Positional Encoding
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
27
Decoder
Masked attention
Cross attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
28
Masked Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
29
Query: <SOS>
Key: <SOS>
Query: <SOS>
Key: 학교에
Query: 나는
Key: 학교에
Query
Key
<SOS>
나는
학교에
<SOS> 나는 학교에
<SOS>
나는
학교에
간다
나는
학교에
Transformer (Decoder)
<SOS>
나는
학교에
<SOS> 나는 학교에
Masked Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
30
<SOS>
나는
학교에
간다
나는
학교에
Transformer (Decoder)
Query: <SOS>
Key: <SOS>
Query: <SOS>
Key: 학교에
Query: 학교에
Key: 나는
Query: 나는
Key: 학교에
0.91
0.42
0.25
0.05
0.47
0.31
0.04
0.11
0.44
<SOS>
나는
학교에
<SOS>
나는
학교에
2.01
0.84
0.29
-0.94
0.96
0.48
-1.18
-0.49
0.84
Softmax
<SOS>
나는
학교에
<SOS>
나는
학교에
Masked Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
31
<SOS>
나는
학교에
간다
나는
학교에
Transformer (Decoder)
Query: <SOS>
Key: <SOS>
Query: <SOS>
Key: 학교에
Query: 학교에
Key: 나는
Query: 나는
Key: 학교에
1.00
0.47
0.25
0
0.53
0.31
0
0
0.44
<SOS>
나는
학교에
<SOS>
나는
학교에
2.01
0.84
0.29
-0.94
0.96
0.48
-1.18
-0.49
0.84
Softmax
<SOS>
나는
학교에
<SOS>
나는
학교에
Masked Attention
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
32
Query
Key
<SOS>
나는
학교에
<SOS> 나는 학교에
Value
Value
<SOS>
나는
학교에
<SOS> 나는 학교에
1.00
0.47
0.25
0
0.53
0.31
0
0
0.44
실험 결과
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
33
요약
34
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
ENCODER
DECODER
I go to school
입력
나는 학교에 간다
출력
Transformer 기반 Computer Vision 모델 구조�ViT
수업 목표
이번 수업의 핵심:
핵심 개념
컴퓨터 비전을 위한 Transformer
이미지 처리를 위해서 Transformer 구조를 사용할 수 있을까?
Vaswani et al. “Attention Is All You Need”. NIPS. 2017.
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
37
Convolutional
Neural Network
Transformer
Pixel-Level Transformer
이미지 처리를 위해서 Transformer 구조를 사용할 수 있을까?
Image credit: https://www.pexels.com/photo/cute-cat-sitting-outdoors-on-grass-and-looking-up-7149465/ CC0
38
Transformer Encoder
출력
이미지 픽셀
Pixel-Level Transformer의 문제점
39
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
Self-attention 행렬
Patch-Level Transformer
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
Image credit: https://www.pexels.com/photo/cute-cat-sitting-outdoors-on-grass-and-looking-up-7149465/ CC0
40
Vision Transformer (ViT)
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
41
Vision Transformer (ViT)
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
42
Positional embedding
Vision Transformer (ViT)
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
43
Positional embedding
Transformer Encoder
Special Token
([CLS] token)
Vision Transformer (ViT)
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
44
Positional embedding
Transformer Encoder
Special Token
([CLS] token)
Vision Transformer (ViT)
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
45
Positional embedding
Transformer Encoder
Special Token
([CLS] token)
고양이
Vision Transformer (ViT)
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
46
Transfer Learning
Pretraining (사전 학습)
Transfer learning (전이 학습)
47
입력 A
Task A
입력 B
Task B
Backprop
Transfer
Learning
Pretrained model
Vision Transformer (ViT)의 장점
ResNet 기반의 모델보다 더 좋은 성능을 보임
Dosovitskiy et al. “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale”. ICLR. 2021.
48
요약
49
Transformer Encoder