做早餐燒菜有什么網站seo綜合查詢是什么
一、背景信息:
Transformer是一種由谷歌在2017年提出的深度學習模型。
主要用于自然語言處理(NLP)任務,特別是序列到序列(Sequence-to-Sequence)的學習問題,如機器翻譯、文本生成等。Transformer徹底改變了之前基于循環(huán)神經網絡(RNNs)和長短期記憶網絡(LSTMs)的序列建模范式,并且在性能上取得了顯著提升。
二、整體結構:
Transformer 由 Encoder 和 Decoder 兩個部分組成,Encoder 和 Decoder 都包含 6 個 block。

Transformer 的輸入
Transformer 的輸入由 x的 詞向量 和 位置向量 相加得到。
其中Transformer 在位置向量中保存單詞在序列中的相對或絕對位置信息,位置向量由PE(Positional Encoding)表示:

eg:假設n為序列長度,d為表示向量維度,原始輸入為 X o r i ? i n p u t X_{ori-input} Xori?input?( [ x 1 , x 2 . . . x n ] [x_{1},x_{2}...x_{n} ] [x1?,x2?...xn?])
則,原始輸入 X o r i ? i n p u t X_{ori-input} Xori?input?的詞向量矩陣為 X W E X_{WE} XWE?其維度為(n, d),
原始輸入 X o r i ? i n p u t X_{ori-input} Xori?input?的位置向量矩陣 X P E X_{PE} XPE?維度也為(n, d),
最終 Transformer 的輸入矩陣 X i n p u t X_{input} Xinput? = X W E X_{WE} XWE? + X P E X_{PE} XPE?維度也是(n, d)。
三、 Encoder
Encoder 部分由6個Encoder block 組成。
Encoder block 由Multi-Head Attention結合Add & Norm、Feed Forward結合 Add & Norm 組成。
即由下面兩部分組成:
X = L a y d e r N o r m ( X i n p u t + M u l t i H e a d A t t e n t i o n ( X i n p u t ) ) X = LayderNorm(X_{input} + MultiHeadAttention(X_{input})) X=LayderNorm(Xinput?+MultiHeadAttention(Xinput?))
X = L a y d e r N o r m ( X + F e e d F o r w o r d ( X ) ) X = LayderNorm(X + FeedForword(X)) X=LayderNorm(X+FeedForword(X))
MultiHeadAttention部分
其中MultiHeadAttention為多個Self-Attention進行Concat后linear而成:
Q = X i n p u t × W q Q = X_{input} \times W_{q} Q=Xinput?×Wq?
K = X i n p u t × W k K = X_{input} \times W_{k} K=Xinput?×Wk?
V = X i n p u t × W v V = X_{input} \times W_{v} V=Xinput?×Wv?
Z = A t t e n t i o n ( Q , K , V ) = s o f t m a x ( Q K T d k ) V Z = Attention(Q, K, V) = softmax( \frac{QK^{T} }{\sqrt{d_{k}} } )V Z=Attention(Q,K,V)=softmax(dk??QKT?)V
其中, Z 1 . . . . Z 8 Z_{1}....Z_{8} Z1?....Z8?為X_{input} 經過8個不同Self-Attention得到的結果
X = M u l t i H e a d A t t e n t i o n ( X i n p u t ) = L i n e a r ( C o n c a t ( Z 1 , Z 2 . . . . Z 8 ) ) X =MultiHeadAttention(X_{input} ) = Linear(Concat(Z_{1},Z_{2}....Z_{8})) X=MultiHeadAttention(Xinput?)=Linear(Concat(Z1?,Z2?....Z8?))
FeedForword部分
Feed Forward 層,是一個兩層的全連接層,第一層的激活函數(shù)為 Relu,第二層不使用激活函數(shù),公式如下。
F e e d F o r w o r d ( X ) = m a x ( 0 , X W 1 + b 1 ) W 2 + b 2 FeedForword(X) = max(0, XW_{1} + b_{1})W_{2} + b_{2} FeedForword(X)=max(0,XW1?+b1?)W2?+b2?
四、 Decoder
Decoder 由 6個Decoder block 以及最后的一個linear組成。
Decoder block 由 一個帶有 Masked的Multi-Head Attention結合Add & Norm和一個Multi-Head Attention結合Add & Norm以及一個Feed Forward結合 Add & Norm 組成。
X o u t p u t = X o u p u t ? o r i ? X M a s k X_{output}=X_{ouput-ori }\otimes X_{Mask} Xoutput?=Xouput?ori??XMask?
X = L a y d e r N o r m ( X o u t p u t + M a s k M u l t i H e a d A t t e n t i o n ( X o u p u t ) ) X = LayderNorm(X_{output} + MaskMultiHeadAttention(X_{ouput})) X=LayderNorm(Xoutput?+MaskMultiHeadAttention(Xouput?))
X = L a y d e r N o r m ( X + M u l t i H e a d A t t e n t i o n ( [ X a s Q , E C a s K , E C a s V ] ) X = LayderNorm(X + MultiHeadAttention([X_{as Q}, EC_{as K}, EC_{as V}]) X=LayderNorm(X+MultiHeadAttention([XasQ?,ECasK?,ECasV?])
X r e s u l t = S o f t m a x ( X ) X_{result} = Softmax(X) Xresult?=Softmax(X)
帶有 Masked的Multi-Head Attention層
其中帶有 Masked的Multi-Head Attention中 X o u p u t X_{ouput} Xouput?為Transformer 標簽對應輸出向量; X o u p u t ? o r i X_{ouput-ori} Xouput?ori?需要先 ? \otimes ? X M a s k X_{Mask} XMask?得到 X o u p u t X_{ouput} Xouput?
Q = X o u p u t × W q Q = X_{ouput} \times W_{q} Q=Xouput?×Wq?
K = X o u p u t × W k K = X_{ouput} \times W_{k} K=Xouput?×Wk?
V = X o u p u t × W v V = X_{ouput} \times W_{v} V=Xouput?×Wv?
Z = A t t e n t i o n ( Q , K , V ) = s o f t m a x ( Q K T d k ? X M a s k ) V Z = Attention(Q, K, V) = softmax( \frac{QK^{T} }{\sqrt{d_{k}} } \otimes X_{Mask} )V Z=Attention(Q,K,V)=softmax(dk??QKT??XMask?)V
其中第二個 Multi-Head Attention層
Self-Attention 的 K, V矩陣使用的是根據(jù)Encoder編碼的輸出矩陣C計算得到 K, V; Self-Attention 的 Q矩陣是根據(jù)Decoder block中的Masked Multi-Head Attention層輸出矩陣 Z 計算得到 Q。
Reference
1.Attention Is All You Need
2.Transformer模型詳解(圖解最完整版)
3.Self-Attention & Transformer完全指南:像Transformer的創(chuàng)作者一樣思考