Deep Learning - Transformer

想搞懂 Transformer ? Attention Is All You Need !
Transformer networks
一種用在自然語言處理 (natural language processing, NLP) 的神經網路
Transformer architecture
由 Encoder 和 Decoder 組成
且透過 self-attention,不透過 recurrence 或 convolutions 來生成 output

RNN vs Transformer
RNN | Transformers |
---|---|
不擅長長期的依賴關係 | 促進長期的依賴關係 |
順序計算 | 平行計算 |
Transformer Training

第一個 vector 是 “How” 和其他單詞之間關係的資訊
第二個 vector 是 “are” 和其他單詞之間關係的資訊
第三個 vector 是 “you” 和其他單詞之間關係的資訊
都是平行處理
Transformer Inference

透過 Autoregressive decoding 自回歸解碼,每次生成一個 token,每個標記都基於之前生成的標記
例如一開始先解碼出了「你」,而第二次解碼時,「你」會被加入 start token,而後解出「好」
Transformer architecture step by step explanation
Input Embedding & Positional Encoding

Input Embedding
將 word 或 token 轉換成 text sequence 進 dense numerical vector
(dense 的意思是資料很稠密,意旨大部分的資料都不是 0;
相反意思為稀疏)
這些 vector 代表了詞語的意義
連續空間中的 vector 表示其捕捉詞與詞之間相似性的能力
Positional Encoding
將 positional information 嵌入 input embedding,使模型能夠理解序列的先後順序
例如以下語句有不同含意:
- Tom loves his dog
- His dog loves Tom
Self-Attention
Self-attention 使模型能夠有選擇性地關注不同的輸入部分,考量同一句子中單詞之間的關係
當
很大的時候更能體現 self-attention 的效用
常用在影像處理
Self-Attention 有很多變型,我的文章連結:
Deep Learning - Self-Attention- 計算內積 dot product:
- 用 softmax Normalize 正規化:
代表一個句子中每個單詞
- 計算 attention value 從 normalized weights 和其相關聯的 inputs:
舉例來說: “I ate an apple”, input embeddings 如下:
word | Numbers | Numbers | Numbers |
---|---|---|---|
I | 0.2 | 0.3 | 0.1 |
ate | 0.1 | 0.4 | 0.5 |
an | 0.3 | 0.2 | 0.4 |
apple | 0.5 | 0.6 | 0.2 |
Step1: 計算內積 dot product
- Dot product between “I” and “I”:
- Dot product = (0.2 * 0.2) + (0.3 * 0.3) + (0.1 * 0.1) = 0.04 + 0.09 + 0.01 = 0.14
- Dot product between “I” and “ate”:
- Dot product = (0.2 * 0.1) + (0.3 * 0.4) + (0.1 * 0.5) = 0.02 + 0.12 + 0.05 = 0.19
- Dot product between “I” and “an”:
- Dot product = (0.2 * 0.3) + (0.3 * 0.2) + (0.1 * 0.4) = 0.06 + 0.06 + 0.04 = 0.16
- Dot product between “I” and “apple”:
- Dot product = (0.2 * 0.5) + (0.3 * 0.6) + (0.1 * 0.2) = 0.10 + 0.18 + 0.02 = 0.30
全部算完後如下:
I | ate | an | apple | |
---|---|---|---|---|
I | 0.14 | 0.19 | 0.16 | 0.3 |
ate | 0.19 | 0.42 | 0.31 | 0.39 |
an | 0.16 | 0.31 | 0.29 | 0.35 |
apple | 0.3 | 0.39 | 0.35 | 0.65 |
Step 2: Normalize using softmax
用 softmax Normalize 正規化:
- Exponentiate each value:
: - Divide each exponentiated value by the sum:
Step3. 計算 attention value 從 normalized weights 和其相關聯的 inputs:
when
Query, Key, Value
將 self-attention 視為映射 query, key-value 為 output 的方法
query, key, value 和 output 都是 vector
Query:我們想知道的訊息 The information we want to find
Key:上下文 The context
Value:訊息 The information
添加三個可訓練的權重矩陣,與輸入序列嵌入
序列中的每個詞都有相關的 query,key 和 value 的 vector,這些 vector 來自他們的 embedding
Basic Self-Attention 對於 query, key 和 value 有相同的表示

Scaled Dot-Product Attention

query, keys, values, output 都是 vectors
將所有的 query 包成一個矩陣 Q
把 keys 和 values 包成矩陣 K 和 V

Multi-Head Attention
透過多個平行的 self-attention 來捕捉詞語之間不同面向的關係
每個 self-attention layer 都有不同的
在 original transformer 裡的 8 個 attention head 允許以不同方式關注序列中的不同部份,類似 CNN 中的通道有多個內核

而因為解碼時,輸出值只取決於先前的輸出,因此會對未來的輸出進行 masked multi-head attention (屏蔽)
The model must not be able to see future words,所以 matrix 會變成這樣
Encoder Decoder Attention

Other Concepts
Additive process
加法過程通常被稱為 residual connection 殘差連接,有助於保留輸入訊息,同時從層的計算中納入新學習的訊息
Layer normalization
將各層的值正規化,使其 mean 均值為 0,variance 變異數為 1
Output Probabilities

linear layer 將 decoder 輸出的高維文字表示轉換為與詞彙量大小相同的空間
softmax layer 輸出文字序列中每個位置生成詞彙的機率
Summery
Transformer 的特點是透過 Encoder-Decoder 結構和 self-attention 來平行化處理

Summery
最後,我們再來看一眼完整的 Transformer
你現在已經完全理解了嗎。
Reference
學習內容與截圖來源
- 黃貞瑛老師的機器學習課程 - Transformer
- NLP的首選模型Transformer介紹
- 【機器學習 2022】各式各樣神奇的自注意力機制 (Self-attention) 變型